[prog] C++ strange linking behaviour

Kathryn Hogg kjh at flyballdogs.com
Tue Nov 9 09:27:54 EST 2004


Miriam Ruiz said:
> Sometimes coders put the templated code in a different
> header than the definitions to be a bit more
> structurated.

Template instantiation like the cfront method used to encourage separation
of template declarations from definitions.   HP's old CC compiler used to
spit out a warning if it coulding find $something.C if a template was
declared in $something.h.  While you were compiling, it built a repository
of template definitions and the files they were declared in.  When it
needed to instantiate a template it would generate a little file that
would look like

#include <something.h>
#include <something.C>

static something<int> x;

and compiled it with special flags that forced template instantiation.

It kept repeating this process until all templates were resolved.  This
worked nice but could be tediously slow.

> Yep, for what I know, that's true. The other trick
> would be to force the compiler to build some of the
> versions of the templated code for certain classes in
> another file, so it was already compiled and thus
> could be linked, but I guess it's a poor idea that
> throws away all the power of templates.

Closing out templates in shared libraries can be quite useful.  You may
have "private" templates that end users of your library shouldn't care
about.  Especially if they are only using lib A which uses lib B. 
Nowadays. all the compilers I use (gcc, HP aCC, Solaris CC, Tru64 cxx, and
AIX xlC) do a great job of template instantiation that his isn't much of
an issue.

-- 
Kathryn
http://womensfooty.com



More information about the Programming mailing list