Ubuntu的vscode配置c/c++环境
时间: 2025-03-10 19:08:12 浏览: 40
### 配置Visual Studio Code进行C/C++开发
#### 安装Visual Studio Code
为了在Ubuntu上配置Visual Studio Code用于C/C++开发,首先要安装Visual Studio Code本身。可以通过软件中心或者命令行来完成这一操作[^1]。
对于通过命令行安装的情况,在终端执行如下指令可以实现Visual Studio Code的安装:
```bash
sudo snap install --classic code
```
#### 安装GCC编译器
除了编辑器外,还需要有能够编译C/C++代码的工具链。GNU Compiler Collection (GCC) 是一个广泛使用的选项之一。可通过下面这条命令来进行安装[^3]:
```bash
sudo apt-get update && sudo apt-get install gcc g++
```
#### 安装C/C++扩展包
为了让Visual Studio Code更好地支持C/C++编程,建议安装由微软官方提供的C/C++扩展。这能提供诸如语法高亮、智能感知等功能。打开Visual Studio Code后,访问 Extensions 视图并搜索 'C/C++' 来找到对应的插件点击安装即可[^2]。
#### 设置launch.json和tasks.json文件
要让项目能够在VSCode内部被顺利构建与调试,则需创建`.vscode`目录下的两个重要JSON配置文件——`launch.json`(定义启动参数以及连接到本地或远程调试会话的方式) 和 `tasks.json`(描述了预处理/构建任务的信息),具体设置取决于个人项目的实际需求。
例如, 一个基本的任务配置可能看起来像这样:
```json
{
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "/usr/bin/g++",
"args": [
"-g",
"${workspaceFolder}/hello.cpp",
"-o",
"${workspaceFolder}/hello"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"]
}
]
}
```
而相应的调试配置则可能是这样的形式:
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/hello",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build hello world",
"miDebuggerPath": "/usr/bin/gdb",
"logging": {"trace":true,"traceResponse":true},
"internalConsoleOptions": "openOnSessionStart"
}
]
}
```
上述配置使得开发者可以直接从IDE界面发起程序编译及单步跟踪等操作,极大地提高了工作效率。
阅读全文
相关推荐


















