title: Visual Studio dll export issue for class and function template instantiations
body: <p>I am using VS2008 in win7 and g++ 4.7 in CentOS 18. The issue is only seen on Windows when I used dynamically shared library. When I convert it static library the program links fine. </p>
  
  <p>I understand that in shared library template functions/class either should be defined in header file or template instantiation of the template types(parameters) should be provided through compilation unit. I have chosen the later option. I have done it before, I went through </p>
  
  <p><a href="https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file/495056#495056">Why can templates only be implemented in the header file?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1022623/c-shared-library-with-templates-undefined-symbols-error">C++ Shared Library with Templates: Undefined symbols error</a></p>
  
  <p>But I cannot figure out why in windows as soon as I convert the library to dll it failed to resolve symbols:
  error LNK2019: unresolved external symbol "void __cdecl HelpingRegistration(double)" (??$HelpingRegistration@N@@YAXN@Z) referenced in function _main</p>
  
  <p>In Windows it works fine with static library. 
  In Linux both dynamic and shared library works. </p>
  
  <pre><code>//Static library
  //Library header
  #ifndef _TEMPLATED_STATIC_LIB_
  #define _TEMPLATED_STATIC_LIB_
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "Export.h"
  
  template&lt;typename T&gt;
  class EXPORT TemplatedStaticLib
  {
  public:
      TemplatedStaticLib(){};
      ~TemplatedStaticLib(){};
  
      void print(T t);
  
  };
  
  template&lt;typename T&gt;
  EXPORT void HelpingRegistration(T);
  
  #endif
  </code></pre>
  
  <p>//library .cpp</p>
  
  <pre><code>#include "TemplatedStaticLib.h"
  #include &lt;typeinfo&gt;
  
  
  template&lt;typename T&gt;
  void TemplatedStaticLib&lt;T&gt;::print(T t)
  {
      std::cout &lt;&lt; "Templated Print: "&lt;&lt; t&lt;&lt; " type:: "  &lt;&lt; typeid(t).name() &lt;&lt; std::endl;
  }
  
  //Class Template explicit instantiation
  template class TemplatedStaticLib&lt;double&gt;;
  template class TemplatedStaticLib&lt;std::string&gt;;
  
  template&lt;typename T&gt;
  void  HelpingRegistration(T t)
  {
      std::cout &lt;&lt; "Function Templated Print: "  &lt;&lt; t &lt;&lt; " type: " &lt;&lt; typeid(t).name() &lt;&lt; std::endl;
         //return t;
  }
  
  //function template explicit instantiation
  template void HelpingRegistration&lt;&gt;( double );
  template void HelpingRegistration&lt;&gt;( std::string );
  </code></pre>
  
  <p>//Windows symbol exporter</p>
  
  <pre><code>//.h
  
  #ifndef STATIC_LIB_EXPORT
  #define STATIC_LIB_EXPORT
  
  #if !defined WIN32
      #define EXPORT
  #elif defined LIB_EXPORTS
      #define EXPORT __declspec(dllexport)
  #else
      #define EXPORT __declspec(dllimport)
  #endif
  
  //STATIC_LIB_EXPORT
  #endif
  </code></pre>
  
  <p>//library user .cpp</p>
  
  <pre><code>#include &lt;TemplatedStaticLib/TemplatedStaticLib.h&gt;
  
  #include&lt;string&gt;
  
  int main(int argc, char* argv[])
  {
      double aDouble = 3.9;
      TemplatedStaticLib&lt;double&gt; double_test; 
      double_test.print(aDouble);
  
      std::string aString = "James";
      TemplatedStaticLib&lt;std::string&gt; string_test; 
      string_test.print(aString);
  
      HelpingRegistration(aDouble);
      HelpingRegistration(aString);
  
  
      return 0;
  }
  </code></pre>