'multiple definitions' problem

I

Ittay Dror

Hi

I have this peculiar situation. I have a library of utility classes,
which I use to compile a binary. I want to reimplement a method in
class. The class is only a collection of static methods.

So I copy the source and header files to another place, change the
include path so this location will precede the location of the headers
of the library, change the method (details after) and compile.

Everything works, except when linking. The linker complains about some
static methods in the class, but not all of them. Is there a general
reason for that?

The chnage in the method is to add another parameter, with a default
value.

Btw, just copying the files, with no changes compiles fine.

Thanx,
Ittay
 
J

John Harrison

Hi

I have this peculiar situation. I have a library of utility classes,
which I use to compile a binary. I want to reimplement a method in
class. The class is only a collection of static methods.

So I copy the source and header files to another place, change the
include path so this location will precede the location of the headers
of the library, change the method (details after) and compile.

Everything works, except when linking. The linker complains about some
static methods in the class, but not all of them. Is there a general
reason for that?

The chnage in the method is to add another parameter, with a default
value.

Btw, just copying the files, with no changes compiles fine.

Thanx,
Ittay

Sounds to me like you are trying to use your modified class to hide the
original class by making sure that the modified classes are included and
linked first, is that correct? If so then you are breaking the one
definition rule (ODR) which says that if multiple copies of a class are
compiled then those copies must be identical, otherwise you have undefined
behaviour.

john
 
J

John Harrison

Sounds to me like you are trying to use your modified class to hide the
original class by making sure that the modified classes are included and
linked first, is that correct? If so then you are breaking the one
definition rule (ODR)

I should say probably breaking the ODR. The issue is whether parts of your
program are compiling the old definitions and parts the new definitions.

john
 
S

Senapathy

Ittay Dror said:
Hi

I have this peculiar situation. I have a library of utility classes,
which I use to compile a binary. I want to reimplement a method in
class. The class is only a collection of static methods.

So I copy the source and header files to another place, change the
include path so this location will precede the location of the headers
of the library, change the method (details after) and compile.

Everything works, except when linking. The linker complains about some
static methods in the class, but not all of them. Is there a general
reason for that?

The chnage in the method is to add another parameter, with a default
value.

Btw, just copying the files, with no changes compiles fine.

Thanx,
Ittay

How about the order of linking?
How are you using the modified implementation? Are you adding the modified
implementation to your compilations (or project)?

For example you could do it like this while linking (I am giving a VC
example)
cl.exe modified.cpp -> to give modified.obj
link.exe xxx, yyy, modified.obj....

If you still link to the old library (maybe a .lib file), the problem would
arise because of the difference in your header and the old lib.
You should not be still linking to the old library - you should include the
modified source file in your project rather.

Senapathy
 
I

Ittay Dror

John Harrison said:
I should say probably breaking the ODR. The issue is whether parts of your
program are compiling the old definitions and parts the new definitions.

john

classes from within the library use the old definition, classes from
the new one use the new definition. but why does it matter? to the
compiler/linker there are two distinct methods, e.g., static void
A::foo(int i) in the library, and static void A::foo(int i, int j) in
my code. After mangling, they should be distinct symbols, afaik.

ittay
 
T

tom_usenet

classes from within the library use the old definition, classes from
the new one use the new definition. but why does it matter? to the
compiler/linker there are two distinct methods, e.g., static void
A::foo(int i) in the library, and static void A::foo(int i, int j) in
my code. After mangling, they should be distinct symbols, afaik.

From what you said earlier, some methods have two different
definitions, one in the library and one in the separate source file.
The linker doesn't know which one is the correct one, hence the
multiple definition error. If you used a DLL rather than a static
library (under Windows), the problem would probably go away, or become
more subtle.

Having two different definitions of a class in one program is a
frankly terrible idea, since even if you get it working, it will be
fragile if you try it on a different compiler, or even with different
compiler options.

What are you trying to do? Why can't you just derive a new class from
the old one, adding the new definitions you want, and even hiding the
old ones if appropriate.

Tom
 
J

John Harrison

john
classes from within the library use the old definition, classes from
the new one use the new definition. but why does it matter? to the
compiler/linker there are two distinct methods, e.g., static void
A::foo(int i) in the library, and static void A::foo(int i, int j) in
my code. After mangling, they should be distinct symbols, afaik.

The only people who can answer that are the developers of your particular
compiler/linker. The C++ standard simply says it is undefined behaviour.
That's the only answer that is applicable on this newsgroup. For an answer
to why it happens on your particular platform you should ask on a group that
deals with your particular platform.

john
 
I

Ittay Dror

tom_usenet said:
Having two different definitions of a class in one program is a
frankly terrible idea, since even if you get it working, it will be
fragile if you try it on a different compiler, or even with different
compiler options.

What are you trying to do? Why can't you just derive a new class from
the old one, adding the new definitions you want, and even hiding the
old ones if appropriate.

the class is just a collection of static methods. deriving from it
won't help me here (i can do it, but i am just extending an existing
method's functionality, i don't want some places in my code calling
A::foo and others A2::foo)

ittay
 
T

tom_usenet

the class is just a collection of static methods. deriving from it
won't help me here (i can do it, but i am just extending an existing
method's functionality, i don't want some places in my code calling
A::foo and others A2::foo)

How about using namespaces? Put the modified version of A in a
different namespace? Then you can add a using declaration for the one
you want to use in that source file. Fighting against the language
rules is not really worth it. A non-inline function shall have one and
only one definition.

Tom
 

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
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top