title: Exporting static data in a DLL
body: <p>I have a DLL which contains a class with static <strong>members</strong>. I use <code>__declspec(dllexport)</code> in order to make use of this class's <strong>methods</strong>. But when I link it to another project and try to compile it, I get "unresolved external symbol" errors for the static data.</p>
  
  <p>e.g.
  In DLL, Test.h</p>
  
  <pre><code>class __declspec(dllexport) Test{
  protected:
      static int d;
  public:
      static void m(){int x = a;}
  }
  </code></pre>
  
  <p>In DLL, Test.cpp</p>
  
  <pre><code>#include "Test.h"
  
  int Test::d;
  </code></pre>
  
  <p>In the application which uses Test, I call m().</p>
  
  <p>I also tried using __declspec(dllexport) for each method separately but I still get the same link errors for the static members.</p>
  
  <p>If I check the DLL (the .lib) using dumpbin, I could see that the symbols have been exported.</p>
  
  <p>For instance, the app gives the following error at link time:</p>
  
  <pre><code>1&gt;Main.obj : error LNK2001: unresolved external symbol "protected: static int CalcEngine::i_MatrixRow" (?i_MatrixRow@CalcEngine@@1HA)
  </code></pre>
  
  <p>But the dumpbin of the .lib contains:</p>
  
  <pre><code>Version      : 0
    Machine      : 14C (x86)
    TimeDateStamp: 4BA3611A Fri Mar 19 17:03:46 2010
    SizeOfData   : 0000002C
    DLL name     : CalcEngine.dll
    Symbol name  : ?i_MatrixRow@CalcEngine@@1HA (protected: static int CalcEngine::i_MatrixRow)
    Type         : data
    Name type    : name
    Hint         : 31
    Name         : ?i_MatrixRow@CalcEngine@@1HA
  </code></pre>
  
  <p>I can't figure out how to solve this. What am I doing wrong? How can I get over these errors?</p>
  
  <p>P.S. The code was originally developed for Linux and the .so/binary combination works without a problem</p>
  
  <p>EDIT: In the given case, the static variables are not directly referred by the application but the method is inlined since it's in the header. I was able to resolve the link errors by moving the methods to the .cpp file.</p>