VSCode作为一个轻量级的编程软件,无论在界面、内存占用和速度等方面都完胜VS。但用vscode来写opencv代码需要自己编译OpenCV(如果是VS安装opencv则不需要自己编译opencv,因为在opencv的官网上就有编译好的文件),主要用到MinGW-w64和CMake工具。前前后后折腾了好久,终于配好了opencv的环境,写篇博客,一是为了怕自己忘了,又要重新学,二是分享一下自己的经验。主要参考两篇博客:
https://www.cnblogs.com/kensporger/archive/2020/02/19/12320622.html
https://blog.csdn.net/zhaiax672/article/details/88971248
1.安装MinGW-w64
MinGW-w64下载地址
https://github.com/huihut/OpenCV-MinGW-Build
(参考博客:https://www.cnblogs.com/kensporger/archive/2020/02/19/12320622.html)
配置环境变量
2.安装CMake
CMake下载地址:
https://cmake.org/download/
配置环境变量
记得重启电脑使3个环境变量生效
3.生成MakeFiles
需要去官网下载OpenCV的sources源文件,如果下载太慢,可以找找百度网盘,或者是国内镜像网站
https://www.bzblog.online/wordpress/index.php/2020/03/09/opencvdownload/
注意:是下载opencv对应的source源文件
打开CMake-gui.exe,点击configure,会出现一些问题,主要是会卡在下载ffmpge文件,需要改下host文件,这时只能耐心等待了。再次点击configure,完成后点generate。
Cmake下载文件时一次不成功,那就再来一次
4.编译opencv
CMD到MakeFiles所在文件夹(shift+右键,选择powershell打开),执行minGW32-make命令,或者使用多线程minGW32-make -j 4命令:
编译过程中可能会出现各种各样的错误,不要急,主要是返回第三步,去掉勾选,再次点击configure,generate。
好了,opencv编译成功,最后只剩下在VSCode中配置opencv了(筒子们,离最后的胜利不远了,坚持)
编译完成后记得输入minGW32-make install完成装载,这样才会生成Install文件夹。
5.VSCode配置
c_cpp_properties.json
{
"configurations": [
{
"name": "win",
"includePath": [
"${workspaceFolder}/**",
"D:\\OpenCV4.4.0\\build\\x64\\mingw\\install\\include",
"D:\\OpenCV4.4.0\\build\\x64\\mingw\\install\\include\\opencv2"
],
"defines": [],
"compilerPath": "C:/Users/chong/Downloads/mingw64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.o",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/Users/chong/Downloads/mingw64/bin/gdb.exe",
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
]
}
tasks.json
{
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileBasenameNoExtension}.o",
"-I", "D:\\OpenCV4.4.0\\build\\x64\\mingw\\install\\include",
"-I", "D:\\OpenCV4.4.0\\build\\x64\\mingw\\install\\include\\opencv2",
"-L", "D:\\OpenCV4.4.0\\build\\x64\\mingw\\lib",
"-l", "libopencv_calib3d440",
"-l", "libopencv_core440",
"-l", "libopencv_dnn440",
"-l", "libopencv_features2d440",
"-l", "libopencv_flann440",
"-l", "libopencv_gapi440",
"-l", "libopencv_highgui440",
"-l", "libopencv_imgcodecs440",
"-l", "libopencv_imgproc440",
"-l", "libopencv_ml440",
"-l", "libopencv_objdetect440",
"-l", "libopencv_photo440",
"-l", "libopencv_stitching440",
"-l", "libopencv_video440",
"-l", "libopencv_videoio440"
],// 编译命令参数
"problemMatcher":{
"owner": "cpp",
"fileLocation":[
"relative",
"${workspaceFolder}"
],
"pattern":[
{
"regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
"file": 1,
"location": 2,
"message": 3
}
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
配置成功,记得点赞哟!!!