1 安装cmake
下载链接如下https://github.com/Kitware/CMake/releases/download/v3.27.7/cmake-3.27.7-windows-x86_64.zip
解压后放到指定目录后,添加bin目录到环境变量即可。
2 mingw安装
下载链接如下(下图的x86_64-posix-sjlj):
Download x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0.7z (MinGW-w64 - for 32 and 64 bit Windows)
解压后放到指定路径,并将bin目录添加到环境变量即可。
3 opencv安装
已经安装过的可以跳过此步!
win10使用mingw安装OpenCV4.8_opencv 4.8 mingw编译好-CSDN博客
4 在vscode配置opencv
先写一个测试代码test.cpp,内容示例如下:
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
cv::Mat image = cv::imread("path/to/your/image.jpg");//绝对路径
if (image.empty()) {
std::cerr << "Could not open or find the image!\n";
return -1;
}
cv::imshow("Display Image", image);
cv::waitKey(0); // 等待按键
return 0;
}
然后点击vscode左侧调试标识(小虫子),Run and Debug ->create a lauch.json file,接下来,选择C++ (GDB/LLDB)这个选项,配置好的launch.json如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: gdb Launch", // 配置名称,显示在调试配置选择器里
"type": "cppdbg", // 调试器类型,cppdbg是C/C++的调试器类型
"request": "launch", // 请求类型,表示启动调试器
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
//上面这个Debugger是我自己定义的,为了方便放置生成的exe文件
"args": [],
"stopAtEntry": false, //这里如果为 false,则说明调试直接运行。(反之则停止)
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,//是否调用外部cmd
"MIMode": "gdb",
"miDebuggerPath": "D:/Deps/mingw64/bin/gdb.exe",//自己进行设置
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "opencv4.8.0"
}
]
}
然后配置tasks.json,如下:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "D:/Deps/mingw64/bin/g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-ID:/Deps/opencv-4.8.0/build/include", // 替换为你的 OpenCV 头文件路径
"-LD:/Deps/opencv-4.8.0/build/x64/mingw/lib", // 替换为你的 OpenCV 库文件路径
"-lopencv_world480" // 注意版本号,可能是 opencv_world480.lib 或类似名称
],
"options": {
"cwd": "D:/Deps/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
配置c_cpp_properties.json,内容如下:
{
"configurations": [
{
"name": "win32",
"includePath": [
"${workspaceFolder}/**",
"D:/Deps/opencv-4.8.0/build/include", // 替换为你的 OpenCV 头文件路径
"D:/Deps/opencv-4.8.0/build/include/opencv2" // 替换为你的 OpenCV 头文件路径
],
"defines": [],
"compilerPath": "D:/Deps/mingw64/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
最后,点击调试或者运行,会生成对应的执行文件,需要提前将动态库libopencv_world480.dll拷贝到当前工作目录,如下图: