I has compiled and linked OpenCV project with Visual C++ Express under Windows.
With GCC under Ubuntu is compiled but is link error:
finder.cpp|219|undefined reference to `cv::imread(cv::String const&, int)'|
I have added libraries:
-lopencv_calib3d
-lopencv_core
-lopencv_features2d
-lopencv_flann
-lopencv_highgui
-lopencv_imgproc
-lopencv_ml
-lopencv_nonfree
-lopencv_objdetect
-lopencv_photo
-lopencv_stitching
-lopencv_superres
-lopencv_ts
-lopencv_video
-lopencv_videostab
and for example cv:line proceure is OK, but only one error (so far) imread
Related
I'm trying to complie opencv-3.1.0/samples/gpu/surf_keypoint_matcher.cpp through:
g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binary surf_keypoint_matcher.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_stitching -lopencv_imgcodecs -lopencv_xfeatures2d
But I'm getting this error:
/tmp/ccQW7t3Z.o: In function `main':
/home/luca/Downloads/opencv-3.1.0/samples/gpu/surf_keypoint_matcher.cpp:65: undefined reference to `cv::cuda::DescriptorMatcher::createBFMatcher(int)'
collect2: error: ld returned 1 exit status
Why?
Your error message:
/home/luca/Downloads/opencv-3.1.0/samples/gpu/surf_keypoint_matcher.cpp:65: undefined reference to `cv::cuda::DescriptorMatcher::createBFMatcher(int)'
collect2: error: ld returned 1 exit status
shows that you have an undefined reference to cv::cuda::DescriptorMatcher::createBFMatcher
this resides in the cuda libs so you need to include the appropriate headers and link into the following libs as opencv_cudafeatures2d depends on them:
opencv_cudafilters
opencv_cudawarping
opencv_features2d
opencv_cudaarithmn
opencv_flann
opencv_core
certainly this is true for opencv version 3.1.0 which is what I'm using
DescriptorMatcher is in cudafeatures2d.hpp. So, you have to include the header cudafeatures2d.hpp in the .cpp program and link the library in the compile process.
The program runs if you use this -lopencv_cudafeatures2d:
g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binary surf_gpu_probe1.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_stitching -lopencv_imgcodecs -lopencv_xfeatures2d -lopencv_cudafeatures2d
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.