linux指令 make -C ..... M =....
我們可以為代碼清單4.1的模板編寫一個簡單的Makefile:
obj-m := hello.o
並使用如下命令編譯Hello World模塊:
make -C /usr/src/linux-2.6.15.5/ M=/driver_study/ modules
如果當前處於模塊所在的目錄,則以下命令與上述命令同等:
make –C /usr/src/linux-2.6.15.5 M=$(pwd) modules
其中-C后指定的是Linux內核源代碼的目錄,而M=后指定的是hello.c和Makefile所在的目錄,編譯結果如下:
[root@localhost driver_study]# make -C /usr/src/linux-2.6.15.5/ M=/driver_study/ modules
make: Entering directory `/usr/src/linux-2.6.15.5'
CC [M]/driver_study/hello.o
/driver_study/hello.c:18:35: warning: no newline at end of file
Building modules, stage 2.
MODPOST
CC /driver_study/hello.mod.o
LD [M]/driver_study/hello.ko
make: Leaving directory `/usr/src/linux-2.6.15.5'
從中可以看出,編譯過程中,經歷了這樣的步驟:先進入Linux內核所在的目錄,並編譯出hello.o文件,運行MODPOST會生成臨時的hello.mod.c文件,而后根據此文件編譯出hello.mod.o,之后連接hello.o和hello.mod.o文件得到模塊目標文件hello.ko,最后離開Linux內核所在的目錄。
中間生成的hello.mod.c文件的源代碼如代碼清單4.7所示。
代碼清單4.7 模塊編譯時生成的.mod.c文件
1 #include <linux/module.h>
2 #include <linux/vermagic.h>
3 #include <linux/compiler.h>
4
5 MODULE_INFO(vermagic, VERMAGIC_STRING);
6
7 struct module __this_module
8 __attribute__((section(".gnu.linkonce.this_module"))) = {
9 .name = KBUILD_MODNAME,
10 .init = init_module,
11 #ifdef CONFIG_MODULE_UNLOAD
12 .exit = cleanup_module,
13 #endif
14 };
16 static const char __module_depends[]
17 __attribute_used__
18 __attribute__((section(".modinfo"))) =
19 "depends=";
hello.mod.o產生了ELF(Linux所采用的可執行/可連接的文件格式)的2個節,即modinfo和.gun.linkonce.this_module。
如果一個模塊包括多個.c文件(如file1.c、file2.c),則應該以如下方式編寫Makefile:
obj-m := modulename.o
modulename-objs := file1.o file2.o
-----------------------------------------------------------------
4.9模塊的編譯
----------------------------------------------------------------------
2.4內核中,模塊的編譯只需內核源碼頭文件;需要在包含linux/modules.h之前定義MODULE;編譯、連接后生成的內核模塊后綴為.o。
2.6內核中,模塊的編譯需要配置過的內核源碼;編譯、連接后生成的內核模塊后綴為.ko;編譯過程首先會到內核源碼目錄下,讀取頂層的Makefile文件,然后再返回模塊源碼所在目錄。
