-1

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?

Billjk
  • 10,387
  • 23
  • 54
  • 73

2 Answers2

5

have you tried

int main() or even int main(int argc, char**argv) ?

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • 2
    The correct declarations are `int main(void)` and `int main(int argc, char *argv[])` (or equivalent), but it's unlikely that declaring it as `main()` (which was correct in early versions of C, and likely still accepted by most compilers) would cause that particular error. – Keith Thompson Mar 27 '12 at 03:30
2

The error message indicates that the symbol _main (which refers to the main() function) is defined twice, once in helloworld.o and once in main.o.

It's likely that you have two source files, helloworld.c and main.c, in the same project, and that both define a function named main.

You can only have one main function in a program. Removing one of those two source files (and the associated object file) from your Xcode project should fix the problem. (I haven't used Xcode myself; perhaps someone else can tell you how to do that, if it's not obvious.)

(And the correct definition is int main(void), not the old-style main(), but I don't think that's related to the symptom you're seeing.)

There are a couple of stackoverflow questions that could be closely related to yours:

Community
  • 1
  • 1
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631