Template Instantiation

Joe Buck jbuck@synopsys.com
Sun Feb 8 18:44:00 GMT 1998


> Can someone clarify exactly how template instantiation
> works in egcs?  I'm using x86/Linux/ELF and SPARC/Solaris 
> systems.  

On both of these systems, it works pretty much like it does with Borland
C++.  All needed templates are instantiated in each object file, but
weak symbols are used.  At link time, one weak symbol is used as the
definition.  If you have a recent GNU ld, the duplicate definitions, if
any, get stripped out and are not included in the binary.  If you have
an older linker or are using the Solaris linker, everything works, but
your executable may contain extra copies of some functions.

> I've been using -frepo when linking object files.  When creating 
> liraries, I've been manually instantiating templates though IIRC, 
> someone mentioned that this wasn't necessary.

You're better off using no flags at all and just allowing things to work.

> In the (very outdated) g++ FAQ at www.cygnus.com,

It contains text referring to old versions as well as sections referring
to the latest version.

> I found this passage:
> 
>    Jason Merrill writes: "To perform closure on a set of objects, just try
>    to link them together. It will fail, but as a side effect all needed
>    instances will be generated in the objects." 
> 
> Maybe I'm dense, but I don't understand this.

It refers to 2.7.2.x .  The procedure is not recommended with 2.8.0 or
egcs.  The reason it worked is because -frepo attaches the instantiated
functions to the original .o files.  So if you did

	g++ -frepo *.o -o dummy

then -frepo would try to instantiate templates as needed; the link will
fail because there is no main defined.  But after that you can do

	ar qc libfoo.a *.o

to make the library, and the library will contain template expansions.



More information about the Gcc mailing list