468,107 Members | 1,412 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

Strange template "redefinition" linker error.

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
Robbie Hatley
Tustin, CA, USA
lonewolfintj atsign pacbell period net
home period pacbell period net slantbar earnur slantbar
Jun 28 '06 #1
0 1555

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by lomat | last post: by
22 posts views Thread by Qopit | last post: by
7 posts views Thread by ben | last post: by
8 posts views Thread by TTroy | last post: by
1 post views Thread by Robbie Hatley | last post: by
2 posts views Thread by Robbie Hatley | last post: by
1 post views Thread by Ioannis Gyftos | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.