到目前为止,我已经下载了OpenCV-3.2.0并进行了编译和安装。我正在使用CMake编译OpenCV,因此我更改了我的CMakeLists.txt文件:
cmake_minimum_required (VERSION 3.2)
project(io)
find_package(OpenCV REQUIRED)
include_directories("/home/ubuntu/opencv-3.2.0/include") # directory of OpenCV-3.2.0
link_directories("/home/ubuntu/opencv-3.2.0/lib") # directory of OpenCV-3.2.0
add_executable(cv_io io.cpp)
target_link_libraries(cv_io ${OpenCV_LIBS})
现在,当我运行这段小代码时,
#include <iostream>
#include "opencv2/core/version.hpp"
int main(int argc, char ** argv)
{
std::cout << "OpenCV version: "
<< CV_MAJOR_VERSION << "."
<< CV_MINOR_VERSION << "."
<< CV_SUBMINOR_VERSION
<< std::endl;
return 0;
}
我得到的是
OpenCV版本:3.2.0
而不是
OpenCV版本:2.4.13
所以一切似乎都很正常,除了当我开始运行一些实际的OpenCV函数,例如:
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
int main()
{
cv::Mat img = cv::imread("ferrari.jpg");
cv::Mat dst;
cv::Sobel(img, dst, CV_32F, 1, 1);
cv::imwrite("ferrari_sobel.png", dst);
cv::VideoCapture input(0);
}
我遇到了所有这些未定义引用错误:
CMakeFiles/cv_io.dir/io.cpp.o: 在函数main' 中:
io.cpp:(.text+0x40): 对 cv::imread(cv::String const&, int) 未定义的引用
io.cpp:(.text+0xd4): 对 cv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&) 未定义的引用
CMakeFiles/cv_io.dir/io.cpp.o: 在函数 cv::String::String(char const*):
io.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x40): 对 cv::String::allocate(unsigned long) 未定义的引用
CMakeFiles/cv_io.dir/io.cpp.o: 在函数 cv::String::~String():
io.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x10): 对 cv::String::deallocate() 未定义的引用
CMakeFiles/cv_io.dir/io.cpp.o: 在函数 cv::String::operator=(cv::String const&):
io.cpp:(.text.ZN2cv6StringaSERKS0[ZN2cv6StringaSERKS0]+0x2c): 对 cv::String::deallocate() 未定义的引用
collect2: error: ld 返回 1 退出状态
CMakeFiles/cv_io.dir/build.make:121: 目标 'cv_io' 失败
make2: *** [cv_io] 错误 1
CMakeFiles/Makefile2:67: 目标 'CMakeFiles/cv_io.dir/all' 失败
make1: *** [CMakeFiles/cv_io.dir/all] 错误 2
Makefile:83: 目标 'all' 失败
make: *** [all] 错误 2
有人知道如何解决这个问题吗?我认为问题在于我仍然没有正确地在CMakeLists.txt
中链接所有库。此外,我发现本文下面的评论提到了类似于我正在经历的问题,但我不理解包含解决方案的页面它所指的内容。我对OpenCV和CMake非常陌生,希望您能提供尽可能明确的说明。我已经卡在这里很久了,所以任何帮助都将不胜感激!非常感谢!