Weird C Compiler receiving error message "ld: duplicate symbol _main"

I just started learning C and wrote my cute world program:

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

When I run the code, I get a very 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

Do I need to reinstall DevTools?

+5
source share
4 answers

If you read error messages (in particular, a line starting with ld: duplicate symbol _main in ...), you will notice that it complains about two functions main, one of which:

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

and the other in:

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

This means that your project is somehow confused. Either you have two separate source files containing main, or Xcode provides automatic delivery.

You just need to fix it.

+14
source

, :

Apple Mach-O Linker (id)

Ld//...
    cd...
    setenv...
    //...

, Xcode . .

ld: _main /Users/.../helloworld.o /Users/.../main.o x86_64

. , _main, helloworld.o main.o. , , main, . helloworld.c, - main.c. , .

//usr/bin/clang 1

Xcode. , , , 1 .

+12

. " " , . , . .

enter image description here

+10

, , #include "...filename...", . .

, #include , #include.

+3

All Articles