Linker Error LNK2001 when using static variable in native class

G

Guest

I have a native singleton in a C++/CLI dll. To compile the dll itself I put a
declaration of the instance variable into the cpp file (otherwise the linker
complains with a LNK 2020).
I now want to use the singleton also from outside the dll project. So I just
#include the header file in a file in another dll. It compiles but the
linker doesn't find the static variable (LNK2001).

Any suggestions?
 
B

Ben Voigt [C++ MVP]

Fabian said:
I have a native singleton in a C++/CLI dll. To compile the dll itself I put
a
declaration of the instance variable into the cpp file (otherwise the
linker
complains with a LNK 2020).
I now want to use the singleton also from outside the dll project. So I
just
#include the header file in a file in another dll. It compiles but the
linker doesn't find the static variable (LNK2001).

Any suggestions?

Your immediate problem would be solved with a declaration tagged with
__declspec(dllimport)... but that's a bad idea.

Write extern "C" wrappers for the functions you want to call from the DLL's
client. Sharing native C++ classes across module boundaries is a bad idea,
because it's almost impossible to get the exact same definition on both
sides, so you end up violating ODR and getting undefined behavior. Use
extern "C" functions instead, or DCOM, or use a managed class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top