在ubuntu安装llvm/clang
sudo apt install llvm clang clangd lldb
vscode的调试器接口是按GDB开发的,所以需要一个适配器,lldb-mi就是这个适配器。lldb-mi原来是llvm项目的一部分,后面成为了一个单独的项目https://github.com/lldb-tools/lldb-mi。
接下来安装lldb-mi
sudo apt install liblldb-dev
git clone https://github.com/lldb-tools/lldb-mi.git
cd lldb-mi
mkdir build
cd build
cmake ..
cmake --build .
cp lldb-mi /usr/bin
验证一下lldb-mi,发现有错误
使用lldb -P来查看查找的路径
执行一下命令
sudo mkdir -p /usr/lib/local/lib/python3.10/dist-packages
sudo ln -s /usr/lib/llvm/lib/python3.10/dist-packages/lldb /usr/lib/local/lib/python3.10/dist-packages
cd /usr/bin
sudo ln -s ../lib/llvm-14/bin/lldb-server lldb-server-14.0.0
再次运行lldb-mi验证是否正常
创建测试目录test,在目录中建立文件test.cpp。
用vsocde打开目录test。
创建tasks.json,用于编译源代码
{"tasks": [{"label": "compile","type": "cppbuild","command": "/usr/bin/clang++","args": ["-fcolor-diagnostics","-fansi-escape-codes","-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}"],"options": {"cwd": "${fileDirname}"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "调试器生成的任务。"}],"version": "2.0.0"}
创建文件launch.json
{"version": "0.2.0","configurations": [{"name": "lldb调试","type": "cppdbg","request": "launch","program": "${fileDirname}/${fileBasenameNoExtension}","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "lldb","miDebuggerPath": "/usr/bin/lldb-mi","setupCommands": [{"description": "为 gdb 启用整齐打印","text": "-enable-pretty-printing","ignoreFailures": true},{"description": "将反汇编风格设置为 Intel","text": "setting set target.x86-disassembly-flavor intel","ignoreFailures": true}],"preLaunchTask": "compile"}]
}
现在按快捷键Ctrl+F5运行代码
设置断点,按F5测试调试功能
--完成--