如何使用 CMake 将 opencv 简单地包含在我的项目中

JGi*_*Gil 4 c++ opencv cmake

我想在我的项目文件夹中使用 opencv,因为我的其他同事并不都在同一平台(Linux、Windows)上工作。因此,为了更容易地克隆和编译项目,我只想克隆 git 并启动编译就可以在该项目上工作。(不使用任何环境变量,完全独立)

\n\n

与其他库(glfw)一样,我只是将解压缩的文件夹添加到我的依赖项文件夹中。然后我添加了 add_subdirectory 和 target_link_libraries 语句。

\n\n

然后我得到一个我无法解决的小错误。

\n\n

有关我的项目的信息如下

\n\n

1. 项目文件夹结构

\n\n
project\n\xe2\x94\x82   README.md\n|   CMakeLists.txt                (my CMakeLists project)    \n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80dependencies\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80opencv-4.1.2\n\xe2\x94\x82       \xe2\x94\x82   CMakeLists.txt        (the original opencv CMakeLists (untouched))\n\xe2\x94\x82       \xe2\x94\x82   ...\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80src\n    \xe2\x94\x82   main.cpp\n
Run Code Online (Sandbox Code Playgroud)\n\n

2.我的CMakeLists内容

\n\n
cmake_minimum_required(VERSION 3.15)\nproject(OpTests)\n\nset(CMAKE_CXX_STANDARD 17)\n\nadd_subdirectory(dependencies/opencv-4.1.2)\n\nadd_executable(OpTests src/main.cpp)\ntarget_link_libraries(OpTests opencv_core opencv_highgui opencv_imgproc opencv_videoio)\n
Run Code Online (Sandbox Code Playgroud)\n\n

3.main.cpp内容

\n\n
#include <iostream>\n#include <opencv2/opencv.hpp>\n\nint main() {\n    std::cout << CV_VERSION << std::endl;\n    cv::Mat M(2,2, CV_8UC3, cv::Scalar(0,0,255));\n    std::cout << "M = " << std::endl << " " << M << std::endl << std::endl;\n    std::cout << "Hello, World!" << std::endl;\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

4、错误

\n\n
\xc2\xb0\n\xc2\xb0\n\xc2\xb0\n-- \n-- Configuring done\n-- Generating done\n-- Build files have been written to: /home/jeremy/CLionProjects/OpTests/cmake-build-debug\n[  3%] Built target ittnotify\n[  9%] Built target ippiw\n[ 18%] Built target libjasper\n[ 45%] Built target libwebp\n[ 69%] Built target opencv_core\n[ 90%] Built target opencv_imgproc\n[ 93%] Built target opencv_imgcodecs\n[ 96%] Built target opencv_videoio\n[100%] Built target opencv_highgui\nScanning dependencies of target OpTests\n[100%] Linking CXX executable bin/OpTests\nCMakeFiles/OpTests.dir/src/main.cpp.o: In function `cv::String::~String()\':\n/usr/include/opencv2/core/cvstd.hpp:664: undefined reference to `cv::String::deallocate()\'\nCMakeFiles/OpTests.dir/src/main.cpp.o: In function `cv::String::operator=(cv::String const&)\':\n/usr/include/opencv2/core/cvstd.hpp:672: undefined reference to `cv::String::deallocate()\'\ncollect2: error: ld returned 1 exit status\nCMakeFiles/OpTests.dir/build.make:88: recipe for target \'bin/OpTests\' failed\nmake[3]: *** [bin/OpTests] Error 1\nCMakeFiles/Makefile2:81: recipe for target \'CMakeFiles/OpTests.dir/all\' failed\nmake[2]: *** [CMakeFiles/OpTests.dir/all] Error 2\nCMakeFiles/Makefile2:88: recipe for target \'CMakeFiles/OpTests.dir/rule\' failed\nmake[1]: *** [CMakeFiles/OpTests.dir/rule] Error 2\nMakefile:186: recipe for target \'OpTests\' failed\nmake: *** [OpTests] Error 2\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

所以3个问题:

\n\n

我做错了什么?\n是否可以在没有 100 行 CMakeLists 的情况下简单地修复它?

\n\n

这是一个不好的做法吗?

\n\n

先感谢您

\n\n

美好的一天

\n

Arw*_*ett 6

据我所知,首选方法是安装 OpenCV,然后需要它。

因此,克隆 OpenCV 的存储库,构建并安装它:

git clone git@github.com:opencv/opencv.git
cd opencv
make -j8
sudo make install
Run Code Online (Sandbox Code Playgroud)

这也可以通过使用包管理器中的预构建包来实现......

然后你可以通过以下方式在 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)
Run Code Online (Sandbox Code Playgroud)

请注意,可以使用find_package该命令要求该库并将其添加为依赖项target_link_libraries。另请注意,您应该使用target_compile_features. 为了更好地了解 cmake,您可以参考 Daniel Pfeifer 的演讲https://www.youtube.com/watch?v=bsXLMQ6WgIk