When you instantiate a template fully -- you have a complete type. It is no different from any other types. You need to include the header for B
and also compile-time linking in with a lib
file or dynamically load the dll to link to the definition.
Have you read this article: http://support.microsoft.com/kb/168958 ?
Here's a brief summary of what I tested (and it worked):
Create a dummy DLL project
- Used the Win32 Console application wizard to generate the dll header/source files called:
template_export_test
- Added the following:
file: template_export_test.h
#ifndef EXP_STL
#define EXP_STL
#endif
#ifdef EXP_STL
# define DECLSPECIFIER __declspec(dllexport)
# define EXPIMP_TEMPLATE
#else
# define DECLSPECIFIER __declspec(dllimport)
# define EXPIMP_TEMPLATE extern
#endif
EXPIMP_TEMPLATE template class DECLSPECIFIER CdllTest<int>;
file: template_export_test.cpp
template<class T>
CdllTest<T>::CdllTest(T t)
: _t(t)
{
std::cout << _t << ": init\n";
}
Create the test application
- Use the wizard to create a Win32 Console application called:
driver
- Edit the Linker project settings of this project:
- Add to Linker > General > Additional Library Directories: path to
template_export_test.lib
- Add to Linker > Input > Additional Dependencies:
template_export_test.lib
- Include the
template_export_test.h
in the main cpp file
#include "c:\Documents and Settings\...\template_export_test.h"
using namespace std;
int main(int argc, char** argv) {
CdllTest<int> c(12);
}