win32 dlls and template instanciations

Mathieu Lacage Mathieu.Lacage@sophia.inria.fr
Mon Jul 2 14:19:00 GMT 2007


hi,

It was suggested to me to bring this problem of mine onto this mailing
list, so, here it is.

Some of my c++ templated code makes heavy use of template classes which
have static methods and these static methods often contain static local
variables. I wrote a reduced test case of my problem:
http://www-sop.inria.fr/dream/personnel/Mathieu.Lacage/tmp.tar.gz

What this code shows is that, on ELF systems, both the library and the
main binary access the same Test<0>::Exec function instance and, thus,
the same local static variable instance. If you happen to run this code
on mingw (cygwin will work too), the main binary and the dll will access
different copies of the Test<0>::Exec function which means that they
access different versions of the local static variable instance, which
ends up breaking a large amount of my code.

I do not know enough about the win32 linking and loading mechanism to
tell whether or not this is really a bug or just a feature but I am
looking for a workaround. the simplest idea I had originally was to try
to use the c++ manual template instanciation model: I thought that I
could inhibit all template instanciation everywhere except in a specific
file located in my dll where I would generate all the instanciations I
need explicitely and then, that I could make my main binary import all
template instanciations automatically.

I thus tried to re-compile my program with -fno-implicit-templates but
this did not eliminate template instanciation: the dll and the main
binary still contain a copy of the template. Is there something special
I should know about this option ?

If I could make this option work, I would still need to make my main
binary import all template instances. Danny Smith helpfully suggested
the following syntax to perform explicit template instanciation import: 

+ // Import rather than instantiate
+ extern template class __declspec (dllimport) Test <0>;

But I cannot imagine myself having to import all potential template
instanciations by hand: is there a way to tell ld to automatically
import all the templates it does not instanciate ?

thanks for your help,
Mathieu
-- 



More information about the Binutils mailing list