首先這篇文章參考了以下兩位的分享:
在VS Code中使用Clang作為你的C++編譯器 - 簡書 (jianshu.com)
本文章主要是分享自己在Ubuntu的VS Code上配置LLVM和Clang的小總結
按下面配置好基本不會有什么問題,有一個需要注意的是(因為我也是復制的,所以不太懂)需要在程序最后一行設置一個斷點才可以正常運行
需要安裝的包有:
sudo apt install llvm clang clangd liblldb-dev
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": "lldb",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: clang++ build active file",
"miDebuggerPath": "/usr/lib/llvm-10/bin" //這里要找到自己的llvm路徑
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "C/C++: clang++ build active file", // 任務名稱,與launch.json的preLaunchTask相對應
"command": "clang++", // 如果用MinGW,編譯C用gcc,編譯c++用g++
"args": [
"${file}",
"-o", // 指定輸出文件名,不加該參數則默認輸出a.exe
"${fileDirname}/${fileBasenameNoExtension}",
"-g", // 生成和調試有關的信息
"-Wall", // 開啟額外警告
"-static-libgcc", // 靜態鏈接
"-fcolor-diagnostics",
//"--target=x86_64-w64-mingw", // 默認target為msvc,不加這一條就會找不到頭文件
"-std=c++1z" // c++1z即c++17,C語言最新標准為c11,或根據自己的需要進行修改
], // 編譯命令參數
"type": "shell",
"group": {
"kind": "build",
"isDefault": true // 設為false可做到一個tasks.json配置多個編譯指令,需要自己修改本文件,我這里不多提
},
"presentation": {
"echo": true,
"reveal": "silent", // 設置是否在“終端”中顯示編譯信息,可以為always,silent,never。一些錯誤提示也在這里,但是因為有靜態檢測,我就設為silent了。
"focus": false,
"panel": "shared" // 不同的文件的編譯信息共享一個終端面板
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"/"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", // 正則表達式,用於描述在“問題”欄中顯示的信息。
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
} // 1.11.0的更新日志里說可以直接寫"problemMatcher": "$gcc",但是我試了一下不行。
}
]
}
安裝的插件有:
實現了正常的輸出: