 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Kuznetsov Dmitriy Guest
|
Posted: Wed Oct 22, 2003 6:14 pm Post subject: linkage |
|
|
There are two definitions of the same function in a project
(in separate .cpp files):
void foo () { return; } // for example
There are declaration & call of the function 'foo' in third .cpp
file of the project.
Can anybody explain to me what will be linker do (must do)
in this situation?
what will be in next cases:
- there are additional definitions of 'foo' in .lib (.dll) files
included in to the project ?
- there are invokes of 'foo' in .lib (.dll),
included in to the project ?
P.S.
I need to redefine global operator new & operator delete
so as to _all_ (.cpp, .lib, .dll) libraries included in my project
use my version of global new & delete
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael Tiomkin Guest
|
Posted: Thu Oct 23, 2003 3:03 pm Post subject: Re: linkage |
|
|
"Kuznetsov Dmitriy" <dmitriy9 (AT) mail (DOT) ru> wrote
| Quote: | There are two definitions of the same function in a project
(in separate .cpp files):
void foo () { return; } // for example
There are declaration & call of the function 'foo' in third .cpp
file of the project.
Can anybody explain to me what will be linker do (must do)
in this situation?
|
As far as I know, there is no standard for linker behaviour
in this situation. I think that the tradition for Unix linkers is
that the linker takes the first appearance of 'foo' and ignores others.
In C++, when you use templates, the compiler usually creates
several definitions of the same class/methods/function in different
files, and the linker takes one of these definitions.
One of the interesting cases is when you compile some of your files
with optimization, and others without optimization. The speed of your
code will depend on the order of objects given to the linker.
| Quote: | what will be in next cases:
- there are additional definitions of 'foo' in .lib (.dll) files
included in to the project ?
- there are invokes of 'foo' in .lib (.dll),
included in to the project ?
|
A .dll (or a .so on Unix) is an executable. All its external references
must be resolved at link time. Usually, they are found in other
libraries, and these libraries are loaded together with the given
library. This means that appearances and references of/to 'foo'
in an executable will remain the same as before loading.
On Unix, you can usually relink an executable and change its contents.
On Windows, it's much harder because the format of an executable
is different from the format of an object, and you need special tools
to relink a .dll.
A .lib that contains code or data (or a .a on Unix) is a repository
of object files, and it's used by the linker like any other .o or .obj files.
The appearances of 'foo' will not be included, and the references
to 'foo' will be resolved to the first 'foo' found by the linker.
A .lib that contains reference to code or data in a .dll (or a .so
on Unix when used by the linker) is used by the linker
in order to resolve external references of the object code
it tries to link. The code and the references to 'foo' in a .dll/.so
will remain the same.
| Quote: |
P.S.
I need to redefine global operator new & operator delete
so as to _all_ (.cpp, .lib, .dll) libraries included in my project
use my version of global new & delete
|
It seems that you need to change the sources and recompile
all your libraries in order to use these new operators:
the compiler constructs these operators for every class you define.
Another possibility is to change the memory management functions
that the new and delete operators use, but then your ability to change
these operators will be less significant.
You can see what is done by the standard memory leak detectors,
like Purify and MFC/MSVC. I think both recompile the code
and both change the new/delete for better memory tracing.
Michael
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Turner Guest
|
Posted: Thu Oct 23, 2003 6:47 pm Post subject: Re: linkage |
|
|
Hi
"Kuznetsov Dmitriy" <dmitriy9 (AT) mail (DOT) ru> wrote
| Quote: | There are two definitions of the same function in a project
(in separate .cpp files):
void foo () { return; } // for example
There are declaration & call of the function 'foo' in third .cpp
file of the project.
Can anybody explain to me what will be linker do (must do)
in this situation?
|
Not really C++-specific, but useful to know: The linker will almost always
complain, and cowardly refuse to select one definition over the other. This
is sensible behaviour, isn't it? There is one exception, however...
| Quote: | P.S.
I need to redefine global operator new & operator delete
so as to _all_ (.cpp, .lib, .dll) libraries included in my project
use my version of global new & delete
|
Since you're apparently using the Microsoft platform, I can't think of a
good way to do this. Now, if you were using a platform where the linker is
ld, you could do this:
LD_PRELOAD=my_new_replacement.so /usr/bin/my_app
Regards
David Turner
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|