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.

Build location setting

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.

Adding header search paths

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.

Adding library search path

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.

Adding linker flags

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

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Hunting Down an Obscure Avro Bug on a Remote Spark Cluster

Getting started with Bitcoin data on Kaggle with Python and BigQuery

3D Survival FPS Shooter

Scraping it Together

Migrate to Google Cloud DNS Using OctoDNS: Part 2

The Meter Monthly — March 2021

Fluttered and became a Flutter Contributor

Technology Tips for Construction Firms During COVID-19 | Withum

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
René-Jean Corneille

René-Jean Corneille

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

More from Medium

Publishing to the App Store with Fastlane and GitHub Actions

Understanding Git Branches

Pathway to cleaner code

Apple Watch 8 Bug: SwiftUI Navigation Stack pops back with TabView