Visual Studio DLL export problem for instances of class instances and functions

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 #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 

// 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; } 
+3
source share
3 answers

I solved the problem. Under the Windows class template and the template, functions are exported differently, and there is an interesting read on the network.

VS compilers export class template characters if an instance of an instance template is on a translation unit (.cpp).

However, in the case of a function template, the keyword "__declspec (dllexport)" must be explicitly specified for characters present in the dynamic library.

eg

 template EXPORT void HelpingRegistration<double>( double ); //EXPORT is defined as __declspec(dllexport) 

This is just another case where VS solves things differently. It is interesting to see here: http://www.codesynthesis.com/~boris/blog/2010/01/18/dll-export-cxx-templates/

+2
source

I believe that you need to export specializations. You tried this in your .cpp file:

 template class EXPORT TemplatedStaticLib<double>; template class EXPORT TemplatedStaticLib<std::string>; 

and almost the same in your title:

 template class EXPORT TemplateStaticLib<double>; template class EXPORT TemplateStaticLib<std::string>; 

I think this will work with your EXPORT macro (assuming the .cpp file see __declspec(dllexport) and the header sees __declspec(dllimport) ). I admit that I am not an expert with Windows' __declspec .

I acknowledge that I received a response from this other answer to intarwebs: http://social.msdn.microsoft.com/Forums/vstudio/en-US/4fd49664-e28e-4f23-b1eb-b669d35ad264/function-template-instantation- export-from-dll (scroll all the way to the bottom for Final Version Franjo555.)

+3
source

I believe this is due to the fact that the compiler creates specialized code for the template class when the template class is first used with certain parameters. Since the compiler uses only the included header files (.h) when compiling the compilation unit (.cpp files), all tmplate code must be available in the .h files. You can export custom template classes from dll, but not the template classes themselves.

0
source

All Articles