473,472 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

linkage

В проекте в двух разных .cpp файлах определены одинаковые
функции, например:

void foo () { return; }

В третьем .cpp файле есть объявление и вызов 'foo'

кто-нибудь знает, существует ли теория, определяющая поведение
линковщика в таких случаях ?

что будет если
- дополнительные определения функции содержаться в .lib (.dll),
подключенной к проекту ?
- вызовы функции содержаться в .lib (.dll),
подключенной к проекту ?
практическая сторона: необходимо переопределить
глобальный new & delete, так чтобы все библиотеки, используемые
в проекте (содержащиеся в .cpp, .lib, .dll)
вызывали переопределенные new & delete

Jul 19 '05 #1
3 5031
> практическая сторона: необходимо переопределить
глобальный new & delete, так чтобы все библиотеки, используемые
в проекте (содержащиеся в .cpp, .lib, .dll)
вызывали переопределенные new & delete


Для этих целей есть

void* __cdecl operator new ( size_t nSize )
{
....
}

void __cdecl operator delete ( void* p )
{
....
}
Jul 19 '05 #2
[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
Jul 19 '05 #3
On Wed, 22 Oct 2003 19:40:38 GMT, wouldnt_you_like@to_know.com (Bob
Hairgrove) wrote:
(b) Link everything statically with the same static memory-management
DLL;


Of course, this should read:

(b) Link everything statically with the same static memory-management
*library*...(DLLs are never static)
--
Bob Hairgrove
No**********@Home.com
Jul 19 '05 #4

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

Similar topics

9
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. //...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
20
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
3
by: al.cpwn | last post by:
do static and inline functions or members have internal linkage? I have been reading this newsgroup on google and found conflicting ideas. Can someone please help me understand why in some places...
13
by: fctk | last post by:
source: http://rm-f.net/~orange/devel/specifications/c89-draft.html#3.1.2.2 there are two passages in this paragraph i can't fully understand: 1) "If the declaration of an identifier for an...
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
12
by: Taras_96 | last post by:
Hi everyone, AFAIK external linkage allows you to refer to variables/functions outside of the current translation unit. A variable in an unnamed namespace is similar to declaring a static...
1
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
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,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.