1.2 多文件内核模块 |
| 有些时候在几个源文件之间分出一个内核模块是很有意义的。在这种情况下,你需要做下面的事情: 1. 在除了一个以外的所有源文件中,增加一行#define __NO_VERSION__。这是很重要的,因为module.h一般包括kernel_version的定义,这是一个全局变量,包含模块编译的内核版本。如果你需要version.h,你需要把自己把它包含进去,因为如果有__NO_VERSION__的话module.h不会自动包含。 2. 象通常一样编译源文件。 3. 把所有目标文件联编成一个。在X86下,用ld –m elf_i386 –r –o .o <1st source file> 这里给出一个这样的内核模块的例子。 ex start.c /* start.c * Copyright (C) 1999 by Ori Pomerantz * * "Hello, world" - the kernel module version. * This file includes just the start routine */ /* The necessary header files */ /* Standard in kernel modules */ /* Deal with CONFIG_MODVERSIONS */ /* Initialize the module */ /* If we return a non zero value, it means that /* The necessary header files */ /* Standard in kernel modules */ #define __NO_VERSION__ /* This isn't "the" file #include /* Not included by /* Deal with CONFIG_MODVERSIONS */
CC=gcc hello.o: start.o stop.o start.o: start.c /usr/include/linux/version.h stop.o: stop.c /usr/include/linux/version.h |