473,395 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

"Victor Bazarov" <v.********@comAcast.net> wrote:
Robbie Hatley wrote:
[...]
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
Jun 28 '06 #1
2 2776
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

Jun 29 '06 #2
Robbie Hatley wrote:

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?

--
Ian Collins.
Jun 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lomat | last post by:
Hello, While compiling a file, I get following error .... ================================= /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include/g++/type_traits.h:14 2: redefinition of...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
9
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
0
by: Robbie Hatley | last post by:
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...
1
by: Robbie Hatley | last post by:
I asked about this yesterday, but no one bit. So I'll ask again. I can be a persistant cuss. :-) I ran into a problem a few days ago when I added a couple of template functions to one of my...
5
by: thermate | last post by:
Dr.Bricmont, We are extremely proud of you for your brilliant analysis and courage to come out and say it. Your article brings out not one but several key concepts, worth close reading. The...
2
by: Mohammad Omer | last post by:
Hi, i am developing an application which uses WAB API's, for doing all this i am using vs2k5. I have wab.h header file included in my project to use WAB api's but after compilation one error...
0
by: anto.anish | last post by:
Hi , Since, i did not want to write all instantiations in Source file of all template methods for various different datatypes that my client might use, Instead, i choose to write implementation...
1
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.