操作步骤
方式一
step 1
安装插件
# vscode 安装clangd插件
安装 clangd
- 实测 ubuntu22.04 与 clangd-15 可用
apt update
apt install clangd-15
update-alternatives --install /usr/bin/clangd clangd /usr/bin/clangd 100
step 2
生成 compile_commands.json
- 与 bazel 连用网址:https://github.com/hedronvision/bazel-compile-commands-extractor
- 参考链接:https://juejin.cn/post/7126880493668139021
修改 workspace
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "hedron_compile_commands",
# 建议把下面两处 commit hash 换成 github 上最新的版本
url = "https://github.com/hedronvision/bazel-compile-commands-extractor/archive/ed994039a951b736091776d677f324b3903ef939.tar.gz",
strip_prefix = "bazel-compile-commands-extractor-ed994039a951b736091776d677f324b3903ef939",
)
load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup")
hedron_compile_commands_setup()
修改 BUILD.bazel
load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")
refresh_compile_commands(
name = "refresh_compile_commands",
# 指定目标 target 及其编译选项/参数(.bazelrc 中已有的参数/选项无需重复添加)
targets = {
"//engine/exe:scqlengine": "-c opt",
},
)
运行
# 会在当前目录下生成 compile_commands.json 文件
bazel run :refresh_compile_commands
CMake 生成 compile_commands.json
CMakeLists.txt => set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
or cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=on
step 3
vscode 工作区配置
修改 ./vscode/settings.json
"clangd.path": "/usr/bin/clangd",
// Clangd 运行参数(在终端/命令行输入 clangd --help-list-hidden 可查看更多)
"clangd.arguments": [
// 在后台自动分析文件(基于 complie_commands,我们用CMake生成)
"--background-index",
// 同时开启的任务数量
"-j=10",
// compelie_commands.json 文件的目录位置
"--compile-commands-dir=${workspaceFolder}/build",
"--header-insertion=never",
"--query-driver=**"
],
重启 vscode,之后运行
ps -ef | grep clang
# 输出以下内容,证明启动成功,然后等待索引建立即可
xxx 16001 15933 99 22:27 ? 03:49:39 /usr/bin/clangd --background-index -j=10 --compile-commands-dir=/home/cjs/code/SQL/scql/build --header-insertion=never --query-driver=**
方式二
# repositories.bzl
def xxx_deps():
# compile_commands.json
_com_github_hedron_compile_commands()
def _com_github_hedron_compile_commands():
maybe(
http_archive,
name = "com_github_hedron_compile_commands",
sha256 = "085bde6c5212c8c1603595341ffe7133108034808d8c819f8978b2b303afc9e7",
strip_prefix = "bazel-compile-commands-extractor-ed994039a951b736091776d677f324b3903ef939",
urls = [
"https://github.com/hedronvision/bazel-compile-commands-extractor/archive/ed994039a951b736091776d677f324b3903ef939.tar.gz"
]
)
# WORKSPACE
load("@com_github_hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup")
hedron_compile_commands_setup()
# WORKSPACE 同级目录下 BUILD
load("@com_github_hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")
refresh_compile_commands(
name = "refresh_compile_commands",
targets = {
"//xxx:xxx": "",
},
)
# run
bazel run :refresh_compile_commands