[cmake-developers] [patch] Export template instantiations with GenerateExportHeader

Brad King brad.king at kitware.com
Fri Jul 31 09:09:20 EDT 2015


On 07/30/2015 05:17 PM, Roger Leigh wrote:
> Are the duplicated template exports here at the level of the translation 
> unit not elided when linking the DLL?  In the dlltest git repo, I 
> specifically create DLLs with duplicate template exports in different 
> translation units to make sure it doesn't error out, and it appears to 
> cope.  I haven't checked on whether it removed duplicates from the 
> linked objects though--is this even possible?  I thought all current 
> toolchains were able to eliminate duplicate instantiations, or else 
> you'd have massive explosions of duplicate templates common across all 
> translation units.  Given the backwardness of the Windows linker, it 
> wouldn't surprise me if it were true though...

I'm not an expert on the MS linker, but at least some toolchains
treat implicit template instantiations as weak (W) symbols and
explicit template instantiations as normal (T) symbols.  Linkers
will resolve duplicate W symbols but allow only one T symbol.

Regardless, it is inefficient to ask the compiler to repeatedly
do an explicit instantiation when only one is needed.  I've seen
projects that keep each explicit instantiation in a dedicated
translation unit so that when one .o is needed for its instantiation
it doesn't force bringing in a bunch of other instantiations from
the same object.  With the instantiate-in-header approach then all
the objects in your library will instantiate all the templates
whose headers they include.  If the header always does the "extern"
explicit instantiation then the compiler does not need to do the
instantiation in any of the objects except the dedicated one.

-Brad


More information about the cmake-developers mailing list