您的位置:首页 > 运维架构 > Linux

VSCode在Linux和Windows调试C/C++程序

2017-02-21 12:17 489 查看
VSCODE是一个文本编辑器,不具备编译功能,但是可以调用外部编译器调试器来实现调试功能

Linux 使用GDB,Windows 可以安装minGW,

写好测试文件main.cpp , 用vscode 打开代码所在文件夹,切换到调试界面,点击配置按钮



选择 C++(GDB/LLDB) for Windows or Linux
或者选择 C++(Windows) for Windows
弹出默认的launch.json文件,在此基础上修改



“program” : 要调试运行的程序,修改为程序名,或 {file}.o / {file}.exe
根据当前系统在"linux"或者“Windows” 分别添加 gdb的安装路径

"miDebuggerPath": "/usr/bin/gdb",



"miDebuggerPath": "D:/MinGW/bin/gdb.exe",

下面多余的 C++ Attach项可以整个删除,也可忽略,修改好后的launch.json文件如下

//launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${file}.o",
"args": [],
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "g++",
"linux": {
"miDebuggerPath": "/usr/bin/gdb",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"miDebuggerPath": "D:/MinGW/bin/gdb.exe",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
}
]
}


然后点击调试按钮,提示 没有配置task runner,点击配置





选择Others或任一个,弹出默认tasks.json文件,用下面的文件覆盖原来的内容

//tasks.json
{
"version": "0.1.0",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${file}.o"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}


command: gcc , g++
args: 编译参数

最后再次点调试按钮,即可成功运行调试,可以添加断点,查看变量,堆栈等常用调试功能
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: