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

這樣就解決問題了。

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