Thread: LNK2005 object already defined error (Not a library conflict)

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

    LNK2005 object already defined error (Not a library conflict)

    Greetings, I'm getting an error in my code where I include a header file in more than one location and I get the LNK2005 error during linking. I know that in C a code block is needed around the header file so that it is only included once but I forgot what that code block looks like (I'm used to C++ where this problem doesn't occur).

    Isn't it something like?
    Code:
    #ifndef HEADER
    #define HEADER
    //header code
    //more header code
    #ifdef HEADER
    #endif HEADER
    Am I close? It's been a while

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #ifndef HEADER_H
    #define HEADER_H
    //header code
    
    
    
    #endif //HEADER_H
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Hmm, I tried that and I'm still getting the errors. Is there anything else I need?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Header guards don't protect against linker errors.
    Guess you defined some variable or function in the header. Headers should only contain declarations.
    Kurt

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    post your header
    probably you define some variable or function in the header so linker has more than one instance of the same global object
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    You're right, it is because I declare variables and functions inside the header file. Is there anyway around that linker error or do I have to remove the functions and variables?

    Yes I know it's bad coding practice to have functions in headers. I just want to know if it is possible.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should move them to the c-file and leave only function declarations and externs for variables in the header
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Quote Originally Posted by vart
    you should move them to the c-file and leave only function declarations and externs for variables in the header
    Right, I know that's what I *should* do, I was wondering if I had any other options.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not really.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Sigh, I hate C with a passion. Alright, thanks for the help.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by maxhavoc
    Right, I know that's what I *should* do, I was wondering if I had any other options.
    I'm not trying to add insult to injury, but that's why folks should follow the shoulds from the get-go. Most often, the advice that we try to tuck away when we're new is exactly the stuff we should do immediately because we're new. Old timers merely try to prevent other folks from going through the same screwup cycle that we may have gone through.
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Library Design
    By CornedBee in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2004, 10:40 AM
  2. Linked List Templates and Object Types
    By ventolin in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2004, 12:05 PM
  3. How would you do this (object interlinking)
    By darksaidin in forum C++ Programming
    Replies: 7
    Last Post: 08-30-2003, 12:08 AM
  4. Accessing the C++ Standard Library locale object
    By Davros in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 10:18 AM
  5. Errors in passing a function my defined object
    By Dual-Catfish in forum C++ Programming
    Replies: 6
    Last Post: 02-22-2002, 01:36 PM