大家好,我是 👉 【Python当打之年(点击跳转)】
本期主要介绍一下如何在 ubuntu系统下的vscode中配置C++环境,包括tasks.json、launch.json文件的设置,希望对你有所帮助,如有疑问或者需要改进的地方可以私信小编。
🏳️🌈 1. 安装g++、gdb
sudo apt-get update
sudo apt-get install build-essential gdb
🏳️🌈 2. tasks.json(编译说明)
代码:
{
"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
}
}
]
}
部分参数说明:
- command :指要运行的程序,这里是
/usr/bin/g++
,可以通过指令whereis g++
查看。 - args :指明要传给g++的命令行参数,这项工作是调用g++编译目标文件
${file}
,并在当前目录${fileDirname}
创建一个没有文件后缀的、与目标文件同名的可执行文件。 - label :任务标签,会在任务列表中显示,可任意修改。
🏳️🌈 3. 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": "${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 :选项指定要调试的程序。这里设置的目标文件目录
${fileDirname}
和无扩展名的目标文件${fileBasenameNoExtion}
对应test.cpp
生成的目标文件test
。 - preLaunchTask : 任务标签,这一项要配置成
task.json
的label
。 - miDebuggerPath :debug模式,这里是
/usr/bin/gdb
,可以通过指令whereis gdb
查看。
🏳️🌈 4. test.cpp(运行测试)
代码:
#include <iostream>
using namespace std;
int main()
{
printf("Hello World!!! \n");
printf("公众号:Python 当打之年 \n");
return 0;
}
- 返回
test.cpp
,这是目前的目标文件。 - 按
F5
或主菜单选择运行 > 启动调试
。
结果:
以上就是本期为大家整理的全部内容了,赶快练习起来吧,原创不易,喜欢的朋友可以点赞、收藏也可以分享(注明出处)让更多人知道。