【发布时间】:2018-01-18 17:36:21
【问题描述】:
我的电脑正在运行 Ubuntu-16.04-LTS,并且已经安装了 OpenCV-2.4.13。但是,我想在不删除旧版本的情况下使用 OpenCV 较新版本的功能,例如 OpenCV-3.2.0。
到目前为止,我已经下载了 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): undefined reference tocv::imread(cv::String const&, int)' io.cpp:(.text+0xd4): undefined reference tocv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)' CMakeFiles/cv_io.dir/io.cpp.o: In functioncv::String::String(char 常量*)': io.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x40): 对cv::String::allocate(unsigned long)' CMakeFiles/cv_io.dir/io.cpp.o: In functioncv::String::~String()' 的未定义引用: io.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x10): 未定义 参考cv::String::deallocate()' CMakeFiles/cv_io.dir/io.cpp.o: In functioncv::String::operator=(cv::String const&)': io.cpp:(.text.ZN2cv6StringaSERKS0[ZN2cv6StringaSERKS0]+0x2c): 未定义对 `cv::String::deallocate()' collect2 的引用:错误:ld 返回 1 个退出状态 CMakeFiles/cv_io.dir/build.make:121: recipe for 目标 'cv_io' 失败 make2: * [cv_io] 错误 1 CMakeFiles/Makefile2:67:目标“CMakeFiles/cv_io.dir/all”的配方 失败 make1: * [CMakeFiles/cv_io.dir/all] 错误 2 Makefile:83: 目标“所有”的配方失败 make: *** [all] 错误 2
有谁知道如何解决这个问题?我认为问题在于我仍然没有在CMakeLists.txt 中正确链接所有库。另外,我发现a comment below this article 提到了与我正在经历的事情类似的事情,但我不明白the page containing the solution 它指的是什么。 我对 OpenCV 和 CMake 非常陌生,因此希望您能向我提供尽可能明确的说明。我一直坚持这一点,所以非常感谢任何帮助!非常感谢!
【问题讨论】:
-
配置两个 OpenCV 版本的痛苦。我感觉到你了。
-
如果您有兴趣,可以在 Docker 容器上运行两个版本。在不影响系统范围的包的情况下配置多个版本会容易得多
-
@Srinivas 虽然这似乎有点矫枉过正和沉重。