导出和使用外部符号

来源:互联网 发布:如何做网络写手 编辑:程序博客网 时间:2024/06/16 05:42

模块A:定义并导出了两个函数(红色标注)

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>

static char *who= "who";
static int time= 1;
module_param(time, int, S_IRUGO);
module_param(who, charp, S_IRUGO);


static int hello_init(void)
{
return 0;
}


static int hello_exit(void)
{
return 0;
}


int add_intergar(int a,int b)//define add
{
return (a+b);
}


int dec_intergar(int a,int b)//define dec
{


return (a-b);
}


module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("Dual BSD/GPL");
EXPORT_SYMBOL(who);
EXPORT_SYMBOL(time);

EXPORT_SYMBOL(add_intergar);//导出
EXPORT_SYMBOL(dec_intergar);//导出


模块b:使用模块a中导出的符号(红色部分)

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>

extern int add_intergar(int a,int b);//外部声明
extern int dec_intergar(int a,int b);//外部声明

static int hello_init(void)
{
int i=0;
printk(KERN_EMERG"add symbol::::%d\n",add_intergar(2,5));//使用!
printk(KERN_EMERG"dec symbol::::%d\n",dec_intergar(2,5));

return 0;
}


static int hello_exit(void)
{
printk(KERN_EMERG"GOOBYE,WORLD\n");
return 0;
}


module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("Dual BSD/GPL");


编译模块b的时候将编译模块a过程产生的Module.symvers文件cp到当前目录(看编译器配置,非必要),然后make,记得插入模块的顺序。

插入b的结果:

[root@localhost symbol_export]# 
Message from syslogd@ at Sat Nov 12 22:43:14 2011 ...
localhost kernel: add symbol::::7
Message from syslogd@ at Sat Nov 12 22:43:14 2011 ...
localhost kernel: dec symbol::::-3