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.

/usr/bin/ruby -e $(curl -fsSL
  • Install OpenCV

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

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.

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:


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

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


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

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

Then run the binary:

chmod +x 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:
  • 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.
  • 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:

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.