Setting up OpenCV and C++ development environment in Xcode for Computer Vision projects

Xcode is a free and excellent IDE not only for creating iOS apps but also for C++ development and working on computer vision projects using OpenCV. After interviewing with some companies in Silicon Valley, I got to know that a lot of commercial AR software such as Snap Lenses, Instagram’s Filters, Apple’s Animoji, etc. is written in C++ to maximize performance on resource constrained devices such as mobile phones and tablets. A lot of the new features in OpenCV are first available in C++ and then introduced to Python since OpenCV is natively written in C++ and provides bindings for Python, Java and MATLAB.

Unfortunately, I used Python with OpenCV for computer vision for ease of use since Python is much more readable, easier to code than C++ and setting the development environment is easier too. However, Python is not used for computer vision in industry and thus the motivation for writing this.

This post goes through setting up OpenCV and C++ environment on MacBooks which is actually the hardest part for a beginner starting in computer vision because of lack of trustworthy documentation since most tutorials online deal with setting up the environment for Python and a very less percentage of them deal with setting it up on Xcode.

  1. Install Xcode
Image for post
Image for post
Search for Xcode in top right corner after opening App Store and click on Xcode
Image for post
Image for post
Click on the Get button to start the installation. It changes to Open after Xcode is installed.

Install Xcode from the App Store. Open the App Store and search for Xcode and then click the Get button(in my case it shows Open because I have Xcode installed). Since, Xcode(9.2) setup size is about 5.5 GB it may take about 30–60 minutes to install depending on the internet speed.

2. Install Homebrew

Aptly titled ‘The missing package manager for macOS’, enough said. Homebrew is the macOS equivalent of the Ubuntu/Debian-based apt-get. For the installation, just open a terminal and paste:

3. Install OpenCV

To install OpenCV using brew, open a terminal and paste:

This should install OpenCV 3.

4. Install pkg-config

pkg-config is a helper tool used when compiling applications and libraries. It helps you insert the correct compiler options on the command line rather than hard-coding values. This will be helpful for finding the correct linker flags for OpenCV. This will be more clear in the subsequent steps.

To install pkg-config using brew, open a terminal and paste:

5. View OpenCV linker flags

To view the linker flags for OpenCV, run:

The output looks like:

The above shows you the includes and the libraries for OpenCV.

In case the command to get linker flags doesn’t work, you might need to specify the location of opencv.pc file:

My opencv.pc file is located at

Yours should be on a similar path.

Running your code on the terminal

Paste the below line which compiles the OpenCV code with the appropriate linker flags.

Run the binary,

Running your code in Xcode

Image for post
Image for post
Set Language as C++ for the new Command Line Tool project

Before following the below steps to run OpenCV C++ code in Xcode, you first need to create a C++ project in Xcode.

  • Click on File>New>Project
  • Under Choose a template for your new project click on macOS
  • Under Application click on Command Line Tool
  • You should get the above screen. Fill in the details and set the Language to C++

Set Header Search Paths

Image for post
Image for post
Setting Header Search Path in Xcode

To set Header Search Path in Xcode, first click on the Xcode project(computer vision in this case) and then go to Build Settings and then search for Header Search Paths.

Set the Header Search Path to the path of OpenCV include folder. In my case, this is:

Set Library Search Paths

Image for post
Image for post
Setting Library Search Path in Xcode

In this case, follow steps similar to Set Header Search Paths above but search for library search paths in the search bar.

Set the Library Search Path to the path of OpenCV library folder. In my case this is:

Set Other Linker Flags

Image for post
Image for post
Set Other Linker Flags in Xcode

Search for other linker flags in the search bar.

Set the other linker flags with all the flag values obtained after running pkg-config command above.

Run code

You are all set to run your OpenCV project in Xcode. Press Cmd+R to run your Xcode project.

Passing arguments to your code

Image for post
Image for post
Click on the scheme selector to set your active scheme(computer vision in this case) and then click on Edit Scheme
Image for post
Image for post
In the Edit Scheme, add the required arguments or environment variables needed by your source code

In case you want to pass arguments such as an image file to your Xcode project, you will need to edit scheme as shown above.

Using relative file paths

Image for post
Image for post
Setting Working Directory for relative file paths in Xcode

Xcode uses the absolute path of the image or resource. In order to set relative paths, you need to set the Working Directory. To do that, in Edit Scheme go to the Options tab in Run and check Use Custom Working Directory and add the project directory name.

Multiple files in same project

The project will fail to build in case you have multiple C++ files for a given project. In this case, you will need to create Targets within your project, where each Target corresponds to a C++ file.

References

Written by

Machine Learning Engineer at Twitter. Lives in San Francisco. Sharing my thoughts and experiences on technology and culture in Silicon Valley

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