如何在linux内核中使用来自另一个c文件的变量?

时间:2022-04-11 15:59:20

I know the way normal and I tried it but it seems not work.

我知道正常的方法,我试过了,但似乎行不通。

In linux/net/sched/sch_htb.c, I define the variable:

在linux中/net/sched/sch_htb。c,我定义变量:

unsigned int queuelength;
EXPORT_SYMBOL(queuelength);

And some actions about the variable, not important.

关于变量的一些行为,并不重要。

In linux/net/ipv4/tcp_dctcp.c,

在linux中/net/ipv4/tcp_dctcp.c,

extern unsigned int queuelength;

Error come with net/built-in.o:

错误伴随着net/built-in.o:

In function `dctcp_update_alpha':
linux/net/ipv4/tcp_dctcp.c:230: undefined reference to `queuelength'

The kernel version is v4.6.

内核版本是v4.6。

1 个解决方案

#1


1  

It depends on how a source file, which defines the variable (generally, symbol), and a source file, which uses the variable (symbol) are compiled: as a part of the kernel module, or as a part of the kernel core (that is, built-in into kernel).

它取决于如何编译源文件(定义变量(通常是符号)和源文件(使用变量(符号)):作为内核模块的一部分,或者作为内核核心的一部分(即内核内置的内核)。

Assuming names of the source files are define-symbol.c and use-symbol.c correspondingly, you have 5 possibilities:

假设源文件的名称是定义符号。c和use-symbol。c相应的,你有5种可能性:

  1. Both define-symbol.c and use-symbol.c are compiled into kernel core.

    define-symbol。c和use-symbol。c被编译成内核核心。

    EXPORT_SYMBOL isn't needed.

    EXPORT_SYMBOL不是必要的。

  2. define-symbol.c is compiled into kernel core, use-symbol.c is compiled into kernel module.

    define-symbol。c被编译成内核核心,用符号。c被编译成内核模块。

    EXPORT_SYMBOL is needed.

    EXPORT_SYMBOL是必要的。

  3. define-symbol.c is compiled into kernel module, use-symbol.c is compiled into kernel core.

    define-symbol。c被编译成内核模块use-symbol。c被编译成内核核心。

    You cannot use such symbol.

    你不能使用这样的符号。

  4. define-symbol.c and use-symbol.c are compiled into the same kernel module.

    define-symbol。c和use-symbol。c被编译到同一个内核模块中。

    EXPORT_SYMBOL isn't needed.

    EXPORT_SYMBOL不是必要的。

  5. define-symbol.c and use-symbol.c are compiled into the different kernel modules.

    define-symbol。c和use-symbol。c被编译成不同的内核模块。

    EXPORT_SYMBOL is needed.

    EXPORT_SYMBOL是必要的。

Note, that way of source compilation may depend on configuration options.

注意,源代码编译的方式可能取决于配置选项。

In your case, it seems you have situation 3: as net/ipv4/tcp_dctcp.c is used for built-in.o, it is part of the kernel core.

在您的例子中,看起来您有情形3:as net/ipv4/tcp_dctcp。c是内置的。o,它是内核核心的一部分。


Note, that in any case variable should be declared for use it. Otherwise, it will be compile-time error, not a link one.

注意,在任何情况下都应该声明变量来使用它。否则,它将是编译时错误,而不是链接错误。

#1


1  

It depends on how a source file, which defines the variable (generally, symbol), and a source file, which uses the variable (symbol) are compiled: as a part of the kernel module, or as a part of the kernel core (that is, built-in into kernel).

它取决于如何编译源文件(定义变量(通常是符号)和源文件(使用变量(符号)):作为内核模块的一部分,或者作为内核核心的一部分(即内核内置的内核)。

Assuming names of the source files are define-symbol.c and use-symbol.c correspondingly, you have 5 possibilities:

假设源文件的名称是定义符号。c和use-symbol。c相应的,你有5种可能性:

  1. Both define-symbol.c and use-symbol.c are compiled into kernel core.

    define-symbol。c和use-symbol。c被编译成内核核心。

    EXPORT_SYMBOL isn't needed.

    EXPORT_SYMBOL不是必要的。

  2. define-symbol.c is compiled into kernel core, use-symbol.c is compiled into kernel module.

    define-symbol。c被编译成内核核心,用符号。c被编译成内核模块。

    EXPORT_SYMBOL is needed.

    EXPORT_SYMBOL是必要的。

  3. define-symbol.c is compiled into kernel module, use-symbol.c is compiled into kernel core.

    define-symbol。c被编译成内核模块use-symbol。c被编译成内核核心。

    You cannot use such symbol.

    你不能使用这样的符号。

  4. define-symbol.c and use-symbol.c are compiled into the same kernel module.

    define-symbol。c和use-symbol。c被编译到同一个内核模块中。

    EXPORT_SYMBOL isn't needed.

    EXPORT_SYMBOL不是必要的。

  5. define-symbol.c and use-symbol.c are compiled into the different kernel modules.

    define-symbol。c和use-symbol。c被编译成不同的内核模块。

    EXPORT_SYMBOL is needed.

    EXPORT_SYMBOL是必要的。

Note, that way of source compilation may depend on configuration options.

注意,源代码编译的方式可能取决于配置选项。

In your case, it seems you have situation 3: as net/ipv4/tcp_dctcp.c is used for built-in.o, it is part of the kernel core.

在您的例子中,看起来您有情形3:as net/ipv4/tcp_dctcp。c是内置的。o,它是内核核心的一部分。


Note, that in any case variable should be declared for use it. Otherwise, it will be compile-time error, not a link one.

注意,在任何情况下都应该声明变量来使用它。否则,它将是编译时错误,而不是链接错误。