Setup a C/C++ makefile project in Xcode: syntax highlight, autocompletion, jump to definition… and more!

Andrés Cecilia Luque
5 min readFeb 27, 2018

In this post I am going to show you how to setup Xcode, so you can use it in your C/C++ projects. It will allow you to use all the nice features offered by the IDE. At the moment of writing I am using Xcode 9.2 and MacOS High Sierra. I will use the crazyflie 2.0 firmware project as an example. It has a C codebase that uses makefiles as the build system.

First step: get the tools for building the project

In this case, the crazyflie project requires the GNU Embedded Toolchain for Arm. They offer many instalation alternatives (virtual machine, docker, build from source, download the precompiled binaries from ARM), but the easiest one I found is by using this brew formula, which obtains the last precompiled binaries from ARM and configures them, so the only thing you need to do is:

$ brew tap osx-cross/arm
$ brew install arm-gcc-bin

After that you can verify a correct installation by checking the version:

$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 7-2017-q4-major) 7.2.1 20170904 (release) [ARM/embedded-7-branch revision 255204]
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Second step: get the project’s source code

In this case, get the crazyflie firmware’s source code. Because this specific project makes use of git submodules, we need to also pass the --recursive argument:

git clone --recursive https://github.com/bitcraze/crazyflie-firmware.git

After this, the project will be under a folder named crazyflie-firmware .

Third step: setup the Xcode project for building

First, create a new Xcode project by doing:

File -> New -> Project

Under the cross-platform tab, use the External Build System template. Give it a name and finish the setup:

Then, go to the info tab of the main target and change the Directory field to the one that contains the crazyflie source code: crazyflie-firmware . It is also recommended to pass the -j argument to make in order to enable multicore building (which is faster!): in my case I have 2 cores, so I pass the -j2 argument. The result is:

At this point, if you run the project from Xcode you can successfully build it! 😂

Fourth step: get all those sweet sweet editor features

At the moment we have an empty project that is able to build, but we did not add the source code yet. Now, we are going to create a new target that will force Xcode to index the source code, thus allowing us to use all the editor features:

File -> New -> Target

Under the macOS tab, use the Command Line Tool template. Give it a name, set the language and finish the setup:

Xcode automatically created two folders in the project, and a new scheme: you can remove all of them.

Now we are going to drag the source code folder crazyflie-firmware to the Xcode project:

Xcode will prompt you for some information. Make sure to unmark the Create external build system project option:

In the next screen, under the Add to targets section, make sure to mark the previously created target (in my case I called it xcode):

After finishing this, you will see that your code appears in the project, and that Xcode is indexing it. Now you can benefit from all the editor features, such as syntax highlight, autocompletion, jump to definition, errors appearing inside Xcode next to their line, quick jump to the line containing the error, intelligent search and replace, refactoring tools… and many more! 🎉

You can find this xcode project example, with the crazyflie source code in github.

Bonus step: adding a target, so we can perform other actions

There is the possibility that your project includes other makefile targets. In this case, the crazyflie project has one that installs the firmware in the drone:

make cload

We can execute the command from inside Xcode, without the need of opening a terminal. Let’s create a new target:

File -> New -> Target

Under the cross-platform tab, use the External Build System template. Give it a name and finish the setup:

Then, go to the info tab of this newly created target and change the Directory field to the one that contains the crazyflie source code: crazyflie-firmware . Also, change the Arguments field to the makefile target you want to execute, in this case it is cload :

And finally, go to the Build Phases tab, and under Target Dependencies add the crazyflie target. This will make sure that the code is build and the firmware is there before trying to install it in the drone:

Now, you can run the cload target, and see how the code is build and installed in the drone! 🤘

--

--