hello.c
#include <linux/module.h>
#include <linux/init.h>
static int num =10;
int exportvalue=1234;
static void exportfunc(void) //需要导出的函数
{
printk("exportfunc():num = %d; exportvalue = %d\n",num,exportvalue);
}
static __init int hello_init(void)
{
printk("hello_init \n");
return 0;
}
static __exit void hello_exit(void)
{
printk("hello_exit \n");
}
EXPORT_SYMBOL(exportfunc);
EXPORT_SYMBOL(exportvalue);
MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);
callfile.c
#include <linux/module.h>
extern void exportfunc(void); //导出函数
extern int exportvalue; //导出值
static int callfile_init(void)
{
printk("callfile_init();exportvalue = %d, (exportvalue+2000) = %d\n",exportvalue,exportvalue+2000);
exportfunc();
return 0;
}
static void callfile_exit(void)
{
printk("callfile_exit \n");
}
MODULE_LICENSE("GPL");
module_init(callfile_init);
module_exit(callfile_exit);
输出结果:
[root@FriendlyARM hello]# ls
Makefile hello.c.bak hello.mod.o
Module.symvers hello.ko hello.o
hello.c hello.mod.c modules.order
[root@FriendlyARM hello]# insmod hello.ko
[ 420.558519] hello_init
[root@FriendlyARM hello]# cd ..
[root@FriendlyARM export_symbol]# ls
Makefile callfile hello user
[root@FriendlyARM export_symbol]# cd callfile/
[root@FriendlyARM callfile]# ls
Makefile callfile.c callfile.mod.c callfile.o
Module.symvers callfile.ko callfile.mod.o modules.order
[root@FriendlyARM callfile]# insmod callfile.ko
[ 439.323566] callfile_init();exportvalue = 1234, (exportvalue+2000) = 3234
[ 439.323630] exportfunc():num = 10; exportvalue = 1234