4

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.

2
  • solved...a minite after I posted this question...just use export_symbol to define a global var in core.c, not in x86.c. but don't know why? anybody know? Commented Jul 26, 2014 at 20:09
  • 2
    The order of compiling and linking of files does matter too. Commented Jul 26, 2014 at 20:20

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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. 

1 Comment

EXPORT_SYMBOL_GPL_FUTURE() - marks the symbols which may be changed to a GPL-only export at some time in the future

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.