Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 904
Billjk Avatar asked Mar 27 '12 01:03

Billjk


3 Answers

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.

like image 95
paxdiablo Avatar answered Oct 04 '22 20:10

paxdiablo


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.

like image 32
ughoavgfhw Avatar answered Oct 04 '22 22:10

ughoavgfhw


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

like image 24
Phil Avatar answered Oct 04 '22 21:10

Phil