1.下载安装MinGW
https://osdn.net/projects/mingw/releases/
2.配置g++到环境变量中
添加到windows环境变量
3.调试方式
3.1 不修改json,直接debug
1.打开Cpp文件的界面的前提下,F5 选 C++ GDB
2.选 g++
3.关闭laucher.json, 继续按 F5调试即可
3.2 创建json
1. c_cpp_properties.json 用于配置头文件所在的文件夹
2. task.json用于 编译cpp文件
手动修改 “args”: 要编译文件列表
3. lauch.json用于 gdb去debug cpp文件
手动修改gdb的全路径
实例:
c_cpp_properties.json
{
"configurations": [
{
"name": "MinGW",
"intelliSenseMode": "clang-x64",
"compilerPath": "C:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"includePath": [
"${workspaceFolder}",
"E:/project/github/OpenCV-MinGW-Build/include/"
],
"defines": [],
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
task.json
{
"version": "2.0.0",
"command": "g++",
"args": ["-g",
"${file}",
"${workspaceRoot}\\b.cpp",
"${workspaceRoot}\\c.cpp",
"-I", "E:/project/github/OpenCV-MinGW-Build/include",
"-I", "E:/project/github/OpenCV-MinGW-Build/include/opencv",
"-I", "E:/project/github/OpenCV-MinGW-Build/include/opencv2",
"-L", "E:/project/github/OpenCV-MinGW-Build/x64/mingw/bin/lib*",
"-o",
"${fileBasenameNoExtension}.exe"], // 编译命令参数
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
lauch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceRoot}",// 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录
"environment": [],
"externalConsole": false,// 调试时是否显示控制台窗口,一般设置为true显示控制台
"MIMode": "gdb",
"miDebuggerPath": "D:\\bin\\mingw-w64-new\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",// miDebugger的路径,注意这里要与MinGw的路径对应
"preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
注意:
gdb时,工程路径中不能有中文路径。