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
I got this message using Version 10.10.3 Yosemite Mac pro retina 13″
processor: 2,4 GHz Intel Core i5
graphic: Intel Iris 1536 MB
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc’
to the PKG_CONFIG_PATH environment variable
No package ‘opencv’ found
clang: error: no such file or directory: ‘facedetect.cpp’
clang: error: no input files
The default location for .pc file is
/usr/local/lib/pkgconfig/opencv.pc
Make sure you have the file at that location. If you choose to use the different location, you have to specify it using the PKG_CONFIG_PATH environment variable.
I reinstalled it and tried it again and i made the test work running:
g++ -ggdb `pkg-config –cflags –libs opencv3` -stdlib=libc++ facedetect.cpp -o /tmp/test && /tmp/test
cause if i used, libstdc++ I got the “Undefined symbols for architecture x86_64:” error message.
Cant thank you enough!
Thanks for the kind words. I haven’t used nodejs yet, but if you have figured it out and want to share your experience, please send me a note at [email protected]
From here, how can I use opencv3.0 in nodejs?
i m not able set opencv lib. in linux …can anybody help…its urgent
Hi Satya, tx for the articles. lots of help. question about the above post, how do I do this if I used hombrew to install opencv3? tx.
Sorry for this late reply. This should help
$ pkg-config --modversion opencv
2.4.13
$ export PKG_CONFIG_PATH=/usr/local/opt/opencv3/lib/pkgconfig
$ pkg-config --modversion opencv
3.1.0
I’m getting an error saying
/usr/bin/ld: cannot find -lippicv
collect2: error: ld returned 1 exit status
I need help with this
You are missing ippicv which is normally located at
/usr/local//Cellar/opencv3/3.1.0_3/share/OpenCV/3rdparty/lib/libippicv.a
If you don’t have it, you can download it from
https://sourceforge.net/projects/opencvlibrary/files/3rdparty/ippicv/ippicv_macosx_20141027.tgz/download
and put it inside /usr/local/lib
Thanks, issue solved!
Hi there, I’m quite new to all this so sorry if this is something really obvious that I’m missing. I’ve installed opencv fine but when it comes to compiling the files I can’t seem to get anywhere with it.
when I put in the “pkg-config –cflags –libs opencv” command I get a list I get the list of paths to header files fine but when I move on to compiling the actual sample code I keep getting the error that there’s no such file or directory. I’m running opencv 3.1.0 on linux. any help would be greatly appreciated
That is very odd. Have you tried setting the environment variable PKG_CONFIG_PATH to the directory containing the opencv.pc file ? E.g. on a mac I do
$ export PKG_CONFIG_PATH=/usr/local/opt/opencv3/lib/pkgconfig
$ pkg-config --modversion opencv
3.1.0
If the above does not help, can you tell me how you are trying to compile the code ?
Hi Satya, I started Computer Vision recently and this is a great resource. I really appreciate you doing this. By the way, after I subscribed i got a mail with links to download the free resource guide which goes to a dead link.
Thanks a bunch for letting me know. I will fix it and update. Sorry about that.
It is fixed now.
Yup. It works. Thank you very much.
Hello,
When compiling facedetect.cpp using command line as you showed in this tut I had error:
g++: error: pkg-config –cflags opencv: No such file or directory
But when I get the libraries from the command “pkg-config –cflags –libs opencv” and paste it to g++ manually then it runs well, for example:
g++ facedetect.cpp -o facedetect -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn -lopencv_dpm -lopencv_fuzzy -lopencv_hdf -lopencv_line_descriptor -lopencv_optflow -lopencv_plot -lopencv_reg -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lippicv -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core
I don’t know why it cannot recognize ‘pkg-config –cflags –libs opencv’ in g++ command. Can you help me please?
Hello,
I hope you would be able to help me with the following error.
I set the PKG_CONFIG_PATH to the path where it has the opencv.ps using
$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/
when typing
$ pkg-config –cflags –libs opencv
I get the following:
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
But then, compiling gives me errors:
[email protected]:~/Documents/opencv-3.1.0/samples/cpp$ g++ -ggdb `pkg-config –cflags –libs opencv` facedetect.cpp -o facedetect
I get plenty of lines in the form of
undefined reference to `cv::VideoCapture::VideoCapture()’
undefined reference to `cv::CascadeClassifier::CascadeClassifier()’
undefined reference to `cv::CascadeClassifier::CascadeClassifier()’
ans so on…
where did I go wrong?
thanks
Try,
g++ -ggdb facedetect.cpp -o facedetect `pkg-config –cflags –libs opencv`
Didn’t help. Any more ideas?
did you install the opencv-contrib module?
Hello,
I can connect to rtsp but when try to read, read stoped and not finished. do you know why ?
how could i run the opencv code without installing the openCV program or without using the opencv command in my terminal
You have to compile it for your platform, otherwise you cannot.
Hi Satya, the commands given to run OpenCV program on Linux doesn’t work. `pkg-config –cflags –libs opencv` should move to the end of the command.
That sounds very odd, but I will try it. Thanks.
Hi Shashwat , Finally got a chance to try. You’re right. I have fixed it.
Hi, I want to use opencv with g2o. However, g2o follows c++11 standard. By using the g++ command shown above, I keep getting errors. Could you help me figure it our? Can I use g++ with C++11 standard? Thanks!
OpenCV 3.3 has support for C++11. Check it out. http://opencv.org/opencv-3-3.html
Thanks! Do I need to enable the option of “-c++11” when installing opencv3 with homebrew so that it will support for C++11?