如何在添加文件的情况下在VSCode中调试C++程序

How to debug C++ program in VSCode with file added

本文关键字:调试 C++ VSCode 程序 添加 文件 情况下      更新时间:2023-10-16

我可以在不添加文件的情况下进行调试,但是,我不知道如何处理添加的文件。

命令如下:

g++ -g --std=c++11 lab1.cpp -o lab1

./lab1 ./tests/extracredit-test-0.txt

以下是我的task.jsonlaunch.json.我想知道如何修改它以支持添加文件的调试。

启动.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": [
{
"preLaunchTask": "build active file",
"name": "CodeLLDB",
"type": "lldb",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
},
{
"preLaunchTask": "build active file",
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
},
{
"name": "GDB",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"cwd": "${workspaceFolder}",
"MIMode": "gdb"
}
]
}

任务.json

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team        
// ${file}: the current opened file                     
// ${fileBasename}: the current opened file's basename 
// ${fileDirname}: the current opened file's dirname    
// ${fileExtname}: the current opened file's extension  
// ${cwd}: the current working directory of the spawned process
{
"version": "2.0.0",
"tasks": [
{
"label": "build active file",
"type": "shell",
"command": "g++ --std=c++11 ${file} -g -o  ${fileDirname}/${fileBasenameNoExtension}",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"osx": {
"command": "g++ --std=c++11 ${file} -g -o  ${fileDirname}/${fileBasenameNoExtension}",
},
"windows": {
"command": "g++",
"args": [
"-ggdb",
""${file}"",
"--std=c++11",
"-o",
""${fileDirname}\${fileBasenameNoExtension}""
]
}
}
]
}

如何在添加文件的情况下在VSCode中调试C++程序

您正在使用 GCC。考虑升级到 GCC 10。然后使用g++ -Wall -Wextra -g(所有警告和 DWARF 调试信息(进行编译并使用 GDB 调试器。您可能会对GCC 10中引入的静态分析工具和/或Frama-C或Clang Static Analyzer等工具感兴趣。

阅读 GCC、VSCode 和您最喜欢的调试器的文档。

另见本报告草稿。

如果你编译了一个大软件(例如一百万行C++(,你可能也会对通过构建自动化工具(如ninja或GNU make(将GCC与ccache或冰淇淋或GCC预编译头文件相结合感兴趣。在某些情况下,您可能会对某些C++代码生成器(如SWIG或ANTLR(感兴趣。在某些项目中,您将编写自己的C++代码生成器(例如使用Guile,GPP,GNU gawk,Python或m4(。请注意,GCC 在内部使用多个C++代码生成器,并接受插件。

在几个开源项目的源代码中寻找灵感,如Qt,Wt,Clang,libbacktrace或FLTK。你也可以对LinuxFromScratch感兴趣。

这很简单,在这个代码片段中

"configurations": [
{
"preLaunchTask": "build active file",
"name": "CodeLLDB",
"type": "lldb",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
},

只需添加像网络这样的文件.txt在 args 中,在这种情况下,就像

"args": ["network.txt"]

这就完成了!