无法链接opencv库

时间:2017-02-04 06:22:18

标签: c opencv raspberry-pi

我刚刚开始使用OpenCV。我有一个Raspberry Pi和Pi相机,我想学习OpenCV用于机器视觉项目。我正在编写使用vi编辑的C代码,在Raspberry Pi上。我对Linux很新,对于在文本编辑器而不是IDE中编写代码肯定是新手!

我能够从Github下载OpenCV repo并编译它而不会有太多麻烦。安装了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中排除链接错误的过程是什么? 您可以提供的任何见解都将受到赞赏!

1 个答案:

答案 0 :(得分:0)

一些事情......

  • 您在OpenCVV_LIBS

  • 中输错了CMakeLists.txt
  • 我认为您还需要include_directories(${OpenCV_INCLUDE_DIR})

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