• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Learn OpenCV

OpenCV, PyTorch, Keras, Tensorflow examples and tutorials

  • Home
  • Getting Started
    • Installation
    • PyTorch
    • Keras & Tensorflow
    • Resource Guide
  • Courses
    • Opencv Courses
    • CV4Faces (Old)
  • Resources
  • AI Consulting
  • About

How to compile OpenCV sample code ?

Satya Mallick
February 4, 2015 31 Comments
Compile

February 4, 2015 By 31 Comments

Let’s say you want to compile and run the face detection sample code that comes with the OpenCV. This code is located at

OpenCV 2.4.x : /path/to/opencv/samples/c/facedetect.cpp
OpenCV 3 : /path/to/opencv/samples/cpp/facedetect.cpp

Compiling the sample code is super easy using pkg-config. Try this on the command line

pkg-config --cflags --libs opencv

If you have multiple versions of opencv installed, you can provide the path to the opencv.pc file.

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

The above command lists all the paths to header files and libraries for OpenCV. The instructions below show how to use pkg-config with g++ to compile OpenCV sample code.

Compile and run OpenCV code from the command line on Linux

# For OpenCV 2.4.x 
cd /path/to/opencv/samples/c/
# For OpenCV 3 
cd /path/to/opencv/samples/cpp/
#Compile
g++ -ggdb facedetect.cpp -o facedetect `pkg-config --cflags --libs opencv`
#run
./facedetect

Compile and run OpenCV code from the command line on Mac OSX

# For OpenCV 2.4.x 
cd /path/to/opencv/samples/c/
# For OpenCV 3 
cd /path/to/opencv/samples/cpp/
#Compile
g++ -ggdb `pkg-config --cflags --libs opencv` facedetect.cpp -o facedetect

# For OpenCV 3 installed using Homebrew provide the full 
# path to opencv.pc file
g++ -ggdb `pkg-config --cflags --libs /usr/local/Cellar/opencv3/3.1.0_3/lib/pkgconfig/opencv.pc` facedetect.cpp -o facedetect

#run
./facedetect

Mac OSX 10.9 and above

If you had compiled OpenCV with libstdc++ you will get the following error.

Undefined symbols for architecture x86_64:

This happens because on OSX 10.9 and above, the compiler links to libc++ by default. You can fix the problem by simply instructing the compiler to use libstdc++ instead —

g++ -ggdb `pkg-config --cflags --libs opencv` -stdlib=libstdc++ facedetect.cpp -o facedetect

Subscribe

If you liked this article, please subscribe to our newsletter and receive a free
Computer Vision Resource guide. In our newsletter we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.

Subscribe Now

Tags: compile how to OpenCV pkg-config sample

Filed Under: Compile

About

I am an entrepreneur with a love for Computer Vision and Machine Learning with a dozen years of experience (and a Ph.D.) in the field.

In 2007, right after finishing my Ph.D., I co-founded TAAZ Inc. with my advisor Dr. David Kriegman and Kevin Barnes. The scalability, and robustness of our computer vision and machine learning algorithms have been put to rigorous test by more than 100M users who have tried our products. Read More…

Getting Started

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

Resources

Download Code (C++ / Python)

ENROLL IN OFFICIAL OPENCV COURSES

I've partnered with OpenCV.org to bring you official courses in Computer Vision, Machine Learning, and AI.
Learn More

Recent Posts

  • Making A Low-Cost Stereo Camera Using OpenCV
  • Optical Flow in OpenCV (C++/Python)
  • Introduction to Epipolar Geometry and Stereo Vision
  • Depth Estimation using Stereo matching
  • Classification with Localization: Convert any Keras Classifier to a Detector

Disclaimer

All views expressed on this site are my own and do not represent the opinions of OpenCV.org or any entity whatsoever with which I have been, am now, or will be affiliated.

GETTING STARTED

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

COURSES

  • Opencv Courses
  • CV4Faces (Old)

COPYRIGHT © 2020 - BIG VISION LLC

Privacy Policy | Terms & Conditions

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.AcceptPrivacy policy