简体   繁体   中英

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.

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.

You can think of kernel symbols (either functions or data objects, variables) and their visibility at three different levels in the kernel source code:

"static", - visible only within their own source file
"external" - visible to any other code built into the kernel itself
"exported" - visible and available to any loadable module. 

Also you may want to consider how other module should use your exported symbols, typically done with one of:

EXPORT_SYMBOL() - exports to any loadable module, or
EXPORT_SYMBOL_GPL() - exports only to GPL-licensed modules. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM