Mac Xcode and C++ — Project/Programs Management

This post talks more about how one can manage multiple c++ files in a single target or how one can add multiple targets to manage a project better.

If you are confused about how you can start a c++ project in Xcode, check out this : Mac Xcode and C++ — Beginning

This post can be a bit daunting at first. Keep patience with me. I will get better in explaining stuff. If you have any doubts do comment and few things will look a but hacky, if there’s a better way please let me know too.

I started the project to learn c++ and practice it daily. There can be a simple way to do things. One is you create a new project for every new program you write, but I dont want that. I want all my code to be in a single project so I started figuring out how I can do that.

As obvious whatever I am sharing here is what I learned by searching over the internet and if you wrote some article on the same lines, Thank you for doing so :)

There can be two possible ways to do the above :

First : In a single folder/target you create multiple c++ program files.

Second : You create multiple targets to segregate different c++ files or programs.

I used both the approaches as I want to create all the programs related to Array in a single Array target and also wants to practice trees so created a new target for this.

First :

Select the Folder/Target and Add a new file.

Xcode > File > New > File > macOS> C++ file > Name(Deselect Header File)> Enter

or just Cmd + N > macOS> C++ file > Name(Deselect Header File)> Enter

This is how you can add a new file in any target you want.

Now you can write a simple program and as soon as you hit Run or cmd + R you will see an error!

So why this error is coming? Xcode is confused in terms of which file to run: The main.cpp or the newFile.cpp you created. To remove the error I found this way.

Click on the Project, in the Project explorer you will have a single target as of now and you will see Two compile sources. You have to click on it and remove it using the - minus sign. Hit cmd + R or hit run and you will see your program running.

This is how you can have multiple files in a single Target and you can select which file to run. This looks a bit hacky to me too! If you find something better and I am sure there must be a better way to do this, then hit me up :)

Second

Adding Multiple Targets! This will made your day if you are doing it for the first time as I also got happy when I learned about it. This makes the project clean and easier to maintain. You can structure your code and its all in a single project which gives you an advantage as you can look at all your programs in a single place still they dont mess with each other.

How to add a target?

Xcode > File > New > Target > macOS > Command Line Tool > Next > Language -> C++ > Next

In simple term its a new c++ project but tied within a single main project. It will by default comes with its main.cpp file which will run by default.

How to run this specific target?

just next to your Run button you can select the target and can run it. Select the Target, Hit run and you are done. Your new target is up and running. I am not sure if there is a shortcut to select a target other than this.

Deleting a file and a target is pretty simple, select the file or target and click delete. You can permanently delete the file/target( Click on move to trash) or remove from the project which you can add later again.

You can Rename the file by just selecting the file and then hit enter. Rename the file, save and run.

Next I will talk about handling Files, Breakpoints and command line arguments also if you want to contribute I will be more than happy :)

PS : If you feel like some of your friends will find it important then you can recommend it too.

Like what you read? Give Aviral Agarwal a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.