我怎样才能简单地将opencv 包含在我的项目中与CMake
How can I simply include opencv INSIDE my project with CMake
我想在我的项目文件夹中使用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. 我的CMake列表内容
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. 主要内容.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 行 CMakeList 的情况下简单地修复它?
这是一种不好的做法吗?
提前谢谢你
美好的一天
据我所知,首选方法是安装 OpenCV,然后需要它。
因此,克隆OpenCV的存储库,构建并安装它:
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
设置 c++ 版本。 为了更好地了解cmake,您可以参考Daniel Pfeifer https://www.youtube.com/watch?v=bsXLMQ6WgIk 的演讲。
如果你使用的是 linux(我在 Ubuntu 22.04 上尝试过(,你可以用更简单的方式做到这一点。
- 转到终端并安装 OpenCV
sudo apt-get install libopencv-dev
- 在cmake中使用类似于@Arwed梅特指示:
cmake_minimum_required(VERSION 3.15)
project(OpTests)
find_package(OpenCV REQUIRED)
add_executable(OpTests src/main.cpp)
target_link_libraries(OpTests PRIVATE
${OpenCV_LIBS}
)
#target_compile_features(OpTests PRIVATE cxx_std_17)
它应该有效。
相关文章:
- 即使我没有包含多个文件,C++中的多个定义错误
- 如果第一个元素包含任何零,则我的程序以不希望的方式运行
- 包含文件 <sched_yield.h>在我的 RHEL 5 上找不到
- 我怎样才能简单地将opencv 包含在我的项目中与CMake
- 为什么我的结构在包含字符串时崩溃?
- 为什么我的代码包含错误分段错误(核心转储)?
- 我的项目中某些仅标头文件不能包含使用 CMake 和 Vscode 的"SFML/Graphics.hpp"
- 我该如何循环遍历我的数组(缓冲区——包含一个文本文件),并将其打印成30字节的块
- 当我在结构中包含多个数组时,我的程序会跳过一堆代码
- SFML c ++ 当包含文件内游戏中发生事件时,如何使用铃声函数为我的游戏创建声音类?
- 在我的 CLR 项目中添加包含会导致无法在启用 /clr 的情况下编译 WLR
- 我收到未声明的标识符错误,但我已包含头文件?
- 这是我的 .h 文件的一些错误,当我在其中包含我的类模板时,它显示 [错误] 未终止 #ifndef
- 我的 RichEdit 控件是否可以包含可单击的链接?
- SQLite - 从源代码构建库或将源代码直接包含在我的项目中
- 如何在我的 cmake 项目中包含另一个 cmake 项目的头文件?
- 通过包含<iostream>头文件,它的所有 8 个函数是写入我的代码,还是只写入我在程序中使用的函数?
- Eclipse 没有从单独的文件夹中看到我的 c++ 包含
- Eclipse在退出和/或启动(CDT)时编辑我的包含路径
- 编译器不知道我的包含文件