title: Export template class from DLL
body: <p>I've got a problem exporting a templated class from a DLL and then importing it in my application.</p>
  
  <p>My project is structured like this</p>
  
  <ul>
  <li>CommonDLL where the base templated class (CASingleton) is defined</li>
  <li>CoreDLL where a class (CAEngine) is derived from CASingleton on the first DLL</li>
  <li>A test executable using those two DLLs</li>
  </ul>
  
  <p>My problem now is that everything work, until I use some functions defined in the first base class (CASingleton) which is a templated class.</p>
  
  <p>These are the classes:</p>
  
  <p>CASingleton in the first DLL (CommonDLL)</p>
  
  <p>CASingleton.h</p>
  
  <pre><code>template&lt;class T&gt;
  class ANIMACOMMON_API CASingleton
  {
  public:
      T&amp; CreateInstance()
      {
          assert(!m_pSingletonInstance &amp;&amp; "Trying to create for the second time a singleton.");
  
          m_pSingletonInstance = new T();
  
          return GetInstance();
      }
  
      void DeleteInstance()
      {
          assert(m_pSingletonInstance);
          m_pSingletonInstance = 0;
      }
  
      T&amp; GetInstance()
      {
          assert(m_pSingletonInstance);
          return *m_pSingletonInstance;
      }
  
      T* GetInstancePtr()
      {
          assert(m_pSingletonInstance);
          return m_pSingletonInstance;
      }
  
  private:
      static T* m_pSingletonInstance;
  };
  </code></pre>
  
  <p>CASingleton.cpp</p>
  
  <pre><code>#include "ASingleton.h"
  
  template&lt;class T&gt; T* CASingleton&lt;T&gt;::m_pSingletonInstance = NULL;
  </code></pre>
  
  <p>ANIMACOMMON_API is defined like this</p>
  
  <pre><code>#ifdef ANIMACOMMON_EXPORTS
  #define ANIMACOMMON_API __declspec(dllexport)
  #else
  #define ANIMACOMMON_API __declspec(dllimport)
  #endif
  </code></pre>
  
  <p>Then in the second DLL (CoreDLL) I have this class derived from CASingleton</p>
  
  <p>CAEngine.h</p>
  
  <pre><code>class ANIMACORE_API CAEngine : public CASingleton&lt;CAEngine&gt;
  {
  public:
      CAEngine();
      virtual ~CAEngine();
  
      void Test() { int i = 0; i++; }
  };
  </code></pre>
  
  <p>CAEngine.cpp</p>
  
  <pre><code>template class CASingleton&lt;CAnimaEngine&gt;;
  
  CAnimaEngine::CAnimaEngine()
  {
  }    
  
  CAnimaEngine::~CAnimaEngine()
  {
  }
  </code></pre>
  
  <p>ANIMACORE_API defined like this</p>
  
  <pre><code>#ifdef ANIMACORE_EXPORTS
  #define ANIMACORE_API __declspec(dllexport)
  #else
  #define ANIMACORE_API __declspec(dllimport)
  #endif
  </code></pre>
  
  <p>The last thing is the application which simply has a main function like this:</p>
  
  <pre><code>int main()
  {
      CAEngine engineOne;
      engineOne.Test();
      engineOne.CreateInstance();
      CAEngine engineTwo;
  }
  </code></pre>
  
  <p>All the DLLs compile fine, but when I try to compile the application the compile gives me this error</p>
  
  <pre><code>main.obj : error LNK2019: riferimento al simbolo esterno "__declspec(dllimport) public: class CAEngine &amp; __thiscall CASingleton&lt;class CAEngine&gt;::CreateInstance(void)" (__imp_?CreateInstance@?$CASingleton@VCAEngine@@@@QAEAAVCAEngine@@XZ) non risolto nella funzione _main
  </code></pre>
  
  <p>It compiles only if I comment</p>
  
  <pre><code>engineOne.CreateInstance();
  </code></pre>
  
  <p>The only function I use from the base class.</p>
  
  <p>Any idea?</p>