linkage

Discussion in 'C++' started by Kuznetsov Dmitriy, Oct 22, 2003.

  1. ÷ ÐÒÏÅËÔÅ × Ä×ÕÈ ÒÁÚÎÙÈ .cpp ÆÁÊÌÁÈ ÏÐÒÅÄÅÌÅÎÙ ÏÄÉÎÁËÏ×ÙÅ
    ÆÕÎËÃÉÉ, ÎÁÐÒÉÍÅÒ:

    void foo () { return; }

    ÷ ÔÒÅÔØÅÍ .cpp ÆÁÊÌÅ ÅÓÔØ ÏÂßÑ×ÌÅÎÉÅ É ×ÙÚÏ× 'foo'

    ËÔÏ-ÎÉÂÕÄØ ÚÎÁÅÔ, ÓÕÝÅÓÔ×ÕÅÔ ÌÉ ÔÅÏÒÉÑ, ÏÐÒÅÄÅÌÑÀÝÁÑ ÐÏ×ÅÄÅÎÉÅ
    ÌÉÎËÏ×ÝÉËÁ × ÔÁËÉÈ ÓÌÕÞÁÑÈ ?

    ÞÔÏ ÂÕÄÅÔ ÅÓÌÉ
    - ÄÏÐÏÌÎÉÔÅÌØÎÙÅ ÏÐÒÅÄÅÌÅÎÉÑ ÆÕÎËÃÉÉ ÓÏÄÅÒÖÁÔØÓÑ × .lib (.dll),
    ÐÏÄËÌÀÞÅÎÎÏÊ Ë ÐÒÏÅËÔÕ ?
    - ×ÙÚÏ×Ù ÆÕÎËÃÉÉ ÓÏÄÅÒÖÁÔØÓÑ × .lib (.dll),
    ÐÏÄËÌÀÞÅÎÎÏÊ Ë ÐÒÏÅËÔÕ ?


    ÐÒÁËÔÉÞÅÓËÁÑ ÓÔÏÒÏÎÁ: ÎÅÏÂÈÏÄÉÍÏ ÐÅÒÅÏÐÒÅÄÅÌÉÔØ
    ÇÌÏÂÁÌØÎÙÊ new & delete, ÔÁË ÞÔÏÂÙ ×ÓÅ ÂÉÂÌÉÏÔÅËÉ, ÉÓÐÏÌØÚÕÅÍÙÅ
    × ÐÒÏÅËÔÅ (ÓÏÄÅÒÖÁÝÉÅÓÑ × .cpp, .lib, .dll)
    ×ÙÚÙ×ÁÌÉ ÐÅÒÅÏÐÒÅÄÅÌÅÎÎÙÅ new & delete
    Kuznetsov Dmitriy, Oct 22, 2003
    #1
    1. Advertising

  2. Kuznetsov Dmitriy

    shura Guest

    > ÐÒÁËÔÉÞÅÓËÁÑ ÓÔÏÒÏÎÁ: ÎÅÏÂÈÏÄÉÍÏ ÐÅÒÅÏÐÒÅÄÅÌÉÔØ
    > ÇÌÏÂÁÌØÎÙÊ new & delete, ÔÁË ÞÔÏÂÙ ×ÓÅ ÂÉÂÌÉÏÔÅËÉ, ÉÓÐÏÌØÚÕÅÍÙÅ
    > × ÐÒÏÅËÔÅ (ÓÏÄÅÒÖÁÝÉÅÓÑ × .cpp, .lib, .dll)
    > ×ÙÚÙ×ÁÌÉ ÐÅÒÅÏÐÒÅÄÅÌÅÎÎÙÅ new & delete


    äÌÑ ÜÔÉÈ ÃÅÌÅÊ ÅÓÔØ

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

    void __cdecl operator delete ( void* p )
    {
    ....
    }
    shura, Oct 22, 2003
    #2
    1. Advertising

  3. [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"
    <> 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
    Bob Hairgrove, Oct 22, 2003
    #3
  4. 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
    Bob Hairgrove, Oct 22, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sönke Greve

    TabStrip Control with direct linkage?

    Sönke Greve, Jan 22, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    337
    Sönke Greve
    Jan 22, 2006
  2. Thomas Dorris

    JNI linkage issue

    Thomas Dorris, Jan 31, 2004, in forum: Java
    Replies:
    2
    Views:
    3,051
    Thomas Dorris
    Feb 2, 2004
  3. Mary
    Replies:
    1
    Views:
    3,978
    Thomas Fritsch
    Oct 11, 2004
  4. Don@NoSpam

    Devious linkage to enter Web site

    Don@NoSpam, Jul 5, 2004, in forum: HTML
    Replies:
    3
    Views:
    461
    brucie
    Jul 5, 2004
  5. Replies:
    1
    Views:
    580
    Michael DOUBEZ
    Sep 12, 2008
Loading...

Share This Page