这里写自定义目录标题
一、安装visual stdio code
-
打开你的终端(Ctrl+Alt+T)并且运行下面的命令:
sudo snap install --classic code
Visual Studio Code 就安装完成了。当然,你也可以从Ubuntu Software直接下载安装。
-
安装好gcc/g++和gdb
sudo apt-get update [有密码则输入密码] sudo apt-get install gcc [有密码则输入密码] sudo apt-get install g++ [有密码则输入密码] sudo apt-get install gdb [有密码则输入密码]
-
安装vs code插件
C/C++与Code Runner
二、编译与运行
编译与运行可使用Code Runner插件或者命令行终端完成。点击:文件>新建文件,出现一个空白文档,填写测试代码:
#include<stdio.h>
int main()
{
printf("hello world!");
return 0;
}
保存文件后将文件后缀名改为.c或.cpp后缀,然后点击右上角的Code Runner插件生成的三角形按钮,或者按Ctrl+Alt+N就能编译运行当前程序。
三、配置调试环境
新建文件夹在打开的文件夹下新建一个 .vscode文件夹,然后在.vscode文件夹下新建两个json文件
,将文件内容修改为以下内容:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "compile",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
.task.json
{
"version": "2.0.0",
"tasks": [{
"label": "compile",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
如果需要是c语言也就是gcc将下面的command项由g++改为gcc
至此,调试环境配置完成。可以按 ctrl+shift+b 直接调用配置好的g++ task 编译程序而不运行程序。现在就可以愉快使用vscode编写c/c++程序了。