[Scroll down for an English translation and answer...]
Привет Дмитрий,
Извините, пожалуйста, что я так плохо говорю по-русски. Но здесь
международный форум "comp.lang.c++". Значит, к сожалению, что написать
по-английски у нас обязательно. Лучше всего было бы, чтобы получить
ответ по-русски, также попросить к форуму "relcom.comp.lang.c-c++" или
"relcom.fido.su.c++" или другам ... многие таких форумов есть.
[Ответ дальше внизу...]
On Wed, 22 Oct 2003 15:30:55 +0400, "Kuznetsov Dmitriy"
<dk*@garant.ru> wrote:
В проекте в двух разных .cpp файлах определены одинаковые
функции, например:
void foo () { return; }
В третьем .cpp файле есть объявление и вызов 'foo'
кто-нибудь знает, существует ли теория, определяющая поведение
линковщика в таких случаях ?
что будет если
- дополнительные определения функции содержаться в .lib (.dll),
подключенной к проекту ?
- вызовы функции содержаться в .lib (.dll),
подключенной к проекту ?
практическая сторона: необходимо переопределить
глобальный new & delete, так чтобы все библиотеки, используемые
в проекте (содержащиеся в .cpp, .lib, .dll)
вызывали переопределенные new & delete
==============
First, a rough translation of the question which was posed in Russian
[use a Russian font with KOI-8 code page to view properly]:
"Let's assume that there are two different .cpp files within the same
project, each with different implementations of the same function, for
example:
void foo() { return; }
"In a third file, there is also a declaration of the function "foo".
"Can anyone tell me how the linker is supposed to behave in such
situations?
"What happens:
- if there are additional implementations of such functions in a .lib
(.dll) which is included in the project?
- if the .lib (.dll) contains declarations of the functions?
"The practical situation is that it is necessary to overload global
new and delete so that all libraries included in the project use the
[same] overloaded new and delete."
==============
Difficult to answer with my bad Russian, but I'll try in English:
If the linker cannot resolve the different functions, you receive a
linker error. Be very grateful in that case! Problems arise when new
and delete are implemented differently in the library than in the
other modules, and you do "new" in one module, but "delete" in
another. This is especially true when some modules are using static
libraries and others not.
There are some ways to avoid such problems:
(a) Implement the functions (here: new and delete) in a DLL and make
sure that all modules use dynamic runtime linking to use the same DLL;
(b) Link everything statically with the same static memory-management
DLL;
(c) Always do "delete" in the same module where "new" is called. If
you must have different implementations, this is the only way to do it
AFAIK.
For example, you want to (or must) allocate memory in a DLL and return
a pointer to that memory to the calling function in another module.
Implement a "FreeMemory" function in the DLL which deletes the memory
instead of having the caller do it by calling "delete". The
implementation of "FreeMemory" is trivial -- you just call "delete"
(or "delete[]", as the case may be) but you are then sure that the
memory will be deallocated properly.
--
Bob Hairgrove
No**********@Home.com