我正在尝试从OpenCV构建一个示例。不幸的是,我总是在构建时遇到这些错误。
我使用opencv_contrib进行了安装。 OpenCV安装在~/lib/pokus/installed
(包含bin include lib share
)中。我正在建立示例~/lib/pokus/installed/share/OpenCV/samples/cpp/opencv_version.cpp
(OpenCV附带)。
我的g++
命令:
g++ $(pkg-config --cflags --libs ~/lib/pokus/installed/lib/pkgconfig/opencv.pc) opencv_version.cpp -o test
结果:
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
~/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}
很抱歉这么常见的问题,但其他人似乎都没有解决我的问题。 感谢所有提示!
答案 0 :(得分:1)
文件名必须在参数之前:
g++ opencv_version.cpp $(pkg-config --cflags --libs ~/lib/pokus/installed/lib/pkgconfig/opencv.pc) -o test
答案 1 :(得分:1)
安装后尝试使用OPENCV 4.3.0时遇到了同样的问题。为了添加库并正确编译。
为我工作是为了测试opencv_version.cpp
g++ opencv_version.cpp $(pkg-config --cflags --libs opencv4) -o opencv_version
结果
./opencv_version
Welcome to OpenCV 4.3.0