喵哥在去年寫了一篇關於VScode配置的文章,Ubuntu vscode的C++ tasks.json,當時只是記錄瞭如何配置,但是沒有說爲什麼這麼配置,而且只有gcc的配置,而VScode一直把Clang放在最前面,並且Clang比gcc更加節省內存,運行更快。。。
在這裏一併把gcc和Clang的配置記錄下來,並且兩者都會採用gdb和lldb的調試以作對比。
首先需安裝相應的軟件:
gcc、Clang、gdb、lldb、VScode。
sudo apt install gcc
sudo apt install gdb
sudo apt install clang
sudo apt install lldb
VScode安裝可以去官網下載:https://code.visualstudio.com/
注意:下面包含很多gif,由於CSDN文章的橫向長度有限,比例嚴重失調,直接觀看很影響您愉快的心情,建議點開圖片觀察微操作:)
VScode是基於文件夾管理代碼的,所以可以先新建一個文件夾,如‘Main’,儘量不要用中文。
然後在VScode中打開剛剛新建的文件夾,並且在VScode中新建一個cpp到此文件夾:
比如,輸入如下代碼:
#include <bits/stdc++.h>
using namespace std;
int main(){
printf("Hello vscode!\n");
return 0;
}
現在暫時不可以在VScode中調試,需要在擴展中加入C/C++ IntelliSense, debugging, and code browsing。
然後就是重頭戲了,配置launch.json和tasks.json。
選擇到debug,左上角DEBUG旁邊是“No Configurations”,點擊它,選擇“Add Configurations”,選擇“C++(GDB/LLDB)”,選擇“clang++ build and debug active file ”,這樣就生成了初始的launch.json和tasks.json文件。
//launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "clang++ build and debug active file", // 配置名稱,將會在啓動配置的下拉菜單中顯示
"type": "cppdbg", // 配置類型,這裏只能爲cppdbg
"request": "launch", // 請求配置類型,可以爲launch(啓動)或attach(附加)
"program": "${fileDirname}/${fileBasenameNoExtension}", // 將要進行調試的程序的路徑,在linux中文件的後綴名不是那麼重要,這裏可以不添加,但要與tasks.json的輸出一致
"args": [], // 程序調試時傳遞給程序的命令行參數,一般設爲空即可
"stopAtEntry": false, // 設爲true時程序將暫停在程序入口處
"cwd": "${workspaceFolder}", // 調試程序時的工作目錄,在當前的工作路徑即可,總之還是要與tasks.json的配置一致
"environment": [],
"externalConsole": true, // 調試時是否顯示控制檯窗口,一般設置爲true顯示控制檯
"MIMode": "lldb",// 指定連接的調試器,可以爲gdb或lldb。
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "clang++ build active file", // 調試會話開始前執行的任務,一般爲編譯程序。與tasks.json的taskName相對應
"miDebuggerPath": "/bin/lldb-mi" // 調試器路徑。
}
]
}
//tasks.json
{
"tasks": [
{
"type": "shell",
"label": "clang++ build active file", // 任務名稱,與launch.json的preLaunchTask相對應
"command": "/bin/clang++", //命令,與在終端編譯的參數一樣
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}" //輸出的可執行文件可以不要後綴名,爲了方便:)
],
"options": {
"cwd": "/bin"
}
}
],
"version": "2.0.0"
}
有時候clang、gcc、lldb、gdb的位置不一定跟上面的一致,可以用
whereis 關鍵字
來查找位置。
現在就基本配置好了,gcc的配置也是一樣的,只是在選擇編譯器的時候選擇“g++ build and debug active file ”
//launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"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": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/bin/gdb"
}
]
}
//tasks.json
{
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/bin"
}
}
],
"version": "2.0.0"
}
可以發現,clang默認的是lldb調試,gcc默認的是gdb調試,其實clang和gcc都可以使用兩種調試方式,只需要修改launch.json即可。
//gdb
"MIMode": "gdb",
"miDebuggerPath": "/bin/gdb"
//lldb
"MIMode": "lldb",
"miDebuggerPath": "/bin/lldb-mi"
並且,clang和gcc的編譯器選擇只要在tasks.json修改即可
//gcc
"command": "/bin/g++",
或者
"command": "/bin/gcc",
//clang
"command": "/bin/clang++",
或者
"command": "/bin/clang",
不知道是爲什麼,使用lldb調試無法在終端輸出結果,應該與這個錯誤有關:
Loaded '[vdso]'. Cannot find or open the symbol file.
但是目前還沒有找到解決方案,先挖個坑,等以後有時間再來填坑。
在終端可以用lldb手動調試是沒有問題的。
具體表現如下(以gcc示例,clang也是一樣的):
另外,需要強調的是:VScode是基於文件夾管理代碼的,並且在執行某個代碼的時候需要把主葉面切換到對應的文件,喵哥一開始經常會去執行launch.json,然後就會報錯。。。基於這個原因,完全可以在一個文件夾下有多個含有main函數的文件,這與VS系列的IDE是不一樣的。
比如,我在‘Main’文件夾中再新建一個文件test.cpp: