Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4729940
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2009-04-11 12:52:53

最简单的示例程序,写两个模块,分别为A和B,在A中导出了一些函数,而在B中使用A导出的函数。模块都使用GPL。
两个模块的源码如下:
C/C++ code

// Module A (mod_a.c)
#include<linux/init.h>
#include
<linux/module.h>
#include
<linux/kernel.h>

static int func1(void)
{
printk(
"In Func: %s...\n"
,__func__);
return 0
;
}

// Export symbol func1

EXPORT_SYMBOL(func1);

static int __init hello_init(void
)
{
printk(
"Module 1,Init!\n"
);
return 0
;
}

static void __exit hello_exit(void
)
{
printk(
"Module 1,Exit!\n"
);
}

module_init(hello_init);
module_exit(hello_exit);



C/C++ code

// Module B (mod_b.c)
#include<linux/init.h>
#include
<linux/kernel.h>
#include
<linux/module.h>

static int func2(void)
{
extern int func1(void
);
func1();
printk(
"In Func: %s...\n"
,__func__);
return 0
;
}

static int __init hello_init(void
)
{
printk(
"Module 2,Init!\n"
);
func2();
return 0
;
}

static void __exit hello_exit(void
)
{
printk(
"Module 2,Exit!\n"
);
}

module_init(hello_init);
module_exit(hello_exit);



Makefile for Module A
BatchFile code

obj-m
+= mod1.o
mod1-y :
= mod_a.
o

KVERSION
= $(shell uname -r)


all:
make -C
/lib/modules/$(KVERSION)/build M=$(PWD) modules

clean:
make -C
/lib/modules/$(KVERSION)/build M=$(PWD)
clean
rm -f *
.o *.ko *.cmd




Makefile for Module B
BatchFile code

obj-m
+= mod2.o
mod2-y :
= mod_b.
o

KVERSION
= $(shell uname -r)


all:
make -C
/lib/modules/$(KVERSION)/build M=$(PWD) modules

clean:
make -C
/lib/modules/$(KVERSION)/build M=$(PWD)
clean
rm -f *
.o *.ko *.cmd



 原文地址
阅读(897) | 评论(0) | 转发(0) |
0

上一篇:多网卡 ethx

下一篇:date small应用

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