I've installed OpenCV via this instruction.
I use OpenCV when and build the code via cmake - and it's all ok.
But now I'm trying to use OpenCV from QT, and I get errors like this:
error: undefined reference to `cvCreateCameraCapture'
The same errors for all functions from OpenCV.
I tried to add in .pro this code:
INCLUDEPATH += /usr/local/include/opencv2
LIBS += -L/usr/local/lib
LIBS += -lopencv_core
LIBS += -lopencv_imgproc
LIBS += -lopencv_highgui
LIBS += -lopencv_ml
LIBS += -lopencv_video
LIBS += -lopencv_features2d
LIBS += -lopencv_calib3d
LIBS += -lopencv_objdetect
LIBS += -lopencv_contrib
LIBS += -lopencv_legacy
LIBS += -lopencv_flann
But it isn't work.
The dir /usr/local/include/opencv2 exist and not empty.
So, what's wrong?
Add this to .pro file:
LIBS += "pkg-config --libs opencv"
You are getting a linker error, so the compilation is correct. In order to add correctly the linking library directories and the linking libraries to your .pro file you should issue a command like this at the console:
username#linux-host:~> echo $(pkg-config --libs opencv)
You should then read a long list of libraries as in the following line:
-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab /usr/lib64/libXext.so /usr/lib64/libX11.so /usr/lib64/libICE.so /usr/lib64/libSM.so /usr/lib64/libGL.so /usr/lib64/libGLU.so -lrt -lpthread -lm -ldl
After you have obtained this long sequence of strings with all the libraries and the options, insert it in the .pro file like this:
LIBS += -lopencv_calib3d -l... ...insert all the strings as seen above!
Save the .pro file, re-run qmake and run make again. The linking error should have disappeared.
If the problem is not disappeared or if other linking errors are shown, find all the files with the extension .pc in your opencv compilation directory (and subdirectories) and copy them as root in the directory /usr/share/pkgconfig/
Then issue again the command above and insert in the .pro file the correct sequence of strings that identify the library options in the LIBS += line and save the .pro file, re-run qmake and re-run make.
Related
I am using xcode to compile my opencv project, and I have some settings as below:
HEADER_SEARCH_PATHS = /usr/local/include
LIBRARY_SEARCH_PATHS = /usr/local/lib
OTHER_LDFLAGS = -lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videoio -lopencv_videostab
I want to know what shall I write by terminal command rather than setting of xcode.
If you installed OpenCV with homebrew and you also installed the pkgconfig package with homebrew, the package can tell you the settings you need itself - far more accurately than you can guess them.
The easy way is to ask pkgconfig to list all the packages it knows about:
pkg-config --list-all | grep -i opencv
opencv OpenCV - Open Source Computer Vision Library
So, now you know the package name is plain and simple opencv, and you can find the flags you need like this:
pkg-config --cflags --libs opencv
-I/usr/local/Cellar/opencv/2.4.12_2/include/opencv \
-I/usr/local/Cellar/opencv/2.4.12_2/include \
-L/usr/local/Cellar/opencv/2.4.12_2/lib \
-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab
Which means your compilation and linking becomes simply:
g++ $(pkg-config --cflags --libs opencv) program.cpp -o program
If you do that in a Makefile, you will need to double up the $ signs.
If your system is not so well installed, you may need to find the pkgconfig file yourself. So you would do:
find /usr/local -name "opencv*pc"
/usr/local/Cellar/opencv/2.4.12_2/lib/pkgconfig/opencv.pc
Then you can access that file specifically like this:
pkg-config --cflags --libs /usr/local/Cellar/opencv/2.4.12_2/lib/pkgconfig/opencv.pc
You can use:
g++ -I ${HEADER_SEARCH_PATHS} -L ${LIBRARY_SEARCH_PATHS} ${OTHER_LDFLAGS} ${SOURCE_FILES}
When I tried to link the OpenCV library on Ubuntu, some problems happened which I cannot understand.
I used OpenCV 2.4.11 and Qt creator on Ubuntu 14.04.
In my project file, I wrote
INCLUDEPATH += /usr/local/include/opencv
INCLUDEPATH += /usr/local/include/opencv2
LIBS += -L/usr/local/lib\
-lopencv_calib3d\
-lopencv_contrib\
-lopencv_core\
-lopencv_features2d \
-lopencv_flann\
-lopencv_gpu\
-lopencv_highgui\
-lopencv_imgproc\
-lopencv_legacy\
-lopencv_ml\
-lopencv_nonfree\
-lopencv_objdetect\
-lopencv_ocl\
-lopencv_photo\
-lopencv_stitching\
-lopencv_superres\
-lopencv_ts\
-lopencv_video\
-lopencv_videostab
But there were many errors when building the project, such as undefined reference to 'cvCreateMat'. Obviously, the OpenCV wasn't linked to my project properly.
Thus, I searched online and tried a possible solution, and that is
CONFIG += link_pkgconfig
PKGCONFIG += opencv
To my surprise, it works and no linking errors again.
However, I still cannot understand why it didn't work in the first way, since I have successfully set up my project in that way on Mac OS X. Why doesn't it work on my Ubuntu now?
I tried pkg-config --libs opencv, the result was
-L/usr/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -ltbb -lXext -lX11 -lICE -lSM -lGL -lGLU -lrt -lpthread -lm -ldl
You've missed one library: -lopencv_nonfree
Best way to include openCV in qt projects on Linux is to add to pro file something like:
# add open CV
unix {
CONFIG += link_pkgconfig
PKGCONFIG += opencv
}
You will be free of path problems when moving code to another machine.
https://stackoverflow.com/a/17137998/1387438
This is what pkgconfig links:
luca#luca-virtual-machine:~$ pkg-config --libs 'opencv'
/usr/lib/x86_64-linux-gnu/libopencv_calib3d.so -lopencv_calib3d /usr/lib/x86_64-linux-gnu/libopencv_contrib.so -lopencv_contrib /usr/lib/x86_64-linux-gnu/libopencv_core.so -lopencv_core /usr/lib/x86_64-linux-gnu/libopencv_features2d.so -lopencv_features2d /usr/lib/x86_64-linux-gnu/libopencv_flann.so -lopencv_flann /usr/lib/x86_64-linux-gnu/libopencv_gpu.so -lopencv_gpu /usr/lib/x86_64-linux-gnu/libopencv_highgui.so -lopencv_highgui /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so -lopencv_imgproc /usr/lib/x86_64-linux-gnu/libopencv_legacy.so -lopencv_legacy /usr/lib/x86_64-linux-gnu/libopencv_ml.so -lopencv_ml /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so -lopencv_objdetect /usr/lib/x86_64-linux-gnu/libopencv_ocl.so -lopencv_ocl /usr/lib/x86_64-linux-gnu/libopencv_photo.so -lopencv_photo /usr/lib/x86_64-linux-gnu/libopencv_stitching.so -lopencv_stitching /usr/lib/x86_64-linux-gnu/libopencv_superres.so -lopencv_superres /usr/lib/x86_64-linux-gnu/libopencv_ts.so -lopencv_ts /usr/lib/x86_64-linux-gnu/libopencv_video.so -lopencv_video /usr/lib/x86_64-linux-gnu/libopencv_videostab.so -lopencv_videostab
You can compare with yours. Something may be missing.
go to your “.pro” file and add the following lines before “SOURCES”:
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui
you have to add all the libraries you’re going to need in your project.
I've just installed OpenCV on my Debian machine, and I'm having issues with it. I followed the install guide on the Wiki. Trying to compile the examples gives what appears to be a successful compile, however trying to run them ends up throwing an error:
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$ g++ -Wall em.cpp
-lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video
-lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect
-lopencv_contrib -lopencv_legacy
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$ ./a.out
./a.out: error while loading shared libraries: libopencv_core.so.2.3:
cannot open shared object file: No such file or directory
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$ pkg-config opencv
--libs-L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui
-lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d
-lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$
But even doing this doesn't work:
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$ g++ -Wall em.cpp
`pkg-config opencv --cflags --libs`
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$ ./a.out
./a.out: error while loading shared libraries: libopencv_core.so.2.3:
cannot open shared object file: No such file or directory
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$
I think there's an issue with the linking, but I'm not quite sure what's going on. This is a completely fresh install of OpenCV - I've never had it on this machine before. Does anyone have any ideas?
Ran
sudo ldconfig
Suggested by anon.coward
I have done all the necessary job like adding "c:/cygwin/bin" to system path, I have also set the include path under project->properties->c/c++ build->setting and also set the linker library search path.
Now when I include file like #include"cv.h", it gives no error, but when I run the program error comes like
undefined reference to '_cvGetSize'
I have included the necessary files, and read lots of tutorials that are available but still I couldn't able to run my code, please help me
I always recommend that before jumping to an IDE you make sure your system is installed and configured correctly by attempting to compile an application on the cmd-line:
g++ flann.cpp -o flann `pkg-config --cflags --libs opencv`
or:
g++ flann.cpp -o flann -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
Apparently you didn't link your project with the opencv libraries, which is being achieved in the cmd above with: -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann.
That missing symbol is defined in one of these libraries.
I'm having a problem with linking my application that uses Qt and OpenCV on Ubuntu.
my qmake .pro file:
SOURCES = ../../../Source/*.cpp ../../../Source/LinuxSpecific/*.cpp
HEADERS = ../../../Source/*.h ../../../Source/LinuxSpecific/*.h
FORMS = ../../../Source/UI/*.ui
CONFIG+=link_pkgconfig
PKGCONFIG+=opencv
QMAKE_CXXFLAGS += -std=c++0x
I intstalled opencv from the package here: https://launchpad.net/~gijzelaar/+archive/opencv2.3
I've verified OpenCV 2.3 is installed on my machine by running the following:
tim#tim-Desktop:~$ pkg-config --cflags --libs opencv
-I/usr/include/opencv -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
tim#tim-Desktop:~$
The error: /usr/bin/ld: cannot find -lopencv_contrib
G++ version: gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)