Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1910118
  • 博文数量: 383
  • 博客积分: 10011
  • 博客等级: 上将
  • 技术积分: 4061
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-24 18:53
文章分类

全部博文(383)

文章存档

2011年(1)

2010年(9)

2009年(276)

2008年(97)

我的朋友

分类: LINUX

2009-04-07 18:54:24

最简单的示例程序,写两个模块,分别为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

阅读(2667) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~