Setting up Xcode for C++ projects

When I first started using Xcode for my C++ projects I was a bit overwhelmed by the settings interface; sure I had used Xcode heavily for Objective-C and then Swift project but most of the Build Settings are already set up to run any iOS/macOS app. For C++ however, it gets a bit trickier. Here is a solution I found works for C++ libraries built with cmake, make or Homebrew. I hope it turns out to be useful for anyone coding in C++ with Xcode.

Setting up Xcode for C++ projects is a four step process:

a. defining the build location:

This is mostly for convenience so this step can be skipped. Selecting “Legacy” as Build Location allows to store the build data in the same folder as the project. More information about the other choices can be found here.

b. providing the header search path:

In order to call a library API, the macro #include must be called. But Xcode needs to know where the projects header must be found (on macOS, these are usually in /usr/local/include). If you build the library with a package manager, you should look into the default folder or the one you set up at build. For example with Homebrew the default directory is /usr/local/Cellar/<library-name>/../include.

c. providing the libraries search location:

The C++ linker needs to know where to find the third party (dynamic ) libraries. They usually have the .dylib extension and are located by default on macOS in the folder /usr/local/lib or /usr/local/Cellar/<library-name>/../lib for a library built with Homebrew.

d. adding the linker flags

And finally the linker flags tell Xcode which library are actually used in the project. The format is -l<l-name> and the library named ‘l-name’ will be searched in the library search path (provided in c.) when linking. More informations on linking can be found in the gcc documentation.

Now Xcode is ready to work with any C++ library installed with Homebrew or build with cmake.

Principal Data scientist. I write about Machine learning, C++ and Python coding.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store