Reputation: 628
I've been struggling to understand this issue, hope someone can give me some hints. I have a global variable defined using the nifty counter idiom.
In the header file the definition looks like:
BOOST_SYMBOL_EXPORT extern MyClass& GVar;
In the source file I have the following:
MyClass& GVar = reinterpret_cast<MyClass&>(Buffer);
Using GCC on Debian and Ubuntu everything is compiled and linked correctly. On Windows, though, I get the following error when trying to link the DLL to my test executable:
error LNK2001: unresolved external symbol "class MyClass& GVar" (GVAR_MANGLED_NAME)
I also checked that the symbol exists in the DLL and it seems to be there, among the exported ones. I don't really know what else to try anymore.
Does anyone have any experience with a problem like this?
Upvotes: 1
Views: 616
Reputation: 628
So the issue was due to this (from here):
__declspec(dllimport) can be used on both code and data, and its semantics are subtly different between the two. When applied to a routine call, it is purely a performance optimization. For data, it is required for correctness. [...] If you export a data item from a DLL, you must declare it with
__declspec(dllimport)
in the code that accesses it. Using__declspec(dllimport)
is optional on function declarations, but the compiler produces more efficient code if you use this keyword. However, you must use__declspec(dllimport)
for the importing executable to access the DLL's public data symbols and objects.
Long story short, for functions and classes dllimport is optional, for data, for example global variables, it is mandatory
Upvotes: 1