Configure OpenCV for Xcode

Xcode is a complete developer toolset for creating apps for Mac, iPhone, iPad, Apple Watch, and Apple TV. Xcode brings user interface design, coding, testing, debugging, and submitting to the App Store all into a unified workflow.

Here’s a tutorial to setting up OpenCV and C++ environment for Xcode on macOS.

Pre Requirements

  • Install Homebrew

Aptly titled “The missing package manager for macOS” is enough said. Homebrew is the macOS equivalent of the Ubuntu/Debian-based apt-get.

1
/usr/bin/ruby -e $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)
  • Install OpenCV

Simply we use brew to install the latest pre-compiled OpenCV to our computer:

1
brew install opencv
  • Install pkg-config

The 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.

1
brew install pkg-config
  • View OpenCV linker flags

To view the linker flags for OpenCV, we should find .pc file first. It’s common to see this file in:

1
/usr/local/Cellar/opencv/<version_number>/lib/pkgconfig/opencv.pc

Then view the linker flags by specifing the location of opencv.pc file:

1
pkg-config --cflags --libs <path/to/opencv.pc>

Commandline

Running code in the terminal, the code should be compiled first:

1
g++ $(pkg-config --cflags --libs <path/to/opencv.pc>) -std=c++17  main.cpp -o main.o

Then run the binary:

1
2
chmod +x main.o
./main.o

Xcode developing

Configure enviroment

  • Create project

    Before following the below steps to run OpenCV C++ code in Xcode, a C++ project in Xcode should be created first.

    1. Click on File > New > Project
    2. Under Choose a template for new project click on macOS
    3. Under Application click on Command Line Tool
    4. Get the above screen. Fill in the details and set the Language to C++.
  • Set Header Search Paths

    1. To set Header Search Path in Xcode, first click on the Xcode project, then go to Build Settings and then search for Header Search Paths.
    2. Set the Header Search Path to the path of OpenCV include folder:
1
2
3
/usr/local/Cellar/opencv/<version_number>/include/opencv4/opencv2
/usr/local/Cellar/opencv/<version_number>/include/opencv4
/usr/local/Cellar/opencv/<version_number>/include
  • Set Library Search Paths

    1. In this case, follow steps similar to Set Header Search Paths above but search for Library Search Paths in the search bar.
    2. Set the Library Search Path to the path of OpenCV library folder.
1
/usr/local/Cellar/opencv/<version_number>/lib
  • Set Other Linker Flags

    1. Search for Other Linker Flags in the search bar.
    2. Set the other linker flags with all the flag values obtained after running pkg-config command above.

Apply permitions

  • To allow camera access, first click on the Xcode project, then go to TARGETS > Signing & Capabilities and check Disable Library Validation, Debugging Tool, Audio Input and Camera.

  • Create Info.plist, add access requirement details:

1
2
Key: Privacy - Camera Usage Description
Value: $(PRODUCT_NAME) camera use

Then go to TARGETS > Build Phases > Copy Files, select Destnation to Products Directory, leave Subpath blank and click + to select file Info.plist.