1.准备工作:安装g++或者gdb
sudo apt updatesudo apt install g++g++ --versionsudo apt install gdbgdb --version
2.配置环境
2.1在本地新建一个main.cpp
#include <iostream>
#include <vector>
#include <string>using namespace std;int main()
{vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};for (const string& word : msg){cout << word << " ";}cout << endl;
}
2.2调试,点击左边按钮,选择创建launch.json文件
注意:1.“program”表示需要调试的文件,需要和下面的cwd路径一致,下面设置为工作目录 ${fileDirname} 中的 ${fileBasenameNoExtension} ,
2.注意gdb路径:
"miDebuggerPath": "gdb", // 调试器路径,Windows下后缀不能省略,Linux下则去掉
3.“stopAtEntry”默认为false, 运行调试时,debugger不会在源文件中添加断点,设置为true时,调试会在main函数入口处等待。
将launch.json文件修改为如下:
{// 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示"type": "cppdbg", // 配置类型,这里只能为cppdbg"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)"program": "${fileDirname}/{fileBasenameNoExtension}", // 将要进行调试的程序的路径"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,我一般设置为true"cwd": "${fileDirname}", // 调试程序时的工作目录"environment": [], // (环境变量?)"externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台,// 但是最新版cpptools有BUG,具体请看文末的注意"internalConsoleOptions": "neverOpen", // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡,你应该不需要对gdb手动输命令吧?"MIMode": "gdb", // 指定连接的调试器,可以为gdb或lldb。但目前lldb在windows下没有预编译好的版本。"miDebuggerPath": "/usr/bin/gdb", // 调试器路径,Windows下后缀不能省略,Linux下则去掉"setupCommands": [ // 用处未知,模板如此{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": false}],}]
}
2.3编译,生成tasks.json文件
快捷键:ctrl+shift+b
根据自己的需求来修改,tasks.json是为整个目录配置环境,不需要修改诸如 “${fileDirname}” 这样的变量,“type”可以改成“shell”,不能任意; “label”是task的名称,可以随意,但要与launch.json文件中的变量“preLaunchTask”设置一致; “command” 来指定编译器名,可以不带绝对路径。变量参考详见官方文档
{"version": "2.0.0","tasks": [{"label": "gcc","command": "gcc","args": ["${file}","-o","${workspaceFolder}/main","-g","-static-libgcc","-std=c99"],"type": "shell","group": "build","presentation": {"echo": true,"reveal": "always","focus": true,"panel": "shared"},"problemMatcher": "$gcc"},{"type": "cppbuild","label": "g++ build","command": "g++","args": ["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}"],"options": {"cwd": "${fileDirname}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "调试器生成的任务。"}]
}
3.调试代码,显示结果
按F5进行单步调试
参考链接1:
https://www.cnblogs.com/booturbo/p/17379178.html#:~:text=%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AAlaunch.json%E6%96%87%E4%BB%B6%E6%9D%A5%E9%85%8D%E7%BD%AE%E8%B0%83%E8%AF%95%E7%8E%AF%E5%A2%83%EF%BC%8C%E9%80%89%E6%8B%A9Run%20--%3E%20Add%20Configuration%EF%BC%8C%E4%BC%9A%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E4%B8%80%E4%B8%AAlaunch.json%EF%BC%8C%E7%82%B9%E5%87%BB%E5%8F%B3%E4%B8%8B%E8%A7%92%E7%9A%84Add%20Configuration%E6%9D%A5%E9%80%89%E5%AE%9A%E8%A6%81%E5%8A%A0%E5%85%A5%E7%9A%84%E9%85%8D%E7%BD%AEC%2FC%2B%2B%3A%20%28gdb%29%20Launch%EF%BC%88VS,Code%E7%89%88%E6%9C%AC%E4%B8%8D%E5%90%8C%EF%BC%8C%E6%96%B9%E5%BC%8F%E6%9C%89%E4%BA%9B%E5%8F%98%E5%8C%96%EF%BC%89%EF%BC%8C%E2%80%9Cprogram%E2%80%9D%E8%A1%A8%E7%A4%BA%E9%9C%80%E8%A6%81%E8%B0%83%E8%AF%95%E7%9A%84%E6%96%87%E4%BB%B6%EF%BC%8C%E4%B8%8B%E9%9D%A2%E8%AE%BE%E7%BD%AE%E4%B8%BA%E5%B7%A5%E4%BD%9C%E7%9B%AE%E5%BD%95%20%24%20%7BfileDirname%7D%20%E4%B8%AD%E7%9A%84%20%24%20%7BfileBasenameNoExtension%7D%20%EF%BC%8C%E5%BD%93test.cc%E5%A4%84%E4%BA%8E%E6%89%93%E5%BC%80%E7%8A%B6%E6%80%81%E6%97%B6%EF%BC%8C%E7%BC%96%E8%AF%91%E5%BE%97%E5%88%B0%E7%9A%84test%E4%BC%9A%E8%A2%AB%E8%B0%83%E8%AF%95%EF%BC%8C%E2%80%9CstopAtEntry%E2%80%9D%E9%BB%98%E8%AE%A4%E4%B8%BAfalse%2C%20%E8%BF%90%E8%A1%8C%E8%B0%83%E8%AF%95%E6%97%B6%EF%BC%8Cdebugger%E4%B8%8D%E4%BC%9A%E5%9C%A8%E6%BA%90%E6%96%87%E4%BB%B6%E4%B8%AD%E6%B7%BB%E5%8A%A0%E6%96%AD%E7%82%B9%EF%BC%8C%E8%AE%BE%E7%BD%AE%E4%B8%BAtrue%E6%97%B6%EF%BC%8C%E8%B0%83%E8%AF%95%E4%BC%9A%E5%9C%A8main%E5%87%BD%E6%95%B0%E5%85%A5%E5%8F%A3%E5%A4%84%E7%AD%89%E5%BE%85%E3%80%82
参考链接2:
记录一次在Ubuntu18.04下的VSCode C/C++编译环境配置过程——2019年1月_warning found (use --debug to expand): - empty con-CSDN博客