Linux kernel 为外部内核模块正确设置makefile

Linux kernel 为外部内核模块正确设置makefile,linux-kernel,kernel-module,kbuild,Linux Kernel,Kernel Module,Kbuild,我正在编写一个角色驱动程序,它位于源代码树中修改过的ahci版本之上。我基本上有这样的东西: topdir | |- Makfile | |- mod_ahci | | - Makefile, codefiles | |- char_interface | | - Makefile, codefiles 现在,char_接口需要mod_ahci的符号。我有相应的EXPORT\u SYMBOL()宏用于需要导出的符号。但是,我在从c

我正在编写一个角色驱动程序,它位于源代码树中修改过的ahci版本之上。我基本上有这样的东西:

topdir
   |
   |- Makfile
   |
   |- mod_ahci
   |     | - Makefile, codefiles
   |
   |- char_interface
   |     | - Makefile, codefiles
现在,char_接口需要mod_ahci的符号。我有相应的
EXPORT\u SYMBOL()
宏用于需要导出的符号。但是,我在从char_接口获取mod_ahci中的头文件时遇到了问题。我的顶级生成文件

ifneq ($(KERNELRELEASE),)
    obj-y := mod_ahci/ char_interface/

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

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

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

endif
char_接口的makefile(因为其他构建很好)

我在内核文档中引用了各种文本文件。例如,我现在指的是…/Documentation/kbuild/makefiles.txt以及…/Documentation/kbuild/modules.txt。无论何时构建,我都会得到
/home/captaink/devel/kmodtests/char\u interface/char\u interface.c:2:22:error:mod\u ahci.h:没有这样的文件或目录
。目录
。/mod\u ahci
中有一个名为mod\u ahci.h的文件。在char驱动程序的makefile中使用
ccflags-y
有什么不对


谢谢

经过一番挖掘,我找到了问题的答案。我误解了我在LDD3和内核文档中看到的
makefile
(显然,这正是O'Reilly举例的地方)。构建系统实际上将目录更改为
/usr/src/kernels/$(uname-r)/build
(或类似),因为这就是编译器找不到我的头文件的原因

我并不是说这是一种优雅的方式,但这里是我如何修复它的。顶部目录中的
makefile
现在看起来像:

ifneq ($(KERNELRELEASE),)
    obj-y := mod_ahci/ char_interface/

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

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

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

endif
ifneq ($(KERNELRELEASE),)
    ccflags-y += -I$(TOP_DIR)/mod_ahci
    obj-m := char_interface.o

else
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build

default:
    $(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules

modules_install:
    $(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules_install

endif

clean:
    -sudo rmmod ahcip
    -rm -f *.ko* *.mod.* *.o modules.order Modules.symvers
包含char驱动程序接口的子目录中的
makefile
如下所示:

ifneq ($(KERNELRELEASE),)
    obj-y := mod_ahci/ char_interface/

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

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

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

endif
ifneq ($(KERNELRELEASE),)
    ccflags-y += -I$(TOP_DIR)/mod_ahci
    obj-m := char_interface.o

else
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build

default:
    $(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules

modules_install:
    $(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules_install

endif

clean:
    -sudo rmmod ahcip
    -rm -f *.ko* *.mod.* *.o modules.order Modules.symvers
正如您所知,makefile已被广泛复制。子目录中可能不需要“共享”makefile内容,因为这由更高级别的makefile负责。尽管如此,模块现在构建,我知道的字符驱动程序是我在修改后的ahci驱动程序中创建的导出符号

我希望这可以帮助像我这样的新手了解Linux内核构建世界和Linux内核驱动程序