From: Topi M. <top...@in...> - 2009-10-30 06:05:27
|
I have a base class defined in a dll. It is a template, and I have explicitly instantiated it in the dll. I want my application to use the explicit instance. Consider the following example: // dll.h #ifdef BUILD_DLL # define LIB_EXPORT __declspec(dllexport) #else # define LIB_EXPORT __declspec(dllimport) #endif template <class T> struct Base { public: Base() {} virtual ~Base() {} }; extern template struct LIB_EXPORT Base<int>; // dll.cc #include "dll.h" template class Base<int>; Compile this into a dll: g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DBUILD_DLL -o dll.o dll.cc g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -mthreads -Wl -shared -Wl,--out-implib,libdll.a -o dll.dll dll.o Fine. Now the application: // main.cc #include "dll.h" #include <iostream> struct A : Base<int> { A() { std::cout << "Hello world!\n"; } }; int main() { A a; } Compile and link: g++ -c -O2 -frtti -fexceptions -mthreads -Wall -o main.o main.cc g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -Wl,-subsystem,console -mthreads -Wl -o app.exe main.o -ldll Everything seems to be fine, but starting the application results in "Application initialization failed (0xc0000005)". It doesn't even reach main(). I have tried almost all imaginable compiler and liner command-line options with no luck. The application works if Base has no vtable. (Comment out the virtual destructor.) The same code works with many GCC versions on Linux, and with MSVC 200{3,8} on Windows XP and Vista. It fails with MinGW on both XP and Vista. What is the deal? Is there a work-around? |