LNK2005: Object Already Defined. Help?

This is a discussion on LNK2005: Object Already Defined. Help? within the C++ Programming forums, part of the General Programming Boards category; So, I'm trying to build this program that has two source files and one include file. Both source files include ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    LNK2005: Object Already Defined. Help?

    So, I'm trying to build this program that has two source files and one include file. Both source files include the single include file, I know that's why I'm getting the errors. I also know that the supposed solution is to do something like:

    Code:
    #ifndef _SOMETHING_
    #define _SOMETHING_
    
    //All sorts of whatever code here
    #endif
    My problem is this. I did that, and I still get the errors, I have that kind of block around the entire include file.

    What I really want to know, is why this is needed, because I only get these kinds of errors in certain include files. Does it have to do with having functions defined (not just declared) in the include file? Does it have to do with having static variables or functions? If it's one of those two reasons, should I only have the #ifndef, #define, #endif block around the declared functions and/or static variables? Or should I have it around the entire file?

    Thanks for the help.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by maxhavoc
    Does it have to do with having functions defined (not just declared) in the include file?
    Yup, most likely. Define functions and data in a separate module if each is being separately compiled (with an exception for template stuff); have only declarations in a header. The header guards avoid compile errors, but you are seeing linker errors.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    I don't understand though, I have another program where I have a class fully declared (including its member fuctions) and a few other inline functions fully declared within a header file that is used by multiple source files and I get no error there, even without the #ifndef, #define, #endif stuff.

    What causes that linker error?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Do you #inlcude the header in more than one source file? If so, each file will contain its own copy of the code. And the linker will find multiple instances of the same thing and complain.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Include guards don't help against linker errors. You simply must not define variables in headers. Same goes for functions ( unless declared inline ).
    If you want to declare variables in headers use the extern keyword and define them in a single source file.
    The problem here is that these variables wil be created multiple times in different tranlation units. The linker then sees multiple versions of the same symbol and gives up.
    Kurt

    EDIT: I'm a bit slow today

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Yes I do, and in the other program I get no errors.

    I've discovered something. If I declare the offending variables and functions static, then I don't get the linker error for those objects.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by maxhavoc
    Yes I do, and in the other program I get no errors.
    Does the other program consist of only a single source file? Well then, 1 instance is nothing to complain about.

    Please take our advice and learn to do it right.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Quote Originally Posted by ZuK
    Include guards don't help against linker errors. You simply must not define variables in headers. Same goes for functions ( unless declared inline ).
    If you want to declare variables in headers use the extern keyword and define them in a single source file.
    The problem here is that these variables wil be created multiple times in different tranlation units. The linker then sees multiple versions of the same symbol and gives up.
    Kurt

    EDIT: I'm a bit slow today

    Interesting, can you possibly explain why declaring them inline and/or static fixes the error?

    Well, for static I can understand, that makes it so the object remains in memory once it's declared, and so a second declaration has no meaning right? But why does inline work?

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Quote Originally Posted by Dave_Sinkula
    Does the other program consist of only a single source file? Well then, 1 instance is nothing to complain about.

    Please take our advice and learn to do it right.
    I'm not trying to argue with you here, I want to learn why the error happens.

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by maxhavoc
    Yes I do, and in the other program I get no errors.

    I've discovered something. If I declare the offending variables and functions static, then I don't get the linker error for those objects.
    Yes thats right. If you declare them static they are defined at file scope. But then they are different variables, If you change one the other does not. Will give you hard to find bugs.
    Kurt

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    An #include is like copy-and-paste. So if you have multiple modules with the same functions defined, the linker may see something like this (definitely pseudocode).
    Code:
    // header
    int foo(int x)
    {
       return x * x;
    }
    After each module is compiled...
    Code:
    int foo(int x) // module #1
    {
       return x * x;
    }
    int foo(int x) // module #2
    {
       return x * x;
    }
    int foo(int x) // module #3
    {
       return x * x;
    }
    One for each module in which the header was #included.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Ok, thank you both for the help. I appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird Namespace error..
    By Nebbuchadnezzar in forum C++ Programming
    Replies: 10
    Last Post: 05-13-2006, 12:53 AM
  2. Linker Errors
    By mhandlon in forum Windows Programming
    Replies: 1
    Last Post: 03-20-2006, 08:53 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Variables already defined while linking.
    By xconspirisist in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2005, 06:20 AM
  5. Linked List Templates and Object Types
    By ventolin in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2004, 01:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21