# Camke 安装
- Camke 官网下载页面下载 exe 安装包
- 点击安装,一路 next 就行,推荐安装路径为默认
- 找到 Cmake 安装路径下的 bin 文件路径,添加到环境变量 Path, 比如我的文件路径就是 C:\Program Files\CMake\bin
- 在 windows terminal 使用 Cmake --version 来验证是否安装成功
# MinGW 安装
MinGW 下载不推荐使用官网自带的 exe 文件,老是抽风,还是直接下载离线包吧
- MinGW 官网下载页面下载 x86_64-posix-seh, 注意,必须是 posix 多线程版,否则后面 openCV 编译会出错
- 安装路径自己选
- 找到 MinGW 安装路径下的 bin 文件路径,添加到环境变量 Path, 比如我的文件路径就是 C:\MinGW_64\x86_64-8.1.0-posix-seh-rt_v6-rev0\bin
- 在 windows terminal 使用 gcc --version 或 [g++ --version] 来验证是否安装成功
# OpenCV 下载与编译
openCV 官网下的 windows 版本是针对 VS 的,所以想用 Cmake+MinGW+OpenCV 直接下源代码就行,不想折腾还是直接用 VS 吧
- OpenCV 官网下载页面下载源代码,安装路径自己选
- 在源文件根目录内新建两个空文件夹 mingw-build 和 build, 其中 mingw-build 用于存放 MakeFiles,build 用于存放编译后的 opencv
- 在源文件根目录下打开 Windows terminal, 依次执行下面的命令来编译 openCV
| mkdir mingw-build |
| mkdir build |
| |
| |
| cd mingw-build |
| cmake -G "MinGW Makefiles" C:\opencv-4.5.3 -DCMAKE_INSTALL_PREFIX=C:\opencv-4.5.3\build -DEBUILD_TESTS=OFF -DBUILD_PREF_TESTS=OFF |
| |
| |
| mingw32-make install -j8 |
- 把编译好的 build 的 bin 和 lib 文件夹添加到环境变量中,以便编译时能找到相应文件,比如我的就是 C:\LIBRARY\opencv-4.6.0\build\x64\mingw\bin 和 C:\LIBRARY\opencv-4.6.0\build\x64\mingw\lib
# 测试
- 源文件
| |
| #include<iostream> |
| #include<opencv2/opencv.hpp> |
| |
| int main() |
| { |
| cv::Mat img = cv::imread("C:/STORAGE/Pictures/screen/122.jpg"); |
| cv::imshow("img",img); |
| std::cout<<"conifgure"<<std::endl; |
| cv::waitKey(0); |
| return 0; |
| } |
- CmakeLists.txt 文件
| cmake_minimum_required(VERSION 3.12) |
| |
| set(OpenCV_DIR "C:/LIBRARY/opencv-4.6.0/build") |
| |
| project(hello) |
| find_package(OpenCV 4.6.0 REQUIRED) |
| include_directories(${OpenCV_INCLUDE_DIRS}) |
| add_executable(hello main.cpp) |
| target_link_libraries( |
| hello |
| ${OpenCV_LIBS} |
| ) |
强烈建议不要忽视 set (OpenCV_DIR "C:/LIBRARY/opencv-4.6.0/build") 这句语句,否则你可能会多次遇到下面这个经典错误
| CMake Error at CMakeLists.txt:6 (find_package): |
| By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has |
| asked CMake to find a package configuration file provided by "OpenCV", but |
| CMake did not find one. |
| |
| Could not find a package configuration file provided by "OpenCV" (requested |
| version 4.6.0) with any of the following names: |
| |
| OpenCVConfig.cmake |
| opencv-config.cmake |
| |
| Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or |
| set "OpenCV_DIR" to a directory containing one of the above files. |
| If "OpenCV" provides a separate development package or SDK, be sure it has been installed. |
# 使用命令行运行
| |
| mkdir build |
| cd build |
| cmake -G "MinGW Makefiles" .. |
| mingw32-make |
| ./hello |
# 参考
opencv 配置有点坑