Thread: Linking error in Xcode.

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    40

    Linking error in Xcode.

    Hi all,

    You were so responsive and helpful to my last query, I've decided to come again.

    I'm having a problem during linking, I get the following error:

    ld: duplicate symbol _amino_acid_tlc in /Users/davis/Documents/covarion simulation/covarions/build/covarions.build/Debug/covarions.build/Objects-normal/x86_64/structures.o and /Users/davis/Documents/covarion simulation/covarions/build/covarions.build/Debug/covarions.build/Objects-normal/x86_64/main.o

    Both main.c and structures.c have structures.h as one of their include files- they are required in both files for successful compilation.

    My structures.h file contains

    Code:
    #ifndef structures_h
    #define structures_h
    ....
    #endif

    where the .... represents the everything else that is contained in the structures.h file.

    My understanding was that this should prevent exactly this sort of error, because it will see that structures_h is defined, and then not include any part of the structures.h file, once it's actually been defined.

    amino_acid_tlc is defined only once in all of my files, and if I remove all references to amino_acid_tlc from within the main.c file (but otherwise leave everything else exactly the same), the program compiles and links just fine.

    Code:
    const char *amino_acid_tlc[22]= {"ala", "cys", "asp", "glu", "phe", "gly", "his", "lle", "lys", "leu", "met", "asn", "pyl", "pro", "gln", "arg", "ser", "thr", "sec","val", "trp", "tyr"};
    Note, that if I change that too

    Code:
    static const char *amino_acid_tlc[22]= {"ala", "cys", "asp", "glu", "phe", "gly", "his", "lle", "lys", "leu", "met", "asn", "pyl", "pro", "gln", "arg", "ser", "thr", "sec","val", "trp", "tyr"};
    it compiles just fine, but that doesn't explain why it is still unhappy, even after adding the #ifndef statements at the start of structures.h. I've even tried using #pragma once, and that didn't help either. Using the static keyword is just fine for my purposes, but I'd like to understand what the issue is with #ifindef or #pragma.

    Please note I'm using Xcode with gcc 4.2.1.

    Any suggestions / comments?
    Thanks!
    Brad
    Last edited by bhdavis1978; 03-19-2010 at 11:25 AM. Reason: Updating some stuff

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Headers typically do not have definitions in them. Lookup "definition vs. declaration".

    In this case, the header should have a declaration: extern const char *amino_acid_tlc[];
    And a single source file would have the definition.

    Making the definition static in the header caused each translation unit to have its own private copy of the array - thus no name collisions at link time.

    gg

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    40
    Hi codeplug,

    Thanks a lot for the help, and pointing out where I would've had problems w.r.t declarations vs definitions, I appreciate it. I imagine those sorts of bugs would be difficult to track down if I didn't know the difference.

    Cheers,
    Brad

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 04-20-2008, 01:15 PM
  2. Problems linking with g++
    By Just in forum Linux Programming
    Replies: 11
    Last Post: 07-24-2006, 01:35 AM
  3. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 10:51 PM
  4. Grrr.... SDL Linking Problem
    By 7EVEN in forum Game Programming
    Replies: 5
    Last Post: 08-12-2005, 08:44 PM
  5. Linking error with DirectDrawCreateEx
    By jjj93421 in forum Game Programming
    Replies: 6
    Last Post: 04-06-2004, 03:57 PM