vscode 使用 clangd + bazel 实现代码跳转

本文详细介绍了在Ubuntu22.04上通过两种方式安装和配置clangd插件,以及如何与Bazel配合生成和利用compile_commands.json文件,以便在VSCode中实现代码自动补全和错误检测。包括了CMake的配置和VSCode的工作区设置步骤。
摘要由CSDN通过智能技术生成

方式一

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值