cxcodemacos

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?


Solution

  • 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.