Exporting static class members

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<font style="font-family:Verdana" size=2>Hello,
I have a class that is exported from a DLL and has a static member:
</font><font style="font-family:Verdana" size=2>  </font>
<div style="text-align:left <font style="font-family:Verdana" size=2><font size=2><span style="font-family:Courier class __declspec( dllexport ) MyClass </font></font> <font style="font-family:Verdana" size=2><font size=2><span style="font-family:Courier {<br style="font-family:Courier <span style="font-family:Courier public:<br style="font-family:Courier <span style="font-family:Courier   MyClass();<br style="font-family:Courier <span style="font-family:Courier   static int CONSTANT;<br style="font-family:Courier <span style="font-family:Courier };</font>

<font size=2><span style="font-family:Verdana The implementation of the class is in a separate cpp file:</font>

<font size=2><span style="font-family:Courier #include "MyClass.h"<br style="font-family:Courier <br style="font-family:Courier <span style="font-family:Courier int MyClass::CONSTANT = 0;<br style="font-family:Courier <br style="font-family:Courier <span style="font-family:Courier MyClass::MyClass() { }<br style="font-family:Courier </font>
<font size=2><span style="font-family:Verdana I try to use the class and the exported static member from a function in another project:</font>

<font size=2><span style="font-family:Courier #include "MyClass.h"<br style="font-family:Courier <span style="font-family:Courier int main()<br style="font-family:Courier <span style="font-family:Courier {<br style="font-family:Courier <span style="font-family:Courier     MyClass a;<br style="font-family:Courier <span style="font-family:Courier     int i = MyClass::CONSTANT;<br style="font-family:Courier <span style="font-family:Courier }</font>

<font size=2><span style="font-family:Verdana But when I try to build my solution I get a link error:</font>
<font size=2><span style="font-family:Courier main.obj : error LNK2001: unresolved external symbol "public: static int MyClass::CONSTANT" (?CONSTANT@MyClass@@2HA)</font>

I<font size=2><span style="font-family:Verdana can use other members or methods in the class.  Is there something special I need to be able to link the static members?  I checked the lib with dumpbin and I see the static member in the list of exported items.<br style="font-family:Verdana <br style="font-family:Verdana <span style="font-family:Verdana Thanks.</font>
</font>

View the full article
 
Back
Top