Explicit template instantiation and -fvisibility. Gcc bug?
Mon Nov 17 13:39:00 GMT 2008
I'm currently experimenting with the -fvisibility=hidden visibility
flag. During that I have stumbled upon some very peculiar behavior of
GCC, which I managed to boil down to the following small example:
template < typename _T > class TemplatedClass
template < typename _T > class DependentTemplatedClass
These classes are explicitly instantiated in a library. In one
compilation unit I do:
template class LIBRARY_EXPORT TemplatedClass < int >;
And in another:
template class LIBRARY_EXPORT DependentTemplatedClass < TemplatedClass
< int > >;
The LIBRARY_EXPORT macro sets the visibility of the instantiation to
default (i.e. public).
When I compile the library like this (see the BuildFail.sh script in
the attachment). The TemplatedClass<int>::TemplatedClass symbol
appears twice in the library, the first being hidden the second being
a weak symbol. When linking to this library the linker cannot find
However when I export the class definition (as well as the
instantiation), the linking goes fine (see BuildWorkaround.sh). In
this case the symbol is defined twice in the library, both times as a
The cause seems to be the instantiation of
DependentTemplatedClass<TemplatedClass<int > >, which induces an
instantiation of TemplatedClass<int> as well. However I would expect
this instantiation to have public visibility as well, because the
DependentTemplatedClass instantiation is public. Is this a GCC bug, or
am I doing something wrong here?
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 1587 bytes
Desc: not available
More information about the Gcc-help