Linux内核为模块生成compile-commands.json [英] Linux Kernel generate compile-commands.json for module

查看:150
本文介绍了Linux内核为模块生成compile-commands.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:大多数宏定义甚至头文件都不会被IDE查找,因为IDE配置中没有指定包含路径.它禁止自动补全和导航.

The problem: Most of macro definition and even header files are not looked up by an IDE because include path is not specified in the IDE configuration. It inhibits autocompletion and navigation.

这是我的 Makefile :

#-Wno-declaration-after-statement
ccflags-y := -std=gnu11 -Wno-declaration-after-statement -Werror
obj-m += pfsw.o
pfsw-objs := src/init.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

我运行了 make V = 1 ,并注意到编译命令和包含路径实际上非常麻烦(将Linux特定的 -include 用作参数):

I ran make V=1 and noticed that the compile command along with include path is actually pretty cumbersome (counting Linux Specific -include for parameters):

gcc -Wp,-MD,/home/memyself/lkm/procfs_write/src/.init.o.d  -nostdinc 
   -isystem /usr/lib/gcc/x86_64-linux-gnu/7/include  
   -I./arch/x86/include -I./arch/x86/include/generated  -I./include -I./arch/x86/include/uapi
   -I./arch/x86/include/generated/uapi -I./include/uapi -I./include/generated/uapi 
   -include ./include/linux/kconfig.h -Iubuntu/include  -include ./include/linux/compiler_types.h 
   -D__KERNEL__
   //tons of options ommitted...
   -c -o /home/memyself/lkm/procfs_write/src/init.o
    /home/memyself/lkm/procfs_write/src/init.c

问题:是否可以生成 compile-command.json 来通知IDE有关包含路径的方法?还是唯一的解决方案是手动将包含路径逐个手动传递给IDE?

Question: Is there a way to generate compile-command.json to inform IDE about include paths? Or the only solution is to manually pass the include path to the IDE one by one?

推荐答案

由于CLang具有许多不同的工具,包括一些用于分析代码的工具,因此需要 compile-command.json .这就是Google的汤姆·罗德(Tom Roeder)提供了提交 b30204640192 中的 scripts/clang-tools/gen_compile_commands.py a>(脚本:添加工具以生成compile_commands.json文件").

Due to CLang has a lot of different tools, including some to analyze the code, the compile-command.json is required. That's why Tom Roeder from Google provided a scripts/clang-tools/gen_compile_commands.py in the commit b30204640192 ("scripts: add a tool to produce a compile_commands.json file") for this.

注意,内核必须在列表中编译一次才能使该脚本正常工作.

Note, kernel must be compiled at list once to make this script work.

P.S.我想您正在尝试使用MS Visual Studio代码?

P.S. I suppose you are trying MS Visual Studio Code?

对我的同事Alex表示感谢,他是该脚本的用户,并告诉我有关此类脚本的存在.

Kudos to colleague of mine, Alex, who is user of it and told me about existence of such script.

这篇关于Linux内核为模块生成compile-commands.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆