我怎样才能简单地将opencv 包含在我的项目中与CMake

How can I simply include opencv INSIDE my project with CMake

本文关键字:我的 包含 项目 CMake opencv 简单      更新时间:2023-10-16

我想在我的项目文件夹中使用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 上尝试过(,你可以用更简单的方式做到这一点。

  1. 转到终端并安装 OpenCVsudo apt-get install libopencv-dev
  2. 在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)

它应该有效。

相关文章: