Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to define linux kernel variable accessed by several source file?

I have searched the answer to my question for hours, to no avail.

My question: I want to define a variable that can be accessed(w/r)(shared) by the two files in linux kernel: source/arch/x86/kvm/x86.c and source/kernel/sched/core.c.

My failed attempt: I tried to use export_symbol to define a global var in x86.c. But the compile error message says:

the var is undefined reference

Is there any other solution? I am new to linux kernel programming. Thanks in advance.

like image 801
Jianchen Avatar asked Dec 19 '22 12:12

Jianchen


1 Answers

When you use want to use a global variable in kernel modules, you should use the EXPORT_SYMBOL() or EXPORT_SYMBOL_GPL() or EXPORT_SYMBOL_GPL_FUTURE(): Eg:

 int myvar;
 EXPORT_SYMBOL(myvar);

you should then use

extern int myvar

in the other file where you want to use it, before you use it.

like image 153
askmish Avatar answered Jan 25 '23 22:01

askmish