DLL import with a template class
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
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement