在 Archlinux 搭建 VScode C/C++ 開發環境(GCC)及 調試(GDB)
在 Archlinux 搭建 VScode C/C++ 開發環境(GCC)及 調試(GDB)
2019/04/09 edited by Huang Po-Hsun相信我,肥宅我看了很多網站上教學,都是老舊的廢文,甚至如果照做就有可能造成出錯的問題。還有很多文章也沒有詳細講解該如何配置,關於
Linux中的
GDB配置在
VScode的文章少之又少,錯誤百出。因此肥宅在這教導各位正確地在
Linux環境下從安裝
VScode到配置環境變量與
GDB調試。
肥宅的 Linux 發行版資訊
肥宅使用的發行版是
ArchLinux,圖形界面是
KDE Plasma。
下載安裝 VScode
在 Konsole
輸入下載指令
sudo pacman -S code
註:因爲圖形界面的使用者一定是一般使用者,不可能是超級使用者,所以在輸
pacman前面要加上sudo。
輸入
y並按下
Enter。
下載 GCC GDB
註:肥宅是在
Windows OS用習慣了MinGW-w64,但在Linux這樣配置並不是很好的選擇,如果你使用GCC或Clang會跟Linux兼容性高。MinGW-w64軟件包包含gccg++GDB…等內容及API。
在這裡肥宅使用
GCC與GDB
在 Konsole
輸入下載指令
GCC
sudo pacman -S gcc
輸入
y並按下
Enter。
GDB
sudo pacman -S gdb
輸入
y並按下
Enter。
補充:
註:有些小白可能不懂,在此肥宅附上 WIKI 連結
什麼?!你還是不懂?
安裝 VScode 插件
安裝自己使用的語言並 Reload
在插件搜尋處搜尋自己要的語言,像肥宅我是用繁體中文,所以只要在搜尋處打上
traditional就會出現安裝插件,按下
Install,並等它裝好
Reload就會切換到自己想要的語言界面,如果你想裝的是簡體中文,可以輸入
simplified,其實你只要輸入
Chinese的
ch基本繁體簡體都會顯示出來。
安裝 C/C++ 插件
搜尋
C/C++並安裝
其餘插件推薦請參考 Visual Studio Code 推薦使用插件
VScode 一些設定參考
點擊左下角滾輪中的設定,再點擊右上角的
{},可進入
settings.json。
settings.json
{ "workbench.colorTheme": "One Dark Pro", "workbench.iconTheme": "vscode-icons", "terminal.integrated.fontFamily": "Source Code Pro", "code-runner.runInTerminal": true, "code-runner.saveFileBeforeRun": true, "code-runner.clearPreviousOutput": true, "typescript.locale": "zh-TW", "code-runner.fileDirectoryAsCwd": true, "editor.fontLigatures": true, "editor.fontFamily": "'Droid Sans Mono', 'monospace', monospace, 'Droid Sans Fallback', 'Source Code Pro'", "terminal.integrated.rendererType": "canvas", "files.autoSave": "afterDelay", "editor.fontSize": 16, "C_Cpp.default.intelliSenseMode": "clang-x64" }
註:肥宅有裝 code-runer 插件,所以設定中有這個 code-runner。這裡不需要各位去複製貼上,只是給大家參考肥宅用了那些主題與插件。像肥宅目前使用
One Dark Pro佈置主題。
注意:第 5 行的設定很重要,因為當你裝完 code-runner
時預設是不能在終端機輸入,所以才將 code-runner.runInTerminal
參數設為 true
配置 GDB 調試
這時你的電腦已配置了
gcc
g++的環境變數,但你並還沒將其 IDE 完全配好,如果沒配好會出現一些問題,像肥宅在寫
C敲上
#include <stdio.h>時底部會出現綠色的波浪線,因爲沒將頭文件
*.h都載入,雖然不影響編譯執行,但總會覺得很礙眼,感覺有瑕疵,在這裡肥宅一次幫各位解決。
註:下圖為綠色波浪底線示意圖
讓我們開始配置吧~
註:建議各位將所有代碼集中在一個資料夾方便管理,並有序分類。
註:這裡使用的是
Dolphin來檢視檔案,類似Windows OS的檔案總管,這裡就不介紹這個。
註:資料夾名或檔案名開頭是
.的話會隱藏的,e.g..vscode.git,必須開啟可以顯示的設定才能看到。
請於代碼根目錄(也就是你使用
VScode打開的資料夾)先將隱藏的檔案可以顯示,這裡因爲是肥宅先配置好的所以才會看到
.vscode,請在這新建個資料夾,資料夾名叫
.vscode,並分別新建四個文件,分別是
註:像肥宅在
VScode開啟的資料夾是Study,所以肥宅在Study這個資料夾目錄中新增名為.vscode的資料夾,並進入此資料夾新建四個檔案
- settings.json
- c_cpp_properties.json
- launch.json
- task.json
接著分別填入以下內容
settings.json
{ "files.defaultLanguage": "cpp", // ctrl+N新建文件后默认的语言 "editor.formatOnType": true, // 输入时就进行格式化,默认触发字符较少,分号可以触发 "editor.formatOnSave": true, // 每次存檔時,自動執行自動排版功能 "editor.snippetSuggestions": "top", // snippets代码优先显示补全 "code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入 "code-runner.executorMap": { "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt.out", "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt.out" }, // 设置code runner的命令行 "code-runner.saveFileBeforeRun": true, // run code前保存 "code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false "code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息 "C_Cpp.clang_format_sortIncludes": true, // 格式化时调整include的顺序(按字母排序) "C_Cpp.intelliSenseEngine": "Default", // 可以为Default或Tag Parser,后者较老,功能较简单。具体差别参考cpptools扩展文档 }
註:在
Linux中輸出檔的副檔名是*.out,有別於Windows的*.exe
注意:第八行與第九行的是 code-runner
插件的編譯執行命令行,因為是在 Linux
下編譯執行,所以肥宅在後面 fileNameWithoutExt
變數後面從 .exe
改成 .out
,這樣在使用 code-runner
插件時幫你自動生成的輸出檔副檔名是 *.out
c_cpp_properties.json
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/usr/include", "/lib64/gcc/x86_64-pc-linux-gnu/8.2.1/include/" ], "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "gcc-x64", "defines": [ "_DEBUG", "UNICODE", "__GNUC__=7", "__cdecl=__attribute__((__cdecl__))" ], "browse": { "path": [ "${workspaceFolder}", "/usr/include", "/lib64/gcc/x86_64-pc-linux-gnu/8.2.1/include/", "/lib64/gcc/x86_64-pc-linux-gnu/8.2.1/include-fixed/", "/lib64/gcc/x86_64-pc-linux-gnu/8.2.1/install-tools/include/" ], "limitSymbolsToIncludedHeaders": true, "databaseFilename": "" } } ], "version": 4 }
註:肥宅之前就是這裡沒配置好(第 5 行的
"includePath")所以才會在#include <stdio.h>這行底部出現綠色波浪
注意:肥宅 GCC 的版本是 8.2.1,所以如果你的版本改跟我不一樣請修改成你的當前版本,這裡最重要的是路徑不能有錯。也許你的路徑可能跟我不一樣,這些都可以在 root
根目錄中找到,請自行尋找與修改。
launch.json
{ // 使用 IntelliSense 以得知可用的屬性。 // 暫留以檢視現有屬性的描述。 // 如需詳細資訊,請瀏覽: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Run C", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}.out", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": false } ], "preLaunchTask": "build & run file C" }, { "name": "Run C++", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}.out", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": false } ], "preLaunchTask": "build & run file C++" }, { "name": "Debug C", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}.out", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": false } ], "preLaunchTask": "build & debug file C" }, { "name": "Debug C++", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}.out", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": false } ], "preLaunchTask": "build & debug file C++" } ] }
註:在分
C/C++的配置時可以分開,一個是gcc一個是g++,然而有些人直接全部都是用g++編譯,這樣也可以,只是不能使用gcc去編譯C++。由於肥宅有強迫症,所以分成四個部份。
注意:這裡肥宅分了四個類型,分別是 Run C
Run C++
Debug C
Debug C++
,
其中每一個 "program"
(進行調試的程序路徑)參數都是
"${workspaceFolder}/${fileBasenameNoExtension}.out"
,
"preLaunchTask"
的值則是對應到 task.json
的 "label"
,如果兩者對應的名稱出錯在調試時會出現找不到對應名稱的錯誤訊息,要你開啟 launch.json
重新設定。
tasks.json
{ "version": "2.0.0", "tasks": [ { "label": "build & debug file C", "type": "shell", "command": "gcc", "args": [ "-g", "${file}", "-o", "${fileBasenameNoExtension}.out" ], "group": { "kind": "build", "isDefault": true } }, { "label": "build & debug file C++", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "${fileBasenameNoExtension}.out" ], "group": { "kind": "build", "isDefault": true } }, { "label": "build & run file C", "type": "shell", "command": "gcc", "args": [ "${file}", "-o", "${fileBasenameNoExtension}.out" ], "group": { "kind": "build", "isDefault": true } }, { "label": "build & run file C++", "type": "shell", "command": "g++", "args": [ "${file}", "-o", "${fileBasenameNoExtension}.out" ], "group": { "kind": "build", "isDefault": true } } ] }
註:
-g是調試的意思,-o是輸出的意思。
注意:每個 "args"
參數中的 "${fileBasenameNoExtension}.out"
要特別注意尾端改成.out
。
調試畫面
配置完後,你可以在調試欄看到
Run C
Run C++
Debug C
Debug C++選項。
肥宅現在要調試
1.c這個檔案,可以在代碼中設置斷點,並選擇
Debug C。
註:調試快捷鍵
F5
左側可以看到變數的值
註:肥宅已在
launch.json中設定"externalConsole": false,因爲這個設定的意思是使格外彈出個Konsole,這功能只能在Windows OS中改成true
純手工編譯執行
假設有個檔名叫
1,副檔名可以是
*.c或
*.cpp
C
編譯標準 C99
gcc -std=c99 1.c -o 1.out
執行
./1.out
C++
編譯標準 C++11
g++ -std=c++11 1.cpp -o 1.out
執行
./1.out
如果有引入其他自定義的標頭檔之實現檔
編譯時加入參數
e.g. 我有
main.c且引入自定義的
Circle.h標頭檔,並且將
Circle.h標頭檔的實現部份寫在
Circle.cpp
編譯
gcc -std=c99 main.c Circle.cpp -o main.out
執行
./main.out
©copyright by Huang Po-Hsun
- 使用 VSCode 搭建 C/C++ 开发环境
- 搭建vscode的c++环境
- 在Windows 10 中用 Bash on Windows(WSL) 给 VSCode 搭建 编译调试 C/C++ 环境
- VS code C/C++开发环境搭建
- Visual Studio Code+TDM-GCC搭建面向学生的轻量级C/C++开发环境
- vscode + cygwin gdb 调试C/C++ 配置
- linux环境下vscode 中C/C++编译及调试环境的搭建
- 在Linux中使用VS Code编译调试C++项目(gcc/g++、Makefile入门、vs code)
- [置顶] win10下配置VS Code C/C++编译环境(TDM-GCC)
- VScode C/C++环境搭建,.json脚本设置
- 【VSCode】Windows下VSCode编译调试c/c++
- Cocos2dx C++与Lua合并开发的VS配置环境搭建
- VSCode配置C++编写环境
- 如何在Windows平台使用VS搭建C++/Lua的开发环境
- Ubuntu下安装并配置VS Code编译C++
- VS Code C++开发常用文件过滤设置
- node.js:在vs code中使用node.js搭建本地服务器并进行代码调试
- windows下建立gcc/g++/gdb/make编译及调试C/C++的IDE环境
- VsCode搭建Java开发环境(Spring Boot项目创建、运行、调试)
- VSCode之C/C++调试配置详解