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

READ/DOWNLOAD*- An Introduction to Statistics with Python: With Applications in the Life Sciences…

Software trends for 2021

PrintCSS: Footnotes

Footnotes rendered with PDFreactor.

Failing fast but learning faster

Rails on App Engine: Tips and Tricks

Snowflake : STRIP_OUTER_ARRAY in JSON:

A Field Guide to Building My First CLI Gem

Top 5 Use Cases for Business Capabilities to Transform an Organization

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

How to Create Your Own iOS Password Generator

Use a Swift command-line tool in a GitHub workflow

How to install and run Swift on Linux

UIView File Structuring