VScode clangd 插件浏览代码却决于 compile_commands.json
文件
生成该文件有很多种方法
在 VScode 插件商城搜索安装即可
插件下载好之后任意打开一个 C/CPP 文件,vscode 下方会出现弹窗直接 install 即可(若网络原因或其他原因这里一直下载不出来请参考方式二)
从GitHub上下载 clangd-language,网址:https://githubfast.com/clangd/clangd/releases/
选择适合自己的即可,解压之后需要告诉 VScode 你的 clangd 的地址
Ctrl + ,
打开配置,输入 clangd
输入自己的 path 即可(如果是方法一这里会自动填充)
/home/tyustli/.vscode-server/data/User/globalStorage/llvm-vs-code-extensions.vscode-clangd/install/16.0.2/clangd_16.0.2/bin/clangd
由于 clangd
插件和 c/c++
插件会存在冲突,在 ./vscode/settings.json
文件中添加下面一行配置,将 C/C++ 功能禁止掉
"C_Cpp.intelliSenseEngine": "disabled",
cmake工程生成 compile_commands.json 文件比较简单,定义 CMAKE_EXPORT_COMPILE_COMMANDS 即可。
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1
compile_commands.json
不过很多工程都是用 Makefile 来编译的,例如 linux 内核,没有现成的选项生成 compile_commands.json
文件。我们可以通过 bear 来生成。
bear 生成之前需要确保没有编译过,或者
make clean
一下,否则生成的文件是空的!!!
安装 bear
sudo apt-get install bear
使用 bear -- make
编译(之前的命令是 bear make
)
编译 linux 内核生成
export ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabihf- # 设置编译平台和工具链
make vexpress_defconfig # 加载板子的配置信息
make dtbs # 编译设备树
bear -- make -j8 # 编译内核
生成之后将 /usr/bin/gcc
全局搜索替换为自己工具链路径 /home/tyustli/cross_tool/gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-gcc
生成的部分 compile_commands.json
文件如下
{
"arguments": [
"/home/tyustli/cross_tool/gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-gcc",
"-Wp,-MMD,scripts/mod/.empty.o.d",
"-nostdinc",
"-I./arch/arm/include",
"-I./arch/arm/include/generated",
"-I./include",
"-I./arch/arm/include/uapi",
"-I./arch/arm/include/generated/uapi",
"-I./include/uapi",
"-I./include/generated/uapi",
"-include",
"./include/linux/compiler-version.h",
"-include",
"./include/linux/kconfig.h",
"-include",
"./include/linux/compiler_types.h",
"-D__KERNEL__",
"-mlittle-endian",
"-D__LINUX_ARM_ARCH__=7",
"-fmacro-prefix-map=./=",
"-std=gnu11",
"-fshort-wchar",
"-funsigned-char",
"-fno-common",
"-fno-PIE",
"-fno-strict-aliasing",
"-Wall",
"-Wundef",
"-Werror=implicit-function-declaration",
"-Werror=implicit-int",
"-Werror=return-type",
"-Werror=strict-prototypes",
"-Wno-format-security",
"-Wno-trigraphs",
"-fno-dwarf2-cfi-asm",
"-mno-fdpic",
"-fno-ipa-sra",
"-mtp=cp15",
"-mabi=aapcs-linux",
"-mfpu=vfp",
"-funwind-tables",
"-marm",
"-Wa,-mno-warn-deprecated",
"-march=armv7-a",
"-msoft-float",
"-Uarm",
"-fno-delete-null-pointer-checks",
"-Wno-frame-address",
"-Wno-format-truncation",
"-Wno-format-overflow",
"-Wno-address-of-packed-member",
"-O2",
"-fno-allow-store-data-races",
"-Wframe-larger-than=1024",
"-fstack-protector-strong",
"-Wno-main",
"-Wno-unused-but-set-variable",
"-Wno-unused-const-variable",
"-fomit-frame-pointer",
"-fno-stack-clash-protection",
"-Wvla",
"-Wno-pointer-sign",
"-Wcast-function-type",
"-Wno-stringop-truncation",
"-Wno-stringop-overflow",
"-Wno-restrict",
"-Wno-maybe-uninitialized",
"-Wno-alloc-size-larger-than",
"-Wimplicit-fallthrough=5",
"-fno-strict-overflow",
"-fno-stack-check",
"-fconserve-stack",
"-Werror=date-time",
"-Werror=incompatible-pointer-types",
"-Werror=designated-init",
"-Wno-packed-not-aligned",
"-g",
"-fplugin=./scripts/gcc-plugins/arm_ssp_per_task_plugin.so",
"-DKBUILD_MODFILE=\"scripts/mod/empty\"",
"-DKBUILD_BASENAME=\"empty\"",
"-DKBUILD_MODNAME=\"empty\"",
"-D__KBUILD_MODNAME=kmod_empty",
"-c",
"-o",
"scripts/mod/empty.o",
"scripts/mod/empty.c"
],
"directory": "/home/tyustli/code/open_source/kernel/linux-6.5.7",
"file": "/home/tyustli/code/open_source/kernel/linux-6.5.7/scripts/mod/empty.c",
"output": "/home/tyustli/code/open_source/kernel/linux-6.5.7/scripts/mod/empty.o"
},
从该文件可以看到,每个编译的文件的详细信息都被记录在 compile_commands.json
文件中。
在 VScode 里打开任意一个 C 文件,就会触发 clangd
建立索引:
如果正在建立索引,显示的是 indexing:1276/1699
索引建立完成之后
如果索引建立完成轴,显示的是 clangd: idle
。
此时 linux 代码可以任意跳转了。
compile_commands.json
文件linux 内核提供了生成该文件的脚本
scripts/clang-tools/gen_compile_commands.py
执行该脚本之前需要确保内核已经编译过!!!
执行该脚本即可生成 compile_commands.json
文件
python3 ./scripts/clang-tools/gen_compile_commands.py
生成的部分内容如下
{
"command": "arm-none-linux-gnueabihf-gcc -Wp,-MMD,block/.genhd.o.d -nostdinc -I./arch/arm/include -I./arch/arm/include/generated -I./include -I./arch/arm/include/uapi -I./arch/arm/include/generated/uapi -I./include/uapi -I./include/generated/uapi -include ./include/linux/compiler-version.h -include ./include/linux/kconfig.h -include ./include/linux/compiler_types.h -D__KERNEL__ -mlittle-endian -D__LINUX_ARM_ARCH__=7 -fmacro-prefix-map=./= -std=gnu11 -fshort-wchar -funsigned-char -fno-common -fno-PIE -fno-strict-aliasing -Wall -Wundef -Werror=implicit-function-declaration -Werror=implicit-int -Werror=return-type -Werror=strict-prototypes -Wno-format-security -Wno-trigraphs -fno-dwarf2-cfi-asm -mno-fdpic -fno-ipa-sra -mtp=cp15 -mabi=aapcs-linux -mfpu=vfp -funwind-tables -marm -Wa,-mno-warn-deprecated -march=armv7-a -msoft-float -Uarm -fno-delete-null-pointer-checks -Wno-frame-address -Wno-format-truncation -Wno-format-overflow -Wno-address-of-packed-member -O2 -fno-allow-store-data-races -Wframe-larger-than=1024 -fstack-protector-strong -Wno-main -Wno-unused-but-set-variable -Wno-unused-const-variable -fomit-frame-pointer -fno-stack-clash-protection -Wvla -Wno-pointer-sign -Wcast-function-type -Wno-stringop-truncation -Wno-stringop-overflow -Wno-restrict -Wno-maybe-uninitialized -Wno-alloc-size-larger-than -Wimplicit-fallthrough=5 -fno-strict-overflow -fno-stack-check -fconserve-stack -Werror=date-time -Werror=incompatible-pointer-types -Werror=designated-init -Wno-packed-not-aligned -g -fplugin=./scripts/gcc-plugins/arm_ssp_per_task_plugin.so -fplugin-arg-arm_ssp_per_task_plugin-offset=1248 -DKBUILD_MODFILE='\"block/genhd\"' -DKBUILD_BASENAME='\"genhd\"' -DKBUILD_MODNAME='\"genhd\"' -D__KBUILD_MODNAME=kmod_genhd -c -o block/genhd.o block/genhd.c",
"directory": "/home/tyustli/code/open_source/kernel/linux-6.5.7",
"file": "/home/tyustli/code/open_source/kernel/linux-6.5.7/block/genhd.c"
},
对于研究 linux 内核,直接使用 linux 提供的脚本这种方法最合适。