Weird C Compiler, getting an error "ld: duplicate symbol _main"

40,048

Solution 1

If you read the error messages (specifically the line starting ld: duplicate symbol _main in ...), you'll notice that it's complaining about two main functions, one in:

......blah blah blah/helloworld.o

and the other in:

......yada yada yada/main.o

That means your project is screwed up somehow. Either you have two separate source files containing main or Xcode is supplying one automagically.

You just need to fix that.

Solution 2

Here's how to interpret that message:

Apple Mach-O Linker (id) Error

An error occurred

Ld /Users/ …
cd …
setenv …
/Developer/…

This is the command that Xcode executed to perform the linking step. You can almost always ignore it and skip past the next blank line.

ld: duplicate symbol _main in /Users/…/helloworld.o and /Users/…/main.o for architecture x86_64

This is the actual error message. It tells you that you have duplicate _main symbols, one in the file helloworld.o and one in main.o. This means you have to functions which are both called main, which isn't allowed. One of them is in helloworld.c and the other is in main.c. If you delete one of these functions or files, the error will go away.

Command /Developer/usr/bin/clang failed with exit code 1

This tells you the exit code of the command Xcode performed. It is less helpful than the error message, and I have never seen anything other than 1 for linking errors.

Solution 3

I meet this problem as well. In "Target Membership", just tick the file you want to run. Untick this in other files you don't want to run. Then try again.

enter image description here

Solution 4

It is also important to remember that you could have received this error message if you had a #include "...filename..." that created a duplicate copy of your function calls. However, in your case, that is not likely.

remember that #include essentially just copies and pastes a copy of your code where the #include takes place.

Share:
40,048
Billjk
Author by

Billjk

please delete me

Updated on November 13, 2020

Comments

  • Billjk
    Billjk over 3 years

    I just started learning C, and wrote my hello world program:

    #include <stdio.h>
    main()
    {
        printf("Hello World");
        return 0;
    }
    

    When I run the code, I get a really long error:

    Apple Mach-O Linker (id) Error
    
     Ld /Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Products/Debug/CProj normal x86_64
            cd /Users/Solomon/Desktop/C/CProj
            setenv MACOSX_DEPLOYMENT_TARGET 10.7
            /Developer/usr/bin/clang -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.7.sdk -L/Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Products/Debug -F/Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Products/Debug -filelist /Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Intermediates/CProj.build/Debug/CProj.build/Objects-normal/x86_64/CProj.LinkFileList -mmacosx-version-min=10.7 -o /Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Products/Debug/CProj
    
        ld: duplicate symbol _main in /Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Intermediates/CProj.build/Debug/CProj.build/Objects-normal/x86_64/helloworld.o and /Users/Solomon/Library/Developer/Xcode/DerivedData/CProj-cwosspupvengheeaapmkrhxbxjvk/Build/Intermediates/CProj.build/Debug/CProj.build/Objects-normal/x86_64/main.o for architecture x86_64
        Command /Developer/usr/bin/clang failed with exit code 1
    

    I am running xCode

    Should I reinstall DevTools?