无法在 5.8 内核上包含“linux/config.h”

Unable to include `linux/config.h` on 5.8 kernel

最初我试图包含 linux/config.h 以在最新的 5.8 内核上编译来自 2.6 内核的源代码的旧驱动程序。

这是该驱动程序的包含部分:

#include <linux/config.h>
#include <linux/module.h>
#include <linux/init.h>

#include <linux/kernel.h> /* printk() */
#include <linux/fs.h>     /* everything... */
#include <linux/types.h>  /* size_t */
#include <asm/uaccess.h>

根据要求,我将 Makefile 发布在这里:

ifeq ($(KERNELRELEASE),)

    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD := $(shell pwd)

modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules modules_install clean

else
    obj-m := my_driver.o
endif

当我尝试通过 gcc 编译此驱动程序时,编译器告诉我无法找到 linux/config.h

网上告诉我用linux/autoconf.h替换linux/config.h但是失败了。

所以我只想知道我应该用哪个文件替换 linux/config.h 来编译这个驱动程序和 运行.

好吧,经过大量的谷歌搜索和 DuckDuckGoing,我找到了解决方案 here

内核 2.6 中的 linux/config.hlinux/autoconf.h 被重新定位到 generated/autoconf.h

只需将 linux/config.h 替换为 generated/autoconf.h 即可。

自内核 2.6.15 以来不再需要 linux/config.h。它在内核 2.6.15 中被弃用并在内核 2.6.19 中删除(并且还从某些 Red Hat 2.6.18 内核中删除)。以下序列可用于有条件地包含它:

#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
#include <linux/config.h>
#endif

主内核 Makefile 使用 GCC 的 -include file 选项自动定义内核配置选项宏,无需使用显式 #include <linux/config.h>(或其替换)在 C 源文件中。无需添加 #include <generated/autoconf.h> 或类似 C 源文件。