linux内核模块编译,多个源文件的makefile编写解决init_module不调用的问题

当你存在两个源文件test.c  test2.c test.h;其中test.c源依赖于test2.c

要如何编写makefile,来实现内核模块test.ko的编译,并且不会有不调用init_module入口函数的问题。

Kbuild的文档,有如下描述:

 If a kernel module is built from several source files, you specify
    that you want to build a module in the same way as above.

    Kbuild needs to know which the parts that you want to build your
    module from, so you have to tell it by setting an
    $(<module_name>-objs) variable.

    Example:
        #drivers/isdn/i4l/Makefile
        obj-$(CONFIG_ISDN) += isdn.o
        isdn-objs := isdn_net_lib.o isdn_v110.o isdn_common.o

    In this example, the module name will be isdn.o. Kbuild will
    compile the objects listed in $(isdn-objs) and then run
    "$(LD) -r" on the list of these files to generate isdn.o.

错误的Makefile编写如下:

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

ARCH := ARM

obj-m := test.o
test-objs := test2.o

all:
    make -C $(KERNEL_DIR) M=$(PWD) modules

clean:
    rm -f *.ko *.o *.symvers *.cmd *.cmd.o

存在的问题:

编译没有问题,但是安装后模块的功能没有实现,就连init_module()中打印的提示信息都没有(也就是没有调用到驱动模块的入口函数)。

lsmod查看加载的驱动模块,却有test。


参考以下文章

http://www.linuxquestions.org/questions/programming-9/linking-multiple-files-kernel-module-programming-701735/

修改Makefile

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

ARCH := ARM

obj-m := AAA.o
AAA-objs := test.o test2.o

all:
    make -C $(KERNEL_DIR) M=$(PWD) modules

clean:
    rm -f *.ko *.o *.symvers *.cmd *.cmd.o

这样就解决问题了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章