Jump to content
  • Advertisement
Sign in to follow this  
Woltan

DLL import with a template class

This topic is 4704 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey folks, I once again rely on your help. This time regarding the import of a template based class in a dll and I hope this is the right place to post. This is my source: HEADER: #ifndef TEST_H #define TEST_H template <typename TYPE> class Test { public: __declspec(dllexport) Test(TYPE *p); private: TYPE *dummy; }; #endif CPP FILE: #include "Header//Test.h" template <typename TYPE> __declspec(dllexport) Test<TYPE>::Test(TYPE *p) { dummy = p; return; } Everything works out just fine. A dll and a lib are created but in the project where I need the dll I have trouble doin this: #pragma comment(lib,"TestDLL.lib") #include <stdlib.h> #include "Test.h" //(I changed dllexport to dllimport int main() { int *i = new int(5); <--- no probs here Test<int> *p = NULL; <--- no probs here p = new Test<int>(i); <--- Problem! return 0; } In Visual C++ the error message is this: Unresolved external symbol "__declspec(dllimport) public: __thiscall Test<int>::Test<int>(int *)" (__imp_??0?$Test@H@@QAE@PAH@Z) At least I hope it is cause I do not have an english version of visual. Well I hope I pointed out my problem sufficiant and at the correct location, and of cause I hope you can help me with this one. Cherio Woltan

Share this post


Link to post
Share on other sites
Advertisement
You can't have a template of anykind in a dll. In templates you must include the deffinition in the files that use them. Other wise you will have to create a seperate class for seperate datatypes. The reason for this is because templates are only compiled for the datatypes they are used with.

Share this post


Link to post
Share on other sites
That's not actually true. You can place explicit instantiations of template functions and classes inside a DLL and export them. See this article on MSDN for more details.

Share this post


Link to post
Share on other sites
Okey, thanks for the quick help. Too bad that it is not possible to do that. Do you have any suggestions how I can work around that problem. OK the article says you can export an instance in a dll, but that woldn't solve my problem.
Thanks for any help
regards Woltan

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!