当前位置: 首页 > news >正文

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 提供的脚本这种方法最合适。

http://www.lryc.cn/news/216158.html

相关文章:

  • GZ035 5G组网与运维赛题第8套
  • 《golang设计模式》第三部分·行为型模式-02-命令模式(Command)
  • 【linux进程控制(一)】进程终止--如何干掉一个进程?
  • 言情小说怎么推广?如何推广网络小说?
  • TensorFlow 的应用场景有哪些
  • JAVA提取嵌套夹带文件之Apache Tika
  • SSL数字证书服务
  • 浅谈安科瑞直流电表在荷兰光伏充电桩系统中的应用
  • 淘宝详情API接口怎么实现大数据分析和商品价格监控
  • 智能政务,办事更轻松!拓世法宝AI智慧政务数字人一体机,重新定义你的政务办理体验!
  • WebBits库如何使用
  • 通过netstat命令查看Linux的端口占用
  • 不用动脑小白也能制作出精美的电子杂志
  • 【计算系统】5分钟了解超算,高性能计算,并行计算,分布式计算,网格计算,集群计算以及云计算的区别
  • 6大场景,玩转ChatGPT!
  • 工业废水再利用在哪些地方
  • Spring Cloud的ElasticSearch的进阶学习
  • WordPress恢复时候遇到的几个问题
  • 设备码解释
  • 基于Docker-consul容器服务更新与发现
  • firefox浏览器添加自定义搜索引擎方法
  • redis rdb aof
  • 浮动模块布局
  • 信号、进程、线程、I/O介绍
  • 【css3】涟漪动画
  • 基础课17——智能客服系统
  • vue3 ts 导出PDF jsPDF
  • Agent 应用于提示工程
  • 云原生安全日志审计
  • 2023 辽宁省大学数学建模 B 题 数据驱动的水下导航适配区分类预测