内核模块依赖
一个模块使用了另一个模块的变量或者函数,第一个模块就依赖于第二个模块
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL v2");
extern int test_mul(int,int);//外部声明,就可以调用了
extern int test_div(int,int);
int __init import_init(void)
{
int result = 0;
printk("enter import_init!\n");
result = test_mul(10, 20);
printk("result = %d\n",result);
return 0;
}
void __exit import_exit(void)
{
int result = 0;
printk("enter import_exit!\n");
result = test_div(15, 3);
printk("result = %d\n",result);
}
module_init(import_init);
module_exit(import_exit);
模块的导出符号
如果一个模块中的变量或者函数希望被别的模块使用,需要将对应的变量或者函数导出EXPORT_SYMBOL:导出的内容所有模块都能使用
EXPORT_SYMBOL_GPL:导出的内容只有遵循GPL协议的模块才能使用
所以模块编程是一定要添加
MODULE_LICENSE("GPL v2");
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL v2");
int test_mul(int x,int y)
{
printk("enter test_mul!\n");
return x*y;
}
int test_div(int x,int y)
{
printk("enter test_div!\n");
return x/y;
}
/*内核导出符号*/
EXPORT_SYMBOL(test_mul);
EXPORT_SYMBOL_GPL(test_div);
tips:
1)在使用insmod安装模块的时候 也要先安装依赖文件,不然外部声明会报错。
2)模块函数被别的模块所使用的时候,不能被卸载。
3)modprode安装模块会自动把依赖模块自动加载到上去,当然前提是依赖模块是已经安装好的。