1.准备工作:安装g++或者gdb
sudo apt update
sudo apt install g++
g++ --version
sudo apt install gdb
gdb --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":