I would like to compile a C++ program that uses openCV statically, so that I can use it on other computers without having to install openCV.
I compiled openCV 2.4.5 with following flags in order to get static libraries and to install it besides my main installation which is version 3.1.
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/opt/openCV_2_4_5 -D WITH_FFMPEG=OFF -DBUILD_SHARED_LIBS=NO ..
ls /opt/openCV_2_4_5/lib
libopencv_calib3d.a libopencv_flann.a libopencv_legacy.a libopencv_photo.a libopencv_video.a
libopencv_contrib.a libopencv_gpu.a libopencv_ml.a libopencv_stitching.a libopencv_videostab.a
libopencv_core.a libopencv_highgui.a libopencv_nonfree.a libopencv_superres.a pkgconfig
libopencv_features2d.a libopencv_imgproc.a libopencv_objdetect.a libopencv_ts.a python2.7
After installing, I tried to compile a c++ program for face detection like this:
g++ -std=c++11 -L/opt/openCV_2_4_5/lib -I/opt/openCV_2_4_5/include -o ex2 ex2.cpp -L. -lpthread -lz -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn -lopencv_dpm -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_plot -lopencv_reg -lopencv_saliency -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 -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core
From that I got the error:
/tmp/cct4prBn.o: In function `main':
ex2.cpp:(.text+0x141): undefined reference to `cv::imread(std::string const&, int)'
ex2.cpp:(.text+0x2bc): undefined reference to `cv::imread(std::string const&, int)'
ex2.cpp:(.text+0x4f3): undefined reference to `cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
collect2: error: ld returned 1 exit status
make_2_4_5:9: recipe for target 'ex2' failed
make: *** [ex2] Error 1
I used the same library flags as my installation of OpenCV 3.1 has. I got them from:
pkg-config --cflags --libs opencv
The -L and -I flags point to the openCV 2.4.5 installation in the /opt directory.
Why doesn't it compile? All OpenCV libraries are listed in the section where the static libraries should be.
In OpenCV 2 I believe cv::imread() and cv::imwrite() are in the highgui library. In OpenCV 3 they were moved to the imgcodecs library. Try adding:
-lopencv_highgui
to your command line.
Related
I'm going step by step like this guide and my code does not compile. It has gotten some linker errors which I don't know how to fix. this is my terminal's answer to : pkg-config --libs opencv
/usr/lib/x86_64-linux-gnu$ 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
sorry if it's a mess to read but this is exactly how I received the answer.
This is how I've organized my linker
this is the error I get:
Building target: Ball_Plate Invoking: GCC C++ Linker g++ -L/usr/lib/x86_64-linux-gnu -o"Ball_Plate" ./Src/src.o -lopencv_core\ opencv_imgproc\ opencv_highgui\ opencv_ml\ opencv_video\ opencv_features2d\ opencv_calib3d\ opencv_objdetect\
opencv_contrib\ opencv_legacy\ opencv_flann /usr/bin/ld: cannot find
-lopencv_core opencv_imgproc opencv_highgui opencv_ml opencv_video opencv_features2d opencv_calib3d opencv_objdetect opencv_contrib
opencv_legacy opencv_flann collect2: error: ld returned 1 exit status
makefile:45: recipe for target 'Ball_Plate' failed make: ***
[Ball_Plate] Error 1
I don't know how to fix this, I don't know much about how linker works around .so files and I would appreciate that if you could help me.
So I read this post and found out what the problem was, tried to compile the OpenCV library with -DBUILD_TIFF=ON, Now Eclipse can find the header files but I get linker errors again, Eclipse cannot link the libraries.
I am trying to build a sample from OpenCV. Unfortunately I always get these errors while building.
I made an installation with opencv_contrib. OpenCV is installed in ~/lib/pokus/installed (contains bin include lib share). I am buiding example ~/lib/pokus/installed/share/OpenCV/samples/cpp/opencv_version.cpp (which came with OpenCV).
My g++ command:
g++ $(pkg-config --cflags --libs ~/lib/pokus/installed/lib/pkgconfig/opencv.pc) opencv_version.cpp -o test
Result:
opencv_version.cpp:(.text+0x78): undefined reference to `cv::CommandLineParser::CommandLineParser(int, char const* const*, cv::String const&)'
opencv_version.cpp:(.text+0xa8): undefined reference to `cv::CommandLineParser::has(cv::String const&) const'
opencv_version.cpp:(.text+0xc6): undefined reference to `cv::CommandLineParser::printMessage() const'
opencv_version.cpp:(.text+0xd7): undefined reference to `cv::CommandLineParser::check() const'
opencv_version.cpp:(.text+0xea): undefined reference to `cv::CommandLineParser::printErrors() const'
opencv_version.cpp:(.text+0x113): undefined reference to `cv::CommandLineParser::has(cv::String const&) const'
opencv_version.cpp:(.text+0x12a): undefined reference to `cv::getBuildInformation()'
opencv_version.cpp:(.text+0x180): undefined reference to `cv::CommandLineParser::~CommandLineParser()'
opencv_version.cpp:(.text+0x1bc): undefined reference to `cv::CommandLineParser::~CommandLineParser()'
opencv_version.cpp:(.text+0x1f8): undefined reference to `cv::CommandLineParser::~CommandLineParser()'
/tmp/ccRHDRg5.o: In function `cv::String::String(char const*)':
opencv_version.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccRHDRg5.o: In function `cv::String::~String()':
opencv_version.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status
Content of ~/lib/pokus/installed/lib/pkgconfig/opencv.pc
# Package Information for pkg-config
prefix=~/lib/pokus/installed
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 3.2.0
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cvv -lopencv_dpm -lopencv_freetype -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_plot -lopencv_dnn -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core
Libs.private: -L/usr/lib/x86_64-linux-gnu -lQt5Test -lQt5Concurrent -lQt5OpenGL -lpng -lz -ltiff -ljasper -ljpeg -lImath -lIlmImf -lIex -lHalf -lIlmThread -ldc1394 -lavcodec-ffmpeg -lavformat-ffmpeg -lavutil-ffmpeg -lswscale-ffmpeg -lQt5Core -lQt5Gui -lQt5Widgets -ldl -lm -lpthread -lrt -lGLU -lGL -ltbb
Cflags: -I${includedir_old} -I${includedir_new}
Sorry for so frequently asked question, but none of the others seems to solve my problem.
Thanks for all tips!
The file name must come before the params:
g++ opencv_version.cpp $(pkg-config --cflags --libs ~/lib/pokus/installed/lib/pkgconfig/opencv.pc) -o test
I had the same issue trying to work with OPENCV 4.3.0 after installation and so. In order to add the libs and compile properly.
Worked for me in order to test the opencv_version.cpp
g++ opencv_version.cpp $(pkg-config --cflags --libs opencv4) -o opencv_version
Result
./opencv_version
Welcome to OpenCV 4.3.0
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'm bundling openCV into an SDK for people developing in C++ in linux. I'm linking a test executable with my built library and its giving me a huge dump of errors, one of which is:
/path/to/build/libopencv_core.a(copy.cpp.o): In function `cv::Mat::setTo(cv::_InputArray const&, cv::_InputArray const&)':
copy.cpp:(.text._ZN2cv3Mat5setToERKNS_11_InputArrayES3_+0x15c1): undefined reference to `ippicviSet_32f_C1MR'
copy.cpp:(.text._ZN2cv3Mat5setToERKNS_11_InputArrayES3_+0x1700): undefined reference to `ippicviSet_32f_C4MR'
copy.cpp:(.text._ZN2cv3Mat5setToERKNS_11_InputArrayES3_+0x1743): undefined reference to `ippicviSet_32s_C4MR'
copy.cpp:(.text._ZN2cv3Mat5setToERKNS_11_InputArrayES3_+0x17a4): undefined reference to `ippicviSet_16s_C4MR'
copy.cpp:(.text._ZN2cv3Mat5setToERKNS_11_InputArrayES3_+0x1805): undefined reference to `ippicviSet_16u_C4MR'
What is this error referencing? Should I be building without ipp? I compiled the opencv libs -fPIC and standalone. I'm actually getting thousands of errors which have some mention of ippicvi and this is just an example.
I had the same error. You need to link the executable to the library libippicv.
This is a 3rd party library used by OpenCV an you can find it in /usr/local/share/OpenCV/3rdparty/lib/libippicv.a.
You can also find in opencv.pc all the dependencies used by OpenCV. E.g.:
/usr/local/lib/pkgconfig/opencv.pc
# Package Information for pkg-config
prefix=/usr/local exec_prefix=${prefix} libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 3.2.0
Libs: -L${exec_prefix}/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
Libs.private: -L${exec_prefix}/share/OpenCV/3rdparty/lib -llibwebp
-lippicv -L/usr/lib/x86_64-linux-gnu -lpng -lz -ltiff -ljasper -ljpeg -lImath -lIlmImf -lIex -lHalf -lIlmThread -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0 -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lfontconfig -lfreetype -lgthread-2.0 -ldc1394 -lavcodec-ffmpeg -lavformat-ffmpeg -lavutil-ffmpeg -lswscale-ffmpeg -lstdc++ -ldl -lm -lpthread -lrt Cflags: -I${includedir_old} -I${includedir_new}
I'm trying to compile the 'motemple.c' that comes with the opencv samples in Ubuntu 14.04 environment. I think that opencv has been properly installed; most of the programs are running as expected. However, when compiling the above mentioned file, an error "undefined reference" is raised for several functions. Below is the output of the compilation:
Building target: ACTION_detection1
Invoking: Cross G++ Linker
g++ -L/usr/local/lib -o "ACTION_detection1" ./opencvexample/MotionSegmentation.o -lopencv_core -lopencv_photo -lopencv_videoio -lopencv_videostab -lopencv_imgcodecs -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
./opencvexample/MotionSegmentation.o: In function `update_mhi':
/media/thanuja/DTR_DATA/ACTION-TV/ACTIONDETECTION/Implementations/ACTION_detection1/Debug/../opencvexample/MotionSegmentation.cpp:103: undefined reference to `cvUpdateMotionHistory'
/media/thanuja/DTR_DATA/ACTION-TV/ACTIONDETECTION/Implementations/ACTION_detection1/Debug/../opencvexample/MotionSegmentation.cpp:105: undefined reference to `cvUpdateMotionHistory'
/media/thanuja/DTR_DATA/ACTION-TV/ACTIONDETECTION/Implementations/ACTION_detection1/Debug/../opencvexample/MotionSegmentation.cpp:114: undefined reference to `cvCalcMotionGradient'
/media/thanuja/DTR_DATA/ACTION-TV/ACTIONDETECTION/Implementations/ACTION_detection1/Debug/../opencvexample/MotionSegmentation.cpp:123: undefined reference to `cvSegmentMotion'
/media/thanuja/DTR_DATA/ACTION-TV/ACTIONDETECTION/Implementations/ACTION_detection1/Debug/../opencvexample/MotionSegmentation.cpp:149: undefined reference to `cvCalcGlobalOrientation'
collect2: error: ld returned 1 exit status
make: *** [ACTION_detection1] Error 1
As it can be seen, I have linked all the opencv libraries to mu Eclipse project. I also tried the command line solution provided here, but still get the same error.
they have been replaced to the opencv_contrib from the main repository. You can find them in the optflow module.
Notice: cvUpdateMotionHistory, cvCalcMotionGradient, etc. is the name of OpenCV 1.x API functions, they are now obsoleted/outdated so try to avoid them by replacing to cv::updateMotionHistory, cv::calcMotionGradient, etc.