原文地址:https://blog.csdn.net/DefetC/article/details/79946100
參考了以下幾篇文章:
https://www.zhihu.com/question/30315894/answer/154979413(雖然講解的是Windows環境中的安裝,但十分詳盡,很有參考價值);
https://my.oschina.net/u/1044667/blog/832111;
https://blog.csdn.net/u011258217/article/details/78693564
一、vs code安裝
直接在deepin商店下載即可。
二、安裝gcc/g++
命令行: sudo apt-get install build-essential
三、插件安裝
C/C++(制作者是microsoft,最基礎的插件)
Code Runner(實用工具,可以選中代碼塊后,右鍵選擇“run coder”直接運行代碼,右上角也有快捷按鈕)
下面的插件都是知乎那篇文章推薦的:
C/C++ Clang Command Adapter:提供靜態檢測(Lint)
Include Autocomplete:提供頭文件補全
C/C++ Snippets:Snippets即重用代碼塊
其他可選插件:
Bracket Pair Colorizer:彩虹花括號
One Dark Pro:大概是VS Code安裝量最高的主題
GBKtoUTF8:把GBK編碼的文檔轉換成UTF8編碼的
clang是和gcc/g++類似的編譯器,區別不在此細究。知乎那篇文章推薦clang,不過我還沒有研究其在linux的配制方法。本文使用g++作為編譯器。
四、配置文件
用VSCode打開項目文件夾,打開一個源文件,直接快捷鍵ctrl + shift + D,點擊設置圖標,彈出的選擇中選C++(GDB/LLDB),會自動創建項目的launch.json文件(官方文檔 ),默認是調試配置。不過為什么不是運行配置?
修改其中的program字段值,改為編譯生成的可執行文件路徑。如 "program": "${workspaceRoot}/${fileBasenameNoExtension}.out"。即,若源文件是case.c,則將調試case.out文件。
給launch.json添加一個任務選項: "preLaunchTask": "build","preLaunchTask"可以是"build",也可以是“compile”,不過必須與tasks.json中的“label”一致(后面會提到)。
下面給出代碼:
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
接下來按F5調試,此時彈出一個信息框,選配置任務,選擇后點擊Others,跳出tasks.json(官方文檔 )配置文件,配置一個名為”build“的任務。
直接貼代碼吧:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks":
[
{
"label": "build",//任務名,和lanuch.json中的"preLaunchTask":"build"一致
"type": "shell",
"command": "g++",
"args":["-g","${workspaceRoot}/${fileBasenameNoExtension}.cpp","-o","${fileBasenameNoExtension}.out"],//要編譯的文件mian_test.cpp,${workspaceRoot}表示vscode所打開的工作目錄
"problemMatcher":
{
"owner":"cpp",
"fileLocation":["relative","${workspaceRoot}"],
"pattern":
{
"regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
"file": 1,
"line":2,
"column":3,
"severity": 4,
"location": 2,
"message": 5
}
}
}
]
}
五、解決一點小問題
接下來繼續按F5,彈出一個問題:“Unable to start debugging. No terminal is available to launch the debugger. Please install Gnome Terminal or XTerm”
很好解決,只需要到deepin商店中下載gnome-terminal即可。
至此,環境已經初步建成。
六、細節
Ctrl+shift+d是編譯,F5是編譯+調試。
七、下一步安排
研究編譯器clang的配置方法