VScode clangd 插件浏览 linux 源码

2023-10-31 05:28

本文主要是介绍VScode clangd 插件浏览 linux 源码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • VScode clangd 插件浏览 linux 源码
    • clangd 安装与配置
      • VScode 插件安装
      • clangd 安装
        • 方法一
        • 方法二
      • clangd 配置
    • cmake 生成
    • bear 生成 `compile_commands.json`
      • 触发 clangd
    • linux 内核脚本生成 `compile_commands.json` 文件
    • 三种方式对比

VScode clangd 插件浏览 linux 源码

VScode clangd 插件浏览代码却决于 compile_commands.json 文件
生成该文件有很多种方法

  • bear 命令
  • linux 内核脚本
  • cmake 编译选项

clangd 安装与配置

VScode 插件安装

在 VScode 插件商城搜索安装即可

clangd 安装

方法一

插件下载好之后任意打开一个 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 配置

由于 clangd 插件和 c/c++ 插件会存在冲突,在 ./vscode/settings.json 文件中添加下面一行配置,将 C/C++ 功能禁止掉

"C_Cpp.intelliSenseEngine": "disabled",

cmake 生成

cmake工程生成 compile_commands.json 文件比较简单,定义 CMAKE_EXPORT_COMPILE_COMMANDS 即可。

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1

bear 生成 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 文件中。

触发 clangd

在 VScode 里打开任意一个 C 文件,就会触发 clangd 建立索引:
在这里插入图片描述
如果正在建立索引,显示的是 indexing:1276/1699

索引建立完成之后
在这里插入图片描述
如果索引建立完成轴,显示的是 clangd: idle

此时 linux 代码可以任意跳转了。

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"},

三种方式对比

  • cmake
    • 优点:简单,加入 cmake 选项即可
    • 缺点: 依赖于 cmake 工程
  • bear(未编译之前使用)
    • 优点:任意 Makefile 工程都能使用
    • 缺点:如果改动了源码或者文件依赖,bear 每次都要重新编译
  • linux 脚本(编译之后使用)
    • 优点:简单,只需要 python 执行一下即可
    • 缺点:局限于 linux 工程

对于研究 linux 内核,直接使用 linux 提供的脚本这种方法最合适。

这篇关于VScode clangd 插件浏览 linux 源码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/313112

相关文章

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

linux解压缩 xxx.jar文件进行内部操作过程

《linux解压缩xxx.jar文件进行内部操作过程》:本文主要介绍linux解压缩xxx.jar文件进行内部操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、解压文件二、压缩文件总结一、解压文件1、把 xxx.jar 文件放在服务器上,并进入当前目录#

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

linux hostname设置全过程

《linuxhostname设置全过程》:本文主要介绍linuxhostname设置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录查询hostname设置步骤其它相关点hostid/etc/hostsEDChina编程A工具license破解注意事项总结以RHE