Explicit template instantiation and -fvisibility. Gcc bug?

Jeroen Wijnhout jeroen.wijnhout@gmail.com
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
 TemplatedClass ();

template < typename _T > class DependentTemplatedClass
 DependentTemplatedClass ();
 _T m_Member;

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
this symbol.

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
weak symbol.

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...
Name: GccVisibility.tar.bz2
Type: application/x-bzip2
Size: 1587 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20081117/215c6c28/attachment.bz2>

More information about the Gcc-help mailing list