This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: interface/implementation: export keyword?
- To: tjw30 at cam dot ac dot uk
- Subject: Re: interface/implementation: export keyword?
- From: Joe Buck <jbuck at synopsys dot com>
- Date: Wed, 28 Oct 98 8:37:43 PST
- Cc: jbuck at synopsys dot com, egcs at cygnus dot com
> > The short answer is to put your template declarations in an .h file,
> > and your template definitions in a .cxx (or .cc) file, and until
> > egcs supports export, #include the .cxx file as well as the .h file
> > when you use the templates.
>
> Thanks for the advice, but there's two reasons I didn't do this in the
> first place:
>
> The first reason is that I couldn't get make to do proper dependency
> checking without rewriting all the rules :-(
I don't understand this. Standard approaches for "make depend" will
automatically generate the dependencies, based only on who #includes
what.
> Also, I have a template Matrix class (say) which is used by two other
> classes: they both use a Matrix<double>. If I put the classes in
> separate .cc files I get a bigger executable than if I put them in one
> big .cc file, which (I imagine) means that my final executable has two
> copies of the Matrix<double> code instead of one --
No, if you use the GNU linker, the duplicate copies will be eliminated.
> How does the STL do it? Does the compiler have to emit code for all
> the template instantiations itself and put them in executables,
No, the compiler puts the instantiations in object files where they
occur, but with a special symbol type called a weak symbol. If there
are multiple definitions, the linker chooses one and ignores the others.
If you are using a non-GNU linker you will have duplicate copies in
the executable.
> Surely there must be an elegant way of doing this already, or does
> nobody else use templates and require them to be space-efficient?
If you use the GNU linker and are on an ELF system (like Linux, Solaris,
or an SVR4-based Unix), your templates are space-efficient, though your
.o files may be costing you extra space.
For other platforms, there is an alternate method: the -frepo flag.
When it is used, the compiler still must see the template definitions,
but it does not immediately expand them if they are not inline functions.
A link-time pass will cause the correct templates to be expanded.