c++ - 如何使用 CMake 简单地将 opencv 包含在我的项目中
问题描述
我想在我的项目文件夹中使用 opencv,因为我的其他同事并非都在同一个平台(Linux、Windows)上工作。因此,为了更容易克隆和编译项目,我只想制作一个克隆 git 并启动编译以便能够在项目上工作。(不使用任何环境变量,完全独立)
与其他库 (glfw) 一样,我只是在依赖项文件夹中添加了解压缩的文件夹。然后我添加了 add_subdirectory 和 target_link_libraries 语句。
然后我得到一个我无法解决的小错误。
下面是关于我的项目的信息
1.项目文件夹结构
project
│ README.md
| CMakeLists.txt (my CMakeLists project)
└───dependencies
│ └───opencv-4.1.2
│ │ CMakeLists.txt (the original opencv CMakeLists (untouched))
│ │ ...
└───src
│ main.cpp
2. 我的 CMakeLists 内容
cmake_minimum_required(VERSION 3.15)
project(OpTests)
set(CMAKE_CXX_STANDARD 17)
add_subdirectory(dependencies/opencv-4.1.2)
add_executable(OpTests src/main.cpp)
target_link_libraries(OpTests opencv_core opencv_highgui opencv_imgproc opencv_videoio)
3. main.cpp 内容
#include <iostream>
#include <opencv2/opencv.hpp>
int main() {
std::cout << CV_VERSION << std::endl;
cv::Mat M(2,2, CV_8UC3, cv::Scalar(0,0,255));
std::cout << "M = " << std::endl << " " << M << std::endl << std::endl;
std::cout << "Hello, World!" << std::endl;
return 0;
}
4.错误
°
°
°
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jeremy/CLionProjects/OpTests/cmake-build-debug
[ 3%] Built target ittnotify
[ 9%] Built target ippiw
[ 18%] Built target libjasper
[ 45%] Built target libwebp
[ 69%] Built target opencv_core
[ 90%] Built target opencv_imgproc
[ 93%] Built target opencv_imgcodecs
[ 96%] Built target opencv_videoio
[100%] Built target opencv_highgui
Scanning dependencies of target OpTests
[100%] Linking CXX executable bin/OpTests
CMakeFiles/OpTests.dir/src/main.cpp.o: In function `cv::String::~String()':
/usr/include/opencv2/core/cvstd.hpp:664: undefined reference to `cv::String::deallocate()'
CMakeFiles/OpTests.dir/src/main.cpp.o: In function `cv::String::operator=(cv::String const&)':
/usr/include/opencv2/core/cvstd.hpp:672: undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status
CMakeFiles/OpTests.dir/build.make:88: recipe for target 'bin/OpTests' failed
make[3]: *** [bin/OpTests] Error 1
CMakeFiles/Makefile2:81: recipe for target 'CMakeFiles/OpTests.dir/all' failed
make[2]: *** [CMakeFiles/OpTests.dir/all] Error 2
CMakeFiles/Makefile2:88: recipe for target 'CMakeFiles/OpTests.dir/rule' failed
make[1]: *** [CMakeFiles/OpTests.dir/rule] Error 2
Makefile:186: recipe for target 'OpTests' failed
make: *** [OpTests] Error 2
所以3个问题:
我做错了什么?是否可以在没有 100 行 CMakeLists 的情况下简单地修复它?
这是一个不好的做法吗?
先感谢您
美好的一天
解决方案
据我所知,首选的方法是安装 OpenCV 然后需要它。
因此克隆 OpenCV 的 repo,构建并安装它:
git clone git@github.com:opencv/opencv.git
cd opencv
make -j8
sudo make install
这也可以通过使用包管理器中的预构建包来实现......
然后您可以通过以下方式在 cmake 中使用该库:
cmake_minimum_required(VERSION 3.15)
project(OpTests)
find_package(OpenCV 4.2 REQUIRED)
add_executable(OpTests src/main.cpp)
target_link_libraries(OpTests PRIVATE
${OpenCV_LIBS}
)
target_compile_features(OpTests PRIVATE cxx_std_17)
请注意,可以使用该库来要求该库,并使用该命令find_package
将其添加为依赖项。target_link_libraries
另请注意,您应该使用target_compile_features
. 为了更好地了解 cmake,您可以参考 Daniel Pfeifer 的演讲https://www.youtube.com/watch?v=bsXLMQ6WgIk。
推荐阅读
- java - 调用 Java 方法,从 C 中获取 String 参数
- azure - 如何检查逻辑应用终结点的响应
- postgresql - 出现错误时会发生什么:psql 中不存在角色“foo”?
- c# - 在 Cloudwatch 日志 C# .Net 应用程序的 NLog 配置中配置日志流时间戳
- python - 带有符号列表及其自身出现的爆炸数据集
- python - 3D 绘制所有可能的排列或 0-9 的可能组合
- javascript - 如何将 Adobe Flash 游戏移植到 JavaScript?
- python - 使用 tkinter 在选项卡中显示不同的 csv 文件
- python - BeautifulSoup XML 解析 - 只返回第一个结果
- c - C语言:如何使字符数组由文本(.txt)文件字母组成?