This is system dependent, but you'll have to ensure that the symbols in the DLL containing the definitions of the member functions and static member data correctly exports the symbols, and that the DLL using them correctly imports them. Under Linux, this means using the -E
option when linking the executable (if the symbols are defined in the executable); under Windows, you usually have to use conditionally compiled compiler extensions, see __declspec
; Microsoft compilers do not support DLL's in standard C++.
EDIT:
Here's an example that works on my system (VC 2010):
In A.h:
#ifndef A_h_20111228AYCcNClDUzvxOX7ua19Fb9y5
#define A_h_20111228AYCcNClDUzvxOX7ua19Fb9y5
#include <ostream>
#ifdef DLL_A
#define A_EXPORT __declspec(dllexport)
#else
#define A_EXPORT __declspec(dllimport)
#endif
class A_EXPORT InA
{
static std::ostream* ourDest;
public:
static void setDest( std::ostream& dest );
static std::ostream* getStream() { return ourDest; }
};
#endif
In A.cpp:
#include "A.h"
std::ostream* InA::ourDest = NULL;
void
InA::setDest( std::ostream& dest )
{
ourDest = &dest;
}
In main.cpp:
#include <iostream>
#include "A.h"
int
main()
{
InA::setDest( std::cout );
std::cout << InA::getStream() << std::endl;
return 0;
}
Compiled and linked with:
cl /EHs /LDd /DDLL_A A.cpp
cl /EHs /MDd main.cpp A.lib
As I understand it (I'm more a Unix person), all of the .cpp which
become part of the dll should have /DDLL_A in the command line which
invokes the compiler; none of the others should. In Visual Studios,
this is usually achieved by using separate projects for each dll and
each executable. In the properties for the project, there's an entry
ConfigurationProperties→C/C++→Preprocessor→Preprocessor
Definitions; just add DLL_A
there (but only in the one project that
generates A.ddl
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…