CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: error LNK2005:

  1. #1
    Join Date
    May 2005
    Posts
    68

    error LNK2005:

    Hello,

    In my Dialog based project, I am using CCriticialSection objets in one class, CSerial(Serial.cpp & Serial.h). And this class members are called in ADlg(main Dialog box file).

    When I am building this file, it gives this kind of linker errors.

    Code:
    Linking...
    AP2.obj : error LNK2005: "class CCriticalSection _critWrite" (?_critWrite@@3VCCriticalSection@@A) already defined in AP1.obj
    AP2.obj : error LNK2005: "class CCriticalSection _critRead" (?_critRead@@3VCCriticalSection@@A) already defined in AP1.obj
    Agx3313P1.obj : error LNK2005: "class CCriticalSection _critWrite" (?_critWrite@@3VCCriticalSection@@A) already defined in Agx3281P1.obj
    Agx3313P1.obj : error LNK2005: "class CCriticalSection _critRead" (?_critRead@@3VCCriticalSection@@A) already defined in Agx3281P1.obj
    Agx3313P2.obj : error LNK2005: "class CCriticalSection _critWrite" (?_critWrite@@3VCCriticalSection@@A) already defined in Agx3281P1.obj
    Agx3313P2.obj : error LNK2005: "class CCriticalSection _critRead" (?_critRead@@3VCCriticalSection@@A) already defined in Agx3281P1.obj
    Argus.obj : error LNK2005: "class CCriticalSection _critWrite" (?_critWrite@@3VCCriticalSection@@A) already defined in Agx3281P1.obj
    Argus.obj : error LNK2005: "class CCriticalSection _critRead" (?_critRead@@3VCCriticalSection@@A) already defined in Agx3281P1.obj
    ArgusDlg.obj : error LNK2005: "class CCriticalSection _critWrite" (?_critWrite@@3VCCriticalSection@@A) already defined in Agx3281P1.obj
    ArgusDlg.obj : error LNK2005: "class CCriticalSection _critRead" (?_critRead@@3VCCriticalSection@@A) already defined in Agx3281P1.obj
    Serial.obj : error LNK2005: "class CCriticalSection _critWrite" (?_critWrite@@3VCCriticalSection@@A) already defined in Agx3281P1.obj
    Serial.obj : error LNK2005: "class CCriticalSection _critRead" (?_critRead@@3VCCriticalSection@@A) already defined in Agx3281P1.obj
    Debug/Argus.exe : fatal error LNK1169: one or more multiply defined symbols found
    
    Build log was saved at "file://d:\users\jk\NetProjects\Vcnet\Argus\Argus\Debug\BuildLog.htm"
    Argus - 13 error(s), 0 warning(s)
    Kindly Suggest,
    Regards,

    JK


  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: error LNK2005:

    LNK2005...

  3. #3
    Join Date
    Jun 2004
    Posts
    1,352

    Re: error LNK2005:

    .. DARN you beat me to it. .. but here it goes anyway



    symbol already defined in object
    The given symbol, displayed in its decorated form, was multiply defined.
    Tips
    One of the following may be a cause:
    · The most common cause of this error is accidentally linking with both the single-threaded and multithreaded libraries. Ensure that the application project file includes only the appropriate libraries and that any third-party libraries have appropriately created single-threaded or multithreaded versions.
    · The given symbol was a packaged function (created by compiling with /Gy) and was included in more than one file but was changed between compilations. Recompile all files that include the symbol.
    · The given symbol was defined differently in two member objects in different libraries, and both member objects were used.
    · An absolute was defined twice, with a different value in each definition.

    This error is followed by fatal error LNK1169.


    You probobly have a symbol "..CCritical..." defined differently in different modules.

    Try rebuilding you're whole project, then checking if each instance of CCrit... is defined the same in each module.
    Rate this post if it helped you.

  4. #4
    Join Date
    May 2005
    Posts
    68

    Re: error LNK2005:

    Hi Andreas,

    As mentioned in the link you have sent, I just changed the CCriticalSection objects in Serial.h to static CCriticalSection objects and rebuild - all errors gone!

    Can you give me a brief - why is it so?

    Any way, thanks a lot for the link you given!
    Regards,

    JK


  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: error LNK2005:

    The reason is simply that the header file containing the critical section gets included by more than one file.

    Since the compiler translates each source file on its own...in a so-called translation unit, preprocessor directives such as inclusion guards (that are supposed to prevent multiplu defined symbols) are not stored across different translation units, thus, it won't prevent multiple inclusions of the file in different units. Inclusion guards only prevent the inclusion of the same header more than once in the same compilation unit.

    By addind the 'static' keyword, you basically created several static variables of the critical section, each one local to one translation unit. I am not sure whether this is what you actually want. However, without knowing what you are trying to accomplish, it is hard to tell...

  6. #6
    Join Date
    May 2005
    Posts
    68

    Re: error LNK2005:

    Hello Andreas,

    Thank you for the explaination.

    Actually I wanted to write mutithreaded functions for handling serial port operations, read & write. So, in a class named CSerial, I wanted to define 2 variables of CCriticalSection namely, _critRead & _critWrite - to handle serial port read & serial port write operations seperately.

    Even before initializing these critical section variables as static, I was defining this header file(Serial.h) only once in main dialog box program(CArgusDlg.h) - not in other programs.
    Regards,

    JK


  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: error LNK2005:

    Well...and how many files do include the header 'CArgusDlg.h'?

  8. #8
    Join Date
    May 2005
    Posts
    68

    Re: error LNK2005:

    Main Dialog box in my projects consists of 4 property pages + some controls. Depending on the tests I perform listed in these property pages, the control should go back to Main Dialog box.

    So, I added the main dialog box header file(CArgusDlg.h) in all 4 property pages header files. And I am calling main dialog box from property pages like this,

    Code:
    CArgusDlg* adlg = (CArgusDlg*)AfxGetApp()->GetMainWnd();
    // CArgusDlg* adlg = (CArgusDlg*)AfxGetMainWnd(); 
     
    adlg->EnableCombo(TRUE);
     
    return FALSE;
    You mean including this ArgusDlg.h in other files - is creating problems?
    Regards,

    JK


  9. #9
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: error LNK2005:

    If you

    #include "ArgusDlg.h"

    in a file, then you are including all files included in ArgusDlg.h in that file as well.
    You are also including all files included in files included in ArgusDlg.h etc...

    So you see that you are including the file containing the CCriticalSection more than once.


    This does not address your design issue however.
    Most likely a class that is going to synchronize access to a serial port should be implelmented as a singleton and your critical section should just be a member of this class.
    I am assuming that you had it as a global variable to get the linking problem.
    Making it static is not the solution.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: error LNK2005:

    Quote Originally Posted by chakrijk
    You mean including this ArgusDlg.h in other files - is creating problems?
    Yes...as souldog already pointed out...

    As I understand it, you want to synchronize the access of the serial port. Thus, the best location for placing this synchronization is simply at the closest point...in your serial communication class. The code snippet you posted shows something different, thus, I am not sure what the relation between the property pages and the serial port is...

  11. #11
    Join Date
    Aug 2010
    Posts
    1

    Re: error LNK2005:

    Just had this problem about some functions in a template class where the class was declared and defined in one header file. Simple fix was to include the header in main.cpp instead of stdafx.h
    ? works

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured