一、安裝scode
在ubuntu的軟件商店里直接搜索visual studio code,就可以下載了


二、C/C++環境配置
打開terminal,挨個兒執行下面三個命令:
// 1. 安裝C編譯器
sudo apt-get install gcc
// 2. 安裝C++編譯器
sudo apt-get install g++
// 3. 安裝make
sudo apt-get install make
三、在vscode中配置
主要就是一個.vscode文件夾,這個文件夾下有兩個文件:tasks.json和launch.json。整體的目錄如下:

test.cpp就是自己的源碼,test.out是經過編譯后的執行文件。
需要配置的這兩個.json文件的內容分別如下:
1. tasks.json文件
{
"tasks": [
{
"type": "shell",
"label": "build",
"command": "g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out"
],
// "options": {
// "cwd": "${fileDirname}"
// },
// "problemMatcher": [
// "$gcc"
// ],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "調試器生成的任務。"
}
],
"version": "2.0.0",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
// "panel": "shared",
// "showReuseMessage": true,
// "clear": false
}
}
2. launch.json文件
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 啟動",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "為 gdb 啟用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
四、測試運行
在vscode中打開test.cpp,按F5就可以測試看結果啦~

參考來源:
Ubuntu 20.04.2.0 C/C++ 安裝與配置
https://blog.csdn.net/LingXuan46854534/article/details/117807615
