有时,将一个内核模块分成几个源文件是有意义的。在这种情况下,你需要
在除了一个源文件外的所有源文件中,添加 #define __NO_VERSION__ 这行代码。这很重要,因为module.h通常包含kernel_version的定义,这是一个全局变量,表示该模块编译所针对的内核版本。 如果你需要version.h,你需要自己包含它,因为module.h不会因为__NO_VERSION__.
而为你包含它。
像往常一样编译所有源文件。
将所有目标文件合并成一个文件。在 x86 架构下,使用 ld -m elf_i386 -r -o <模块名.o> <第一个源文件.o> <第二个源文件.o>。
以下是一个这种内核模块的示例。
/* start.c - Illustration of multi filed modules
*
* Copyright (C) 2001 by Peter Jay Salzman
*
* 08/02/2006 - Updated by Rodrigo Rubira Branco <rodrigo@kernelhacking.com>
*/
/* Kernel Programming */
#define MODULE
#define LINUX
#define __KERNEL__
#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;
}
MODULE_LICENSE("GPL");
|
示例 2-8. start.c
下一个文件
/* stop.c - Illustration of multi filed modules
*
* Copyright (C) 2001 by Peter Jay Salzman
*
* 08/02/2006 - Updated by Rodrigo Rubira Branco <rodrigo@kernelhacking.com>
*/
/* Kernel Programming */
#define MODULE
#define LINUX
#define __KERNEL__
#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");
} |
示例 2-9. stop.c