I am trying to use XCode as my training environment for simple C++ programs. If I create a new project, I can successfully complete everything (compile, run) but If I add one more file, it can't compile as a separate one. That means that I need to have a main2() and a main3() etc. because having 2 files with two main() in the same folder lead to an error.

In simple words, I want to write code for 30 C++ examples, each one in its own .cpp file and compile every one of them separately and not as a whole project (all files together that If there are two functions of the same name, it will crash).

  • 1
    You would need to create separate target for each file. Here's more info on Apple Pages: developer.apple.com/library/mac/recipes/… Commented Feb 24, 2015 at 13:00
  • I don't believe it would do because I need to setup a new schema for each file. We're talking about helloworld.cpp, factorial.cpp etc. - totally different programs from a book.
    – Radolino
    Commented Feb 24, 2015 at 13:03
  • Uh, You have to wait a bit until I get more screenshots. You have to make separate targets in order to be able to compile separate programs. Commented Feb 24, 2015 at 13:18
  • I don't want to compile 30 programs each time I make a letter change in one of them. In case that XCode supports this type of use.
    – Radolino
    Commented Feb 24, 2015 at 13:35
  • See my answer. You'll compile only one file, just change the target You'd like to compile. Commented Feb 24, 2015 at 13:39

2 Answers 2


In order to compile multiple separate files (separate binaries as a result) You would need to create separate targets for every file.

To add new target please choose: File -> New -> Target.., select for example Command Line Tool. Make sure it's properly added to Your project. Here's example: enter image description here

My main.cpp is added to target multiple_targets like this: enter image description here

My main-kopia.cpp file is added to target2 like this: enter image description here

to be sure check the build phases for every target, here's example of my target2 Build Phase: enter image description here

With this I am able to compile both main.cpp and main-kopia.cpp. Notice that they both have main() inside them.

  • 1
    not much flexible, but at least works.
    – Radolino
    Commented Feb 24, 2015 at 13:54
  • Wasn't the question really about compiling the files separately? With this, even when I create different targets. When I hit build only the first default main.cpp gets called.
    – HalfWebDev
    Commented Jun 3, 2020 at 3:57
  • I got it. Just one detail is missing here. While building we need to toggle the active scheme next to run and stop buttons.
    – HalfWebDev
    Commented Jun 3, 2020 at 4:04

This gave me hard time too. To fix this I had to make sure that the file I intend to compile is the only one that appears in the 'Build Phases' section.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .