我想编译一个静态使用openCV的C ++程序,这样我就可以在其他计算机上使用它,而无需安装openCV。
我使用以下标志编译openCV 2.4.5以获取静态库并在我的主要安装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
安装完成后,我尝试编译一个用于面部检测的c ++程序,如下所示:
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
由此我得到了错误:
/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
我使用了与我的OpenCV 3.1安装相同的库标志。我从他们那里得到了它们:
pkg-config --cflags --libs opencv
-L和-I标志指向/ opt目录中的openCV 2.4.5安装。
为什么不编译?所有OpenCV库都列在静态库所在的部分中。
答案 0 :(得分:1)
在OpenCV 2中,我认为cv::imread()
和cv::imwrite()
位于highgui
库中。在OpenCV 3中,他们被转移到imgcodecs
库。尝试添加:
-lopencv_highgui
到您的命令行。