【发布时间】:2021-08-31 20:49:15
【问题描述】:
我最近一直在使用 GDB 调试 C++ 程序。对于标准用法,我通常这样做:
$ cd compiledir
$ compilescript
$ gdb compiled.out
$ run inputfile
compilescript 是一个为我正在开发的特定软件编译代码的程序,并且只在 compiledir 中工作。它读取编译器标志的外部文件。对于 gdb,我包括 neccisary -g 标志。这适用于我通过文本界面进行调试。但是,与 IDE 相比,这个文本界面的使用变得越来越令人沮丧,而且我知道 Visual Studio 默认使用 gdb 作为后端来调试 C++ 文件。
首先,我让 Visual Studio 生成默认的 C++ 调试配置并尝试更改命令,但我不知道它在做什么,而且似乎没有太多关于制作自定义构建/调试配置的文档,特别是对于 linux 版本的 VScode。
目前我有:
launch.json(默认)
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "Custom Build",
"command": "compilescript" ,
"options": {
"cwd": "compiledir"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compile using the custom build script."
}
],
"version": "2.0.0"
}
运行此任务以及通过 launch.json 文件间接运行它都失败了。我得到以下信息:
> Executing task: Custom Build<
Starting build...
它挂在那里。我尝试将 echo 放入构建命令中以查看它是否正在运行,或者让它创建一个文件。然而,就好像什么都没有运行,我在任何地方都看不到任何打印或创建的文件。
为 Visual Studio(Linux 版)创建自定义 GDB 构建/调试任务的正确方法是什么?
更新 我已经编辑了 tasks.json 和 launch.json,并且能够成功编译并使用我想要的输入文件运行 GDB。但是,环境变量配置不正确。我需要配置环境变量以正确运行输入文件。我目前有:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": compiled.out,
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [{"VAR1": "VAR1_VALUE", "VAR2": "VAR2_VALUE"}],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
但是,当我运行构建/调试任务时,它会构建,但不会调试,并且 GDB 会报错。似乎“环境”变量正在设置一些东西,但我收到以下错误:
Unable to start debugging. Unexpected GDB output from command "-interpreter-exec console "set env"". Argument required (environment variable and value).
【问题讨论】:
-
您没有在
launch.json中运行“自定义构建”任务。
标签: debugging visual-studio-code gdb