Explicit template instances not properly exported from dll

View: New views
1 Messages — Rating Filter:   Alert me  

Explicit template instances not properly exported from dll

by Topi Mäenpää :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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?



------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
MinGW-users mailing list
MinGW-users@...

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users