我有一个内核模块,其结构如下:
struct test {
int a;
int b;
.....
}
我创建了该结构的实例数组:
struct test foo[8];
我想使用此结构或数组“ foo”导出EXPORT_SYMBOL
并foo[0].a
在其他内核模块中访问。
我EXPORT_SYMBOL(foo);
从提供程序模块和extern struct test * foo;
接收器模块中尝试过,但是无法访问该变量。请指出我在哪里犯错。
这是更多代码:
内核模块1:
#include <....>
#include "test_config.h"
....
MODULE_LICENSE("GPL");
struct test {
int a;
int b;
.....
}
test_t foo[8];
//EXPORT_SYMBOL(foo);
/*Code to create sysctl variables out of these members of the struct test*/
int init_module(void)
{
printk(KERN_INFO "Hello World\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye Cruel World\n");
}
核心模块2:
#include <linux/module.h>
#include <linux/kernel.h>
#include "test_config.h"
int init_module(void)
{
test_t foo[8];
printk ("Value of foo is :: %d\n", foo[0].a);
foo[0].a++;
printk(KERN_INFO "Hello Again World\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye Again Cruel World\n");
}
这是带有结构定义的头文件:
#ifndef __TEST_CONFIG
#define __TEST_CONFIG
typedef struct test
{
int a;
int b
int c;
int d;
float e;
}test_t;
#endif
32先生给出的答案不能解决我的问题。因此,我在不使用结构的情况下实现了这一点,并为每个成员a,b,c创建了单独的数组,以便继续执行我的任务...
经过更多的实验,我能够达到导出结构的原始要求。
我修改了头文件,如下所示:
#ifndef __TEST_CONFIG
#define __TEST_CONFIG
struct test
{
int a;
int b
int c;
int d;
float e;
};
extern struct test foo[8];
#endif
完成此操作后,我在提供程序模块中将此结构定义为:
struct test foo[8];
EXPORT_SYMBOL(foo);
并在接收器模块中包括标头并对其进行引用,如下所示:
extern struct test foo[8];
通过进行这些更改,我可以通过执行从第二个模块中的第一个模块访问值foo[0].a;
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句