有时候将内核模块分为几个源文件是有意义的,你需要做下面的事情:
这里有一个这样的内核模块的范例。
/* start.c
* Copyright (C) 1999 by Ori Pomerantz
*
* "Hello, world" - 内核模块版本.
* 这个文件只包含启动程序
*/
/* 必要的头文件 */
/* 内核模块标准头文件 */
#include <linux/kernel.h> /* 我们正在做内核的工作 */
#include <linux/module.h> /* 明确的指定是内核模块 */
/* 处理 CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
/* 初始化模块 */
int init_module()
{
printk("Hello, world - this is the kernel speaking\n");
/* If we return a non zero value, it means that
* init_module failed and the kernel module
* can't be loaded */
return 0;
}
/* stop.c
* Copyright (C) 1999 by Ori Pomerantz
*
* "Hello, world" - 内核模块版本
* 这个文件只包含终止程序
*/
/* 必要的头文件 */
/* 内核模块的标准头文件 */
#include <linux/kernel.h> /* 我们正在做内核的工作 */
#define __NO_VERSION__ /* 这不是内核模块文件 */
#include <linux/module.h> /* 明确的指定是内核模块 */
#include <linux/version.h> /* 因为有 __NO_VERSION__ 而不能被自动包含*/
/* 处理 CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
/* Cleanup - 撤消init_module 所做的任何事情 */
void cleanup_module()
{
printk("Short is the life of a kernel module\n");
}
# 多文件内核模块的Make文件 CC=gcc MODCFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX hello.o: start.o stop.o ld -m elf_i386 -r -o hello.o start.o stop.o start.o: start.c /usr/include/linux/version.h $(CC) $(MODCFLAGS) -c start.c stop.o: stop.c /usr/include/linux/version.h $(CC) $(MODCFLAGS) -c stop.c