Xcode Showing Build Failed

I have 2 c files in a group folder(created using Xcode), before that the individual c file in the folder was being built properly and output was shown but after adding a second file, it shows build failed

I built the files using gcc in terminal and they are being built and the output is being shown so I suspect that this is a problem with running the file.

The linker error says "duplicate symbols".

Did you build the files successfully individually before? You almost certainly have defined the same symbols (function, global variable, etc.) more than once across the two files. Since they are now compiled in one executable instead of two, you need to have unique names.

Or compile the two separately, in one target each.

The most likely cause of your problem is you are trying to place and run multiple small programs in one Xcode project. Each program has its own main function, but a C program can have only one main function. If you add three files to your project, intending each file to be its own program, and try to run the project, Xcode doesn't know you want to run only one of the files. It assumes you want to run all the files, sees you have three main functions, and generates a link error.

Xcode is designed to have one program per project, no matter how small the program is. If you are learning C and writing multiple small programs, you have the following options:

  • Create one Xcode project for each program.
  • Create one Xcode project and add a target for each program. Use the jump bar in the Xcode toolbar above the code editor to choose the target to run.
  • Create one Xcode project and use the target membership checkboxes on the right side of the project window to tell Xcode which files/programs you want to build and run.
  • Use a text editor like VS Code, TextMate, or BBEdit to write your code instead of Xcode. I know TextMate comes with a bundle to run your programs from inside the editor. VS Code probably has an extension that allows this as well.
Xcode Showing Build Failed
 
 
Q