c - 无法链接 opencv 库

标签 c opencv raspberry-pi

我刚刚开始使用 OpenCV。我有一个 Raspberry Pi 和 Pi 相机,我想学习 OpenCV 以用于机器视觉项目。我正在用 C 编写代码,在 Raspberry Pi 上使用 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")

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

这里是 make 命令的输出:

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 如果您能提供任何见解,我们将不胜感激!

最佳答案

一些事情...

  • 您在 CMakeLists.txt

    中输入了错误的 OpenCVV_LIBS
  • 我认为您还需要 include_directories(${OpenCV_INCLUDE_DIR})

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

关于c - 无法链接 opencv 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42037251/

相关文章:

python - 哪个python脚本正在运行

iphone - 如何计算c中的3D对象运动?

c++ - 如何使用opencv c++的聚类根据面积和高度对连通分量进行分类

python - 缩放图像不会产生明显的变化

networking - Raspberry Pi 上使用 batman-adv 协议(protocol)的无线网状网络

c - Pthread 屏障和树莓派

c - 从内核驱动程序中按名称获取 Windows NT 内核对象

c - 如何用 C 处理 PostgreSQL 中的数字数据类型?

c - 如何使用具有元素确切名称的变量访问结构的元素?

c++ - 取消定义已经包含的标题