【问题标题】:Trouble linking opencv libraries链接 opencv 库时出现问题
【发布时间】:2017-06-21 14:13:08
【问题描述】:

我刚刚开始使用 OpenCV。我有一个 Raspberry Pi 和 Pi 相机,我想学习 OpenCV 以在机器视觉项目中使用。我在 Raspberry Pi 上用 C 语言编写代码,使用 vi 进行编辑。我对 Linux 有点陌生,而且绝对是在文本编辑器而不是 IDE 中编写代码的新手!

我能够从 Github 下载 OpenCV 存储库并编译它而没有太多麻烦。已安装 OpenCV (3.2.0)。

我从这里的教程开始:http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html#linux-gcc-usage

我正确输入了代码,然后制作了CMake文件。

这是我的 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV COMPONENTS core imgproc highgui REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCCV_LIBS} )
LINK_DIRECTORIES("/usr/local/lib")

我尝试制作该项目,但似乎存在库链接错误。我是新手,无法确定是版本冲突、缺少库还是只是链接器在错误的位置查找库。

生成命令时的输出如下:

pi@raspberrypi:~/Programming $ cmake .
-- Found OpenCV: /usr/local (found version "3.2.0") found components:            core imgproc highgui 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/Programming
pi@raspberrypi:~/Programming $ make
[ 50%] Linking CXX executable DisplayImage
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `main':
DisplayImage.cpp:(.text+0x6c): undefined reference to   `cv::imread(cv::String const&, int)'
DisplayImage.cpp:(.text+0xd4): undefined reference to `cv::namedWindow(cv::String const&, int)'
DisplayImage.cpp:(.text+0x118): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
DisplayImage.cpp:(.text+0x138): undefined reference to `cv::waitKey(int)'
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `cv::String::String(char const*)':
DisplayImage.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x50):  undefined reference to `cv::String::allocate(unsigned int)'
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `cv::String::~String()':
DisplayImage.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14):  undefined reference to `cv::String::deallocate()'
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function  `cv::String::operator=(cv::String const&)':
DisplayImage.cpp:  (.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x30): undefined  reference to `cv::String::deallocate()'
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `cv::Mat::~Mat()':
DisplayImage.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined  reference to `cv::fastFree(void*)'
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `cv::Mat::operator=(cv::Mat const&)':
DisplayImage.cpp:(.text._ZN2cv3MataSERKS0_[_ZN2cv3MataSERKS0_]+0x140):  undefined reference to `cv::Mat::copySize(cv::Mat const&)'
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `cv::Mat::release()':
DisplayImage.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x68): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
CMakeFiles/DisplayImage.dir/build.make:94: recipe for target 'DisplayImage' failed
make[2]: *** [DisplayImage] Error 1
CMakeFiles/Makefile2:67: recipe for target  'CMakeFiles/DisplayImage.dir/all' failed
make[1]: *** [CMakeFiles/DisplayImage.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
pi@raspberrypi:~/Programming $ 

接下来呢?解决Linux中链接错误的过程是什么?/OpenCV 您能提供的任何见解将不胜感激!

【问题讨论】:

    标签: c opencv raspberry-pi


    【解决方案1】:

    一些事情......

    • 您在CMakeLists.txt 中输入了错误的OpenCVV_LIBS

    • 我想你也需要include_directories(${OpenCV_INCLUDE_DIR})

    • 我认为 out-of-source 构建是首选,这意味着您创建一个名为 build 的子目录不受版本控制,而是在包含 CMakeLists.txtcd build 的目录中然后cmake ..

    【讨论】:

    • 好的,我更正了 CMakeLists.txt 文件中的 OpenCV_LIBS,并按照您的建议创建了构建目录。对于 include_directories,我是按字面意思输入“OpenCV_INCLUDE_DIR”,还是表示 OpenCV 目录的路径?我想我不确定路径变量在 Linux/OpenCV 中是如何工作的。感谢您的所有帮助!
    猜你喜欢
    • 2020-01-24
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多