有时将模块的源代码分为几个文件是一个明智的选择。在这种情况下,你需要:
只要在一个源文件中添加 #define __NO_VERSION__预处理命令。这很重要因为 module.h 通常包含 kernel_version的定义,此时一个存储着内核版本的全局变量将会被编译。 但如果此时你又要包含头文件 version.h,你必须手动包含它,因为 module.h 不会再包含它如果打开预处理选项 __NO_VERSION__。
像通常一样编译。
将所有的目标文件连接为一个文件。在x86平台下,使用命令ld -m elf_i386 -r -o <module name.o> <1st src file.o> <2nd src file.o>.
这里是这样的一个模块范例。
Example 2-8. start.c
/* start.c - Illustration of multi filed modules
*/
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
int init_module(void)
{
printk("Hello, world - this is the kernel speaking\n");
return 0;
} |
另一个文件:
Example 2-9. stop.c
/* stop.c - Illustration of multi filed modules
*/
#if defined(CONFIG_MODVERSIONS) && ! defined(MODVERSIONS)
#include <linux/modversions.h> /* Will be explained later */
#define MODVERSIONS
#endif
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
#define __NO_VERSION__ /* It's not THE file of the kernel module */
#include <linux/version.h> /* Not included by module.h because of
__NO_VERSION__ */
void cleanup_module()
{
printk("<1>Short is the life of a kernel module\n");
} |
最后是该模块的Makefile: