Chinaunix首页 | 论坛 | 博客
  • 博客访问: 307939
  • 博文数量: 75
  • 博客积分: 4010
  • 博客等级: 上校
  • 技术积分: 775
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-18 22:18
文章分类

全部博文(75)

文章存档

2015年(2)

2014年(1)

2007年(8)

2006年(60)

2005年(4)

我的朋友

分类: LINUX

2006-07-01 08:29:16

 Modules Spanning Multiple Files

Sometimes it makes sense to divide a kernel module between several source files. In this case, you need to:

  1. In all the source files but one, add the line #define __NO_VERSION__. This is important because module.h normally includes the definition of kernel_version, a global variable with the kernel version the module is compiled for. If you need version.h, you need to include it yourself, because module.h won't do it for you with __NO_VERSION__.

  2. Compile all the source files as usual.

  3. Combine all the object files into a single one. Under x86, use ld -m elf_i386 -r -o <1st src file.o> <2nd src file.o>.

Here's an example of such a kernel module.

Example 2-8. start.c

/*  start.c - Illustration of multi filed modules
 */

#include        /* We're doing kernel work */
#include        /* Specifically, a module */

int init_module(void)
{
  printk("Hello, world - this is the kernel speaking\n");
  return 0;
}

The next file:

Example 2-9. stop.c

/*  stop.c - Illustration of multi filed modules
 */

#if defined(CONFIG_MODVERSIONS) && ! defined(MODVERSIONS)
   #include  /* Will be explained later */
   #define MODVERSIONS
#endif        
#include   /* We're doing kernel work */
#include   /* Specifically, a module  */
#define __NO_VERSION__     /* It's not THE file of the kernel module */
#include  /* Not included by module.h because of
	                                      __NO_VERSION__ */
	
void cleanup_module()
{
   printk("<1>Short is the life of a kernel module\n");
}  

And finally, the makefile:

Example 2-10. Makefile for a multi-filed module

CC=gcc
MODCFLAGS := -O -Wall -DMODULE -D__KERNEL__
   	
hello.o:	hello2_start.o hello2_stop.o
   ld -m elf_i386 -r -o hello2.o hello2_start.o hello2_stop.o
   	
start.o: hello2_start.c
   ${CC} ${MODCFLAGS} -c hello2_start.c
   	
stop.o: hello2_stop.c
   ${CC} ${MODCFLAGS} -c hello2_stop.c
阅读(1072) | 评论(0) | 转发(0) |
0

上一篇:Hello World

下一篇:GRUB 明口令加密;

给主人留下些什么吧!~~