OpenCV on Xcode
Setting up Xcode for OpenCV project
OpenCV is one of the best if not the best computer vision library to use and therefore, many people with various backgrounds get their hands dirty trying it out. Developers using macOS as their primary operating system may use Xcode IDE since it is a key development environment to creating applications for Apple products. If you are interested in using OpenCV in your Xcode project, the following steps might help you.
Install OpenCV on macOS
Because you are a macOS user, you probably already use homebrew for package management. Then the easiest way to install OpenCV libary is
brew install opencv
It will install all dependencies on the fly as well.
pkg-config
Install pkg-config
is a very handy tool to ask for package information to use in your projects. To install it, use
brew install pkg-config
Since you want to use OpenCV library in your project and want to know all the flags to pass to compiler, you need to do
pkg-config --cflags --libs opencv4
It will give something like this:
-I/usr/local/Cellar/opencv/4.2.0_1/include/opencv4/opencv -I/usr/local/Cellar/opencv/4.2.0_1/include/opencv4 -L/usr/local/Cellar/opencv/4.2.0_1/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
Xcode Build Settings
Once the OpenCV is successfully installed, create a new Xcode project for macOS as a Command Line Tool and go to the Build Settings. Search with "Search Path" phrase and modify Include Search Path
and Library Search Path
variables. You need to find where the library is installed. If installed via homebrew
, then it is most likely under /usr/local/Cellar/
. When modify them, you will see a non-recursive
option on the right side of the entry, make it recursive
. See example below:
Next is to add linker options. Remember using pkg-config
tool? Basically, we will take that entire output and add them to Other Linker Flags
variable. Notice that there will be -I/usr/local/Cellar/opencv/4.2.0_1/include/opencv4/opencv -I/usr/local/Cellar/opencv/4.2.0_1/include/opencv4
and -L/usr/local/Cellar/opencv/4.2.0_1/lib
in the results. This is potentially what you have done for the Include
and Library
search path variables but for some reason Xcode does not find <opencv2/core.hpp>
if you do not modify these variables. Anyways, search for Other Linker Flags
and add the entire results of pkg-config --cflags --libs opencv4
to your linker flags variable.
And that is it.
Xcode OpenCV project Template
I have not been able to create a template for macOS Command Line Tool projects but looks like you can create an Xcode project template for other projects (iOS app, watchOS app, etc.) You can easily find the details on google.
Have fun.