Win10 + VSCode + OpenCV4.5.0 + CMake + MinGW-w64 配置
参考:
win10下VSCode配置opencv4.4.0(超详细教程,亲测有效)
1、VSCode+MinGW配置C++开发环境
https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/
配置环境变量
C:\Programs\mingw-w64\bin
2、安装CMake
配置环境变量
D:\tools\cmake-3.19.1-win64-x64\bin
3、下载OpenCV的sources源文件
https://github.com/opencv/opencv/releases
下载比较慢的话,可以换国内, OpenCV下载/OpenCV国内镜像/opencv_contrib下载
安装解压到目标路径,
在build \ x64下新建文件夹mingw。
4、使用 CMake 生成 OpenCV 的 Makefiles
运行cmake-gui,在bin下。
选择源地址,目标地址,
点击configure,选择,
点击next,选择刚才安装的MinGW的编译工具,
点击finish会开始执行,过程较慢,最后显示Configure done,是正常的。
然后点击Generate,正常会显示Generate Done。
5、编译opencv
运行cmd,cd到mingw文件夹,
执行minGW32-make命令,过程很慢,可使用多线程minGW32-make -j 4 命令,根据自己机器情况开几个线程,
我遇到的错误是找不到 vs_version.rc.obj 文件,学习OpenCV3:MinGW编译OpenCV到vs_version.rc.obj处出错。
打开build.make文件,找到1494行,显示在modules/core/CMakeFiles/opencv_core.dir 下找不到 vs_version.rc.obj。
新打开一个cmd窗口,cd到core目录下 下,运行命令,可在第1494行复制,
手动生成vs_version.rc.obj,并重新运行mingw32-make。
编译完成后运行minGW32-make install完成装载,生成Install文件夹。
配置环境变量:
6、VSCode配置
.vscode\c_cpp_properties.json。部分添加代码。
{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}", "D:/tools/opencv/build/x64/mingw/install/include", "D:/tools/opencv/build/x64/mingw/install/include/opencv2" ],
.vscode\tasks.json。部分添加代码。
{ "version": "2.0.0", "tasks": [ { ..."args": [ "-g", "${file}", "-o", "${workspaceRoot}/zzz_output_exe_files/${fileBasenameNoExtension}.exe", // 集中存放
"-I", "D:/tools/opencv/build/x64/mingw/install/include", "-I", "D:/tools/opencv/build/x64/mingw/install/include/opencv2", "-L", "D:/tools/opencv/build/x64/mingw/lib", "-l", "libopencv_calib3d450", // 后三位数字为opencv对应版本4.5.0 "-l", "libopencv_core450", "-l", "libopencv_dnn450", "-l", "libopencv_features2d450", "-l", "libopencv_flann450", "-l", "libopencv_gapi450", "-l", "libopencv_highgui450", "-l", "libopencv_imgcodecs450", "-l", "libopencv_imgproc450", "-l", "libopencv_ml450", "-l", "libopencv_objdetect450", "-l", "libopencv_photo450", "-l", "libopencv_stitching450", "-l", "libopencv_video450",
"-l", "libopencv_videoio450", // 不加这个处理视频时VideoCapture会报错 ],
...
测试文件test.cpp,
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; int main() { Mat img=imread("images/man.jpg"); // 图片放到工程目录下,与.vscode同一目录 imshow("image",img); waitKey(0);
return 0; }
工程目录下新建images文件夹,放一张图片,运行可打开图片,完成。