目前大部分人都是用VS开发opencv,只有少部分人用VS Code开发,我一直觉得VS Code是个神器,所以我就决定将它搭建成一个全能的IDE,能够完成python,java,c++,opencv,html等开发
接下来就开始我们的搭建,搭建借鉴https://blog.csdn.net/zhaiax672/article/details/88971248(里面包括MinGW64,opencv,cmake的下载及配置链接),建议看完这个博客后不要直接安装,请往下接着看,这样让你少踩一些坑
当然安装过程不会是一帆风顺的,我按着上面的博客一步一步的搭建,在输入 minGW32-make
命令后,编译过程失败,大概都会卡在27%
,错误信息如下:
3rdparty\protobuf\CMakeFiles\libprotobuf.dir\build.make:412: recipe for target '3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/stubs/io_win32.cc.obj' failed
mingw32-make[2]: *** [3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/stubs/io_win32.cc.obj] Error 1
CMakeFiles\Makefile2:710: recipe for target '3rdparty/protobuf/CMakeFiles/libprotobuf.dir/all' failed
mingw32-make[1]: *** [3rdparty/protobuf/CMakeFiles/libprotobuf.dir/all] Error 2
后来经过查找资料后,发现是在采用cmake工具时没有勾选 ENABLE_CXX11 这个选项,
参考链接https://blog.csdn.net/asiwxy/article/details/80028825
避免了上面这个错误后应该就安装成功了(千万别忘了执行minGW32-make install),这时会在Where to build the binaries这个选项目录下生成编译后生成的库
如下图:
安装好环境后就是测试程序了,这个环节容易出现的问题就是程序写好了,头文件<opencv2/opencv.hpp>在VS Code中可以通过"转到定义处"查看,但是一编译就会报错提示找不到头文件,这个是由于上文那个配置链接中的测试部分写的有问题,下面我便把我的测试程序,以及3个.json文件的配置文件上传
这个就是我的程序源代码
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
int main()
{
Mat img=imread("C:/Users/Administrator/Pictures/dongma.jpg");
imshow("image",img);
waitKey();
return 0;
}
这个是我的launch.json文件
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "opencv3.4.5 debuge",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\MinGW64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "opencv3.4.5 compile task"
}
]
}
这个是我的c_cpp_properties.json文件
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/MinGW64/mingw64/x86_64-w64-mingw32/include/c++/",
"D:/opencv3.4.5/opencv/build/x64/buildByMinGW/install/include",
"D:/opencv3.4.5/opencv/build/x64/buildByMinGW/install/include/opencv",
"D:/opencv3.4.5/opencv/build/x64/buildByMinGW/install/include/opencv2"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:\\MinGW64\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
这个是我的tasks.json文件
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "opencv3.4.5 compile task",
"command": "D:\\MinGW64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${workspaceFolder}\\${fileBasenameNoExtension}.exe",
"-I",
"D:/opencv3.4.5/opencv/build/x64/buildByMinGW/install/include",
"-I",
"D:/opencv3.4.5/opencv/build/x64/buildByMinGW/install/include/opencv",
"-I",
"D:/opencv3.4.5/opencv/build/x64/buildByMinGW/install/include/opencv2",
"-L",
"D:/opencv3.4.5/opencv/build/x64/buildByMinGW/install/x64/mingw/bin/lib*"
],
"options": {
"cwd": "D:\\MinGW64\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
最后,编译成功,如下图
接下来我们执行生成的exe文件,效果如下图
关于json文件中的问题,如果有不懂的,请发邮件或者加我qq1329931228,我看到就会解答,另外补充一个关于文件宏定义的说明链接https://code.visualstudio.com/docs/editor/variables-reference
来源:CSDN
作者:菜鸟boy
链接:https://blog.csdn.net/qq_37120369/article/details/103906108