Template "redefinition" linker error. (attempt #3)

R

Robbie Hatley

Victor Bazarov said:
Robbie said:
[...]
I ran into a problem a few days ago when I added a couple of
template functions to one of my personal library headers.

My library, librh.a [...]

Linking is not defined by [the] C++ [standard].

Possibly true, but not very relevant. C++ translation units
must still be translated to machine language if one wishes
to actually EXECUTE one's program. And if there are more
than one translation unit (which is usually the case), then
the objects must be linked, regardless of whether or not the
standards committee chose to define the word "link" in some
special way.

Furthermore, the standard DOES indirectly "define" the concept
of "linking" by including the famous "one definition" rule. The
existance of a rule requires enforcement. Compilers are not
capable of enforcing this rule, because they operate on only one
translation unit at a time. Enforcing such a rule require a tool
which operates simultaneously on the tranlated versions of all
of the translation units of a project. Such a tool is called a
"linker".
Whatever you do to get where you are is implementation-defined.

Some things are implimentation defined; others are not.
The "one definition" rule is definitely not.
You are probably much better off asking this in the newsgroup
dedicated to your compiler/linker.

If my goal was to become an expert in certain fine points
of usage of one particular compiler and linker, and if
newsgroups existed for those tools, then that might be
the case.

But actually, my interest is more along the lines of how to
make my code more portable, rather than how to make it more
brittle.

Specifically, what I'd like to know is:

1. Is it normally necessary to declare a template function in a
header as being "inline"?
2. Is instantiating a non-inline template function in a library
object module likely to cause "redefinition" errors?
3. If a compiler first compiles the non-library object modules
for a program, instantiating a template in the processs,
then attempts to link to a pre-exising library object which
already contains an identical instantiation, could that cause
problems? Or should compilers and linkers be able to handle
that?

I find it hard to believe that no one here has ever had to deal
with such issues before, or that drastically different approaches
must be taken for each and every individual compiler and linker.
Surely there are better and worse ways of handling these issues?

--
Very curious (and persistant),
Robbie Hatley
Tustin, CA, USA
lone wolf intj at pac bell dot net
home dot pac bell dot net slant earnur slant
 
T

Thomas Tutone

Robbie Hatley wrote:

[snip]
If my goal was to become an expert in certain fine points
of usage of one particular compiler and linker, and if
newsgroups existed for those tools, then that might be
the case.

But actually, my interest is more along the lines of how to
make my code more portable, rather than how to make it more
brittle.

Specifically, what I'd like to know is:

Sadly, I only know the answer to one of your questions.
1. Is it normally necessary to declare a template function in a
header as being "inline"?

No, and the lack of "inline" does not violate the one-definition rule,
even though the header appears in multiple translation units.
2. Is instantiating a non-inline template function in a library
object module likely to cause "redefinition" errors?

I don't know.
3. If a compiler first compiles the non-library object modules
for a program, instantiating a template in the processs,
then attempts to link to a pre-exising library object which
already contains an identical instantiation, could that cause
problems? Or should compilers and linkers be able to handle
that?

I don't know.
I find it hard to believe that no one here has ever had to deal
with such issues before, or that drastically different approaches
must be taken for each and every individual compiler and linker.
Surely there are better and worse ways of handling these issues?

Unfortunately, different compilers use different strategies to
implement templates, so there may not be a single answer to your
question. For background, see Chapter 6 of C++ Templates: The Complete
Guide, by Vandevoorde and Josuttis.

Sorry not to be more helpful.

Best regards,

Tom
 
I

Ian Collins

Robbie said:
Specifically, what I'd like to know is:

1. Is it normally necessary to declare a template function in a
header as being "inline"? No.

2. Is instantiating a non-inline template function in a library
object module likely to cause "redefinition" errors?
Too platform specific, how the various bits of the tool chain interact
is implementation defined.
3. If a compiler first compiles the non-library object modules
for a program, instantiating a template in the processs,
then attempts to link to a pre-exising library object which
already contains an identical instantiation, could that cause
problems? Or should compilers and linkers be able to handle
that?
Same answer as 2. I'm afraid.
I find it hard to believe that no one here has ever had to deal
with such issues before, or that drastically different approaches
must be taken for each and every individual compiler and linker.
Surely there are better and worse ways of handling these issues?
If your tools are well behaved, you shouldn't have this problem...

Are you sure all the bits are compiled with the same tools and the same
options?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top