By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,890 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,890 IT Pros & Developers. It's quick & easy.

Strange template "redefinition" linker error.

P: n/a
I'd always thougth that a C++ compiler/linker should be able to
instantiate a template in mulitple places (say, in two different
translation units), even using the same template parameters so
that the resulting functions or classes are identical, without
causing any errors. Or so I thought.

But a few days ago, I added a couple of new template functions
to one of my personal library modules, and I got this linker
error message (from memory, as near as I can recall):

Linker Error: template std::string ConvertFromFfblk<std::string>
(ffblk const & FileBlock) redefined in MyProgram.cpp.
See original definition in rhutil.cpp.

The only place this template is defined in in a header, rhutil.h,
which is included in both translation units.

I was able to get the program to link by declaring the template
as "inline" in rhutil.h and recompiling rhutil.o before attempting
to recompile my program.

Is it normally necessary to declare a template function in a header
as being "inline"? Or is it just something in this particular case
that makes it necessary?

Perhaps the fact that a library module (a *.o file embedded in a
*.a file) instantiates this template has something to do with it?
If the compiler first compiles the non-library object module(s)
for a program (instantiating a template in the processs), then
attempts to link to a pre-exising library object module which
already contains an identical instantiation, could this cause
problems? Or should the compiler and linker be able to handle
that?
--
Robbie Hatley
Tustin, CA, USA
lonewolfintj atsign pacbell period net
home period pacbell period net slantbar earnur slantbar
Jun 28 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.