最近用Debian 11,进行C学习,想用VScode来配置环境,但是发现有很多问题。
比如g++找不到,launch file doesn't exist。
看了网上很多教程也没搞定,全是拷贝粘贴的,所以去看了官方文档,一下子解决了,这里记录一下,仅供参考
官方文档:https://code.visualstudio.com/docs/cpp/config-linux
主要参考了官方文档进行汉化,调整
1、安装必要软件插件
1、安装 Visual Studio Code.
直接下载,然后安装即可,参考命令:sudo dpkg -i ****.deb
2、安装 C++ extension for VS Code.
在插件里面搜索即可,或者按Ctrl+Shift+X
3、安装gcc,gdb
VScode只能用来编辑代码,编译运行还是要用g++,debug要用gdb
可以通过这个命令来判断是不是安装了gcc,查看gcc的版本
gcc -v
没装的话,可以用下面的命令来装一下
sudo apt-get update
sudo apt-get install gcc
然后安装gdb和build-essential
sudo apt-get install build-essential gdb
2、 创建Hello World
通过以下命令可以创建projects目录,helloworld目录
mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .
code .
命令会在当前目录打开vscode,并且会自动创建3个需要的文件夹
tasks.json
(compiler build 设置)launch.json
(debugger 设置)c_cpp_properties.json
(compiler path and IntelliSense 设置)
创建helloworld程序
在文件浏览里面点击新建文件,然后名称定为helloworld.cpp
.
(官方用的是C++程序演示,个人用的是C程序,helloworld.c,演示上没什么大区别,就不改了)

拷贝下面的示例程序,到刚才的文件里面
#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;
}
Ctrl+S 保存

IntelliSense
似乎是vscode的一个扩展优化工具,方便看代码的,了解的不多,有兴趣的话可以看一下官方的说明

Build helloworld.cpp#
然后要创建一个tasks.json
来告诉VS Code怎么build (compile) 程序,之后会调用g++来创建一个可执行文件
程序这个时候必须要在编辑框里,待会会调用编辑框里的程序来生成可执行文件
菜单栏选择Terminal > Configure Default Build Task,然后选择 C/C++: g++ build active file.

然后会自动创建一个 tasks.json
file在.vscode
文件夹里面,
应该类似下面的内容
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Note:
tasks.json更多的说明在这个里面
variables reference.
command
设置什么程序来运行build; 这里是 g++.
args
确认要传送到g++的参数.
这个文件内容就是告诉g++去拿到打开的文件 (${file}
), 编译它, 然后在当前目录用相同的名字创建一个可执行文件(${fileDirname}
) ,并且把后缀去掉(${fileBasenameNoExtension}
), 这个例子里面生成的就是helloworld
label
参数可以随意设置,可以在tasks里面查看。
isDefault: true
定义了这个task可以通过Ctrl+Shift+B来直接进行执行,方便操作。
即使设成了false,也可以通过菜单栏的Tasks: Run Build Task进行执行
build
- 按Ctrl+Shift+B 或者点击 Terminal选择Run Build Task,正常的话,结果应该如下图

- 然后点击+号可以创建一个新的terminal,然后点./helloworld就可以进行执行
修改tasks.json
可以通过修改tasks.json
来同时build多个C++文件通过使用"${workspaceFolder}/*.cpp"
替换${file}
. 也可以修改输出文件名来更改输出文件的名字 "${fileDirname}/${fileBasenameNoExtension}"
(比如 'helloworld.out').
Debug helloworld.cpp#
然后要创建一个launch.json
文件来配置VS Code使用GDB来debug程序,可以通过F5快捷键实现
点击Run > Add Configuration... 然后选择C++ (GDB/LLDB).
在debug配置里面选择g++ build and debug active file.

VS Code会自动创建一个launch.json
类似下面的内容,然后会build,运行'helloworld'.
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
program定义了你要debug的程序. 这里是定义了活动文件的文件夹${fileDirname}下的
没有后缀的活动文件名称的文件${fileBasenameNoExtension}
, 如果helloworld.cpp是活动文件的话,这里就是helloworld
.
默认情况下 C++ 插件 不会在程序里面创建任何断点,而且stopAtEntry
配置成了false
.
想要在定义的位置让程序停下来,需要将stopAtEntry
改成true
.
开始debug
回到helloworld.cpp
,让它继续成为活动文件- 按F5 或者选择Run > Start Debugging.
- 在下面的Debug Output里面会有debug信息出现,代表着debug正常运行
- 编辑器高亮的这一行就是默认的一个断点
- 在程序编辑框上面能看到一个管理窗口,可以在这里进行debug的一些控制
在程序里面跳转

根据官方文件进行的说明,减去了一部分东西,仅供参考,有问题的话可以联系我,或者直接查看官方文档