Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3922397
  • 博文数量: 534
  • 博客积分: 10470
  • 博客等级: 上将
  • 技术积分: 4800
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-26 14:08
文章分类

全部博文(534)

文章存档

2024年(1)

2021年(1)

2019年(1)

2017年(1)

2016年(2)

2013年(2)

2012年(10)

2011年(43)

2010年(10)

2009年(17)

2008年(121)

2007年(252)

2006年(73)

分类: LINUX

2007-04-07 15:45:27

由多个文件构成的内核模块

  有时将模块的源代码分为几个文件是一个明智的选择。在这种情况下,你需要:
   1. 只要在一个源文件中添加#define __NO_VERSION__预处理命令。 这很重要因为module.h通常包含 kernel_version的定义,此时一个存储着内核版本的全局变量将会被编译。但如果此时你又要包含头文件 version.h,你必须手动包含它,因为 module.h不会再包含它如果打开预处理选项__NO_VERSION__。
   2. 像通常一样编译。
   3. 将所有的目标文件连接为一个文件。在x86平台下,使用命令ld -m elf_i386 -r -o <1st src file.o> <2nd src file.o>。

此时Makefile一如既往会帮我们完成编译和连接的脏活。

这里是这样的一个模块范例。

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;
}

另一个文件:
Example 2-9. stop.c
/*
 *  stop.c - Illustration of multi filed modules
 */
#include     /* We're doing kernel work */
#include     /* Specifically, a module  */

void cleanup_module()
{
    printk("<1>Short is the life of a kernel module\n");
}

最后是该模块的Makefile:
Example 2-10. Makefile
obj-m += hello-1.o
obj-m += hello-2.o
obj-m += hello-3.o
obj-m += hello-4.o
obj-m += hello-5.o
obj-m += startstop.o
startstop-objs := start.o stop.o
阅读(710) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~