I use VS2008 in win7 and g ++ 4.7 on CentOS 18. This problem only occurs on Windows when I used the dynamically shared library. When I convert a static library, the program links perfectly.
I understand that in the templates of the shared library / class must either be defined in the header file, or an instance of the template of types (parameters) of the template is created through the compilation unit. I chose a later option. I did it before, I went through
Why can templates be implemented only in the header file?
C ++ shared library with patterns: Undefined character error
But I canβt understand why in windows, as soon as I convert the library to dll, it couldnβt resolve characters: error LNK2019: unresolved external character "void __cdecl Help in registration (double)" (?? $ HelpingRegistration @N @@ YAXN @Z) referenced by the _main function
On Windows, it works great with a static library. Linux runs both a dynamic and a shared library.
//Static library //Library header #ifndef _TEMPLATED_STATIC_LIB_ #define _TEMPLATED_STATIC_LIB_ #include <iostream> #include <string> #include "Export.h" template<typename T> class EXPORT TemplatedStaticLib { public: TemplatedStaticLib(){}; ~TemplatedStaticLib(){}; void print(T t); }; template<typename T> EXPORT void HelpingRegistration(T); #endif
// .cpp library
#include "TemplatedStaticLib.h" #include <typeinfo> template<typename T> void TemplatedStaticLib<T>::print(T t) { std::cout << "Templated Print: "<< t<< " type:: " << typeid(t).name() << std::endl; } //Class Template explicit instantiation template class TemplatedStaticLib<double>; template class TemplatedStaticLib<std::string>; template<typename T> void HelpingRegistration(T t) { std::cout << "Function Templated Print: " << t << " type: " << typeid(t).name() << std::endl; //return t; } //function template explicit instantiation template void HelpingRegistration<>( double ); template void HelpingRegistration<>( std::string );
// Windows character exporter
//.h
// user of the .cpp library
#include <TemplatedStaticLib/TemplatedStaticLib.h> #include<string> int main(int argc, char* argv[]) { double aDouble = 3.9; TemplatedStaticLib<double> double_test; double_test.print(aDouble); std::string aString = "James"; TemplatedStaticLib<std::string> string_test; string_test.print(aString); HelpingRegistration(aDouble); HelpingRegistration(aString); return 0; }