本人软件版本
cmake 3.28.64: https://cmake.org/download/
msys2 20240113: https://www.msys2.org/
vscode 1.87.2:https://code.visualstudio.com/Download
opencv 4.5.4: https://github.com/opencv/opencv
opencv_contrib 4.5.4: https://github.com/opencv/opencv_contrib
安装cmake时,记得勾选一下添加环境变量。opencv 与 opencv_contrib库要保持相同的版本。
msys2相关
pacman -S mingw-w64-ucrt-x86_64-gcc
pacman -S mingw-w64-ucrt-x86_64-gdb
pacman -S mingw-w64-ucrt-x86_64-toolchain // ucrt下 生成make等工具
pacman -S mingw-w64-x86_64-toolchain // 在mingw64下 但是你别用这个,不然到时候vscode可能会出问题。
需将 xx\msys2\ucrt64\bin 添加到环境变量。然后测试一下gcc --version。
raw.githubusercontent.com // Failed connect to raw.githubusercontent.com:443; 拒绝连接 // 修改host 添加ip和域名
configuration 时,须安装了make才可以。
CMAKE_BUILD_TYPE 设为 release OPENCV_GENERATE_PKGCONFIG 设为 true BUILD_opencv_world 设为 true OPENCV_EXTRA_MODULES_PATH 设为附加模组的modules文件夹路径
generate
mingw32-make -jx [*x为线程数*] 会遇到一个问题: build/3rdparty/ade/ade-0.1.1f/sources/ade/include/ade/typed_graph.hpp:101:10: error: 'uintptr_t' in namespace 'std' does not name a type 101 | std::uintptr_t m_srcGraph; | ^~~~~~~~~ build/3rdparty/ade/ade-0.1.1f/sources/ade/include/ade/typed_graph.hpp:101:10: error: 'uintptr_t' in namespace 'std' does not name a type 101 | std::uintptr_t m_srcGraph; | ^~~~~~~~~ #include <cstdint> // 加上这个
配置环境变量
OpenCV_DIR -- 你的路径
path中新增
%OpenCV_DIR%\include\opencv2 %OpenCV_DIR%\x64\mingw\bin
VScode相关
需安装扩展
C/C++ C/C++ Extension Pack CMake CMake Tool
可直接使用示例代码进行测试:
xx\opencv-4.5.4\samples\cpp\example_cmake
自己开发时直接编写一个CMakeLists.txt 大概结构如下:
cmake_minimum_required(VERSION 3.5) project(opencv_example_project) include_directories(${OpenCV_INCLUDE_DIRS}) find_package(OpenCV REQUIRED) add_executable(opencv_example main.cpp) // 这个main.cpp改成自己的主函数文件 target_link_libraries(opencv_example PRIVATE ${OpenCV_LIBS})
然后通过左下的 CMake Tool 插件运行/编译。未出现的话可以通过 ctrl+shift+p 搜索cmake配置来刷新。
文章评论