VSCode编译运行和调试C++
VSCode编译运行和调试C++
本文将详细介绍如何在Linux系统上使用VSCode配置C++开发环境,包括GCC编译器和GDB调试器的设置。通过本教程,你将学会如何在VSCode中编译和调试一个简单的C++程序。
先决条件
安装VSCode
VSCode的下载地址为:https://code.visualstudio.com/docs?start=true。历史版本的下载链接为:https://code.visualstudio.com/updates。
选择适合你硬件设备的版本进行下载。如果你的硬件是Intel(X86架构),请选择x64的.deb文件;如果你的硬件是树莓派(ARM架构),则可以选择Arm32或Arm64版本。
安装C++扩展
在VSCode界面左侧点击Extensions图标(快捷键:Ctrl+Shift+X),搜索“C/C++”并进行安装。
安装GCC
确保系统已安装GCC。在终端中输入以下命令检查GCC是否已安装:
rosnoetic@rosnoetic-VirtualBox:~$ g++ --version
如果未安装,可以使用以下命令进行安装:
rosnoetic@rosnoetic-VirtualBox:~$ sudo apt-get update
rosnoetic@rosnoetic-VirtualBox:~$ sudo apt-get install build-essential
Hello World示例
创建项目文件夹
通过终端创建一个名为project
的文件夹,并在其中创建子文件夹helloworld
。然后在project/helloworld
目录下打开VSCode:
rosnoetic@rosnoetic-VirtualBox:~$ mkdir project && cd project
rosnoetic@rosnoetic-VirtualBox:~/project$ mkdir helloworld && cd helloworld
rosnoetic@rosnoetic-VirtualBox:~/project/helloworld$ code .
项目的配置文件位于.vscode
文件夹下,主要包括三个配置文件:
tasks.json
:负责编译设置launch.json
:负责调试设置c_cpp_properties.json
:负责更改路径等设置
编写源代码
在helloworld.cpp
中编写以下代码:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
vector<string> msg {"hello", "C++"};
for (const string& word : msg) {
cout << word << " ";
}
cout << endl;
}
编译Build helloworld.cpp
创建tasks.json
在.vscode
下创建tasks.json
文件,用于告诉VSCode如何构建程序。可以通过选择终端 > 配置默认生成任务
,然后选择C/C++: g++ 生成活动文件
来自动生成。也可以手动创建,内容如下:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
执行编译
- 确保活动文件是
helloworld.cpp
- 使用快捷键Ctrl+Shift+B或从菜单中选择
终端 > 运行生成任务
- 在终端中查看编译结果,成功编译后会生成可执行文件
helloworld
调试Debug helloworld.cpp
创建launch.json
创建launch.json
文件用于配置调试器。可以通过选择运行 > 添加配置...
来自动生成。内容如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
开始调试会话
- 确保
helloworld.cpp
是活动文件 - 按F5或从菜单中选择
运行 > 启动调试
逐步执行代码
- 使用F10或Step over图标逐步执行代码
- 在代码中设置断点,方法是在代码行左侧点击或按F9
- 使用F5从断点处继续执行
设置监视
- 在Watch窗口中添加需要监控的变量
- 将鼠标悬停在变量上可快速查看其值
C/C++配置
可以通过创建c_cpp_properties.json
文件来更多地控制C/C++扩展。可以通过命令面板(Ctrl+Shift+P)运行C/C++: Edit Configurations (UI)
来打开配置界面。内容示例如下:
{
"configurations": [
{
"name": "Linux",
"includePath": ["${workspaceFolder}/"],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
通过以上步骤,你已经成功配置了在Linux系统上使用VSCode进行C++开发的环境。这个配置可以重用,只需将对应的JSON文件复制到新项目文件夹中的.vscode文件夹,并根据需要更改源文件和可执行文件的名称即可。