13

I'm very very new to C and C++ programming, and have very little experience in Software Programming (my background is Web Based) But I'm trying to experiment with C / C++ and Xcode... So I've found this code (and many similar variations online):

#include <stdio.h>

int main()
{
    printf ("Test");
    return 0;
}

Yet when I come to compile it in Xcode I get the following error:

> duplicate symbol _main in:
>     /Users/thomas/Library/Developer/Xcode/DerivedData/test-etqojvxbxhxjqeggdzkbfufvbeza/Build/Intermediates/test.build/Debug/test.build/Objects-normal/x86_64/first.o
>     /Users/thomas/Library/Developer/Xcode/DerivedData/test-etqojvxbxhxjqeggdzkbfufvbeza/Build/Intermediates/test.build/Debug/test.build/Objects-normal/x86_64/main.o
> ld: 1 duplicate symbol for architecture x86_64 clang: error: linker
> command failed with exit code 1 (use -v to see invocation)

Maybe Xcode is the wrong thing for me to be using as a newbie? If anyone could recommend a better compiler, that would be great too!

chrk
  • 4,037
  • 2
  • 39
  • 47
Thomas Fearn
  • 387
  • 1
  • 3
  • 9
  • I'm not into Macs so I can't help more but it's something wrong with your environment, not the code. You should try with GCC. – python Sep 19 '14 at 23:41
  • 2
    By chance, is there a `main` function in both `main.c` and `first.c`? Or how are you compiling them? You seem to be linking two object files together. – user703016 Sep 19 '14 at 23:42

2 Answers2

15

When you create a new project in Xcode, it automatically gives you a starting file with main() in it. If you created a new file, such as first.c, and then pasted your test code into it, you'll be defining main() twice, and getting that error.

You need to either delete the file (such as main.c, or main.m) that Xcode provides in your new project, or cut and paste your sample code into that file, instead of creating a new one.

Crowman
  • 25,242
  • 5
  • 48
  • 56
0

In my case I had created another .c file and Xcode was seeing this as another main file and hence created the error -> duplicate symbol for architecture x86_64

Select the second .c file -> Show the File Inspector -> and remove the target membership on the second .c file in your project to get rid of this error.

enter image description here

@Thomas Fearn - you will need to remove the target membership on the first.c file.

uplearned.com
  • 3,393
  • 5
  • 44
  • 59
  • This works, but it is commonly not the correct approach. Please don't work against the tool. -- A project has just one `main()`. If you want another application, create a new project. – the busybee Jul 27 '20 at 14:31
  • Though for the organization of code and rather than having a huge monolithic page of code, what do you suggest if your trying to have a struct in another file @the busybee? – uplearned.com Jul 27 '20 at 20:47
  • Please post this as a question. From the single sentence I don't understand exactly what you like to know. -- You can have as many files in a project as you see fit. But only one `main()` in all of them. This is kind of a real problem with all *automatic* project management systems in IDE. That's why I prefer hand-written makefiles and use an IDE as a luxurious editor. – the busybee Jul 28 '20 at 05:52