Linux内核模块-在源文件之间共享变量 [英] Linux Kernel Module - Sharing variables between source files
问题描述
我正在尝试将内核模块链接到非LKM源文件.问题是,我遇到了一些问题.这两个文件的名称是chardev.c(LKM)和foo.c.
I'm trying to link a kernel module to a non-LKM source file. The problem is, I'm running into some issues. The names of the two files are chardev.c (the LKM) and foo.c.
我的Makefile:
My Makefile:
obj-m += chardev.o
obj-y += foo.o
all:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
$(CC) test.c -o test
clean:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean
rm test
在chardev.c内部,我有以下代码行:extern int foo;
,在foo内部,我有以下代码行:int foo = 123;
. (这两行都在文件范围内.)
Inside of chardev.c I've the following line of code: extern int foo;
, and inside foo I've the following line of code: int foo = 123;
. (Both lines are at file-scope.)
运行make时,我得到以下输出:
When running make I'm getting the following output:
make -C /lib/modules/4.4.0-31-generic/build/ M=/home/kylemart/Desktop/Device-Driver modules
make[1]: Entering directory `/usr/src/linux-headers-4.4.0-31-generic'
CC [M] /home/kylemart/Desktop/Device-Driver/chardev.o
Building modules, stage 2.
MODPOST 1 modules
WARNING: "foo" [/home/kylemart/Desktop/Device-Driver/chardev.ko] undefined!
CC /home/kylemart/Desktop/Device-Driver/chardev.mod.o
LD [M] /home/kylemart/Desktop/Device-Driver/chardev.ko
make[1]: Leaving directory `/usr/src/linux-headers-4.4.0-31-generic'
cc test.c -o test
似乎无法正确链接.我在做什么错了?
Seems things aren't linking properly. What am I doing wrong?
这似乎可行,但是有一个问题:
This seems to work, but there's a problem:
obj-m += chardev.o
chardev-objs += foo.o
all:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
$(CC) test.c -o test
clean:
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean
rm test
尽管它编译时没有警告,但是在安装编译后的模块(即sudo insmod chardev.ko
)之后,/dev
中没有新设备.以前,在不尝试链接源文件的情况下,如前所述安装内核模块会创建一个设备文件.也就是说,运行lsmod
时该设备存在.
Although it compiles without warnings, after installing the compiled module (i.e. sudo insmod chardev.ko
) there isn't a new device present in /dev
. Previously, without trying to link source files, installing kernel modules as previously stated created a device file. That said, the device is present when running lsmod
.
推荐答案
您的all
目标仅构建模块,而不构建内核,因此foo
符号在那里不存在.
Your all
target builds only the module, but not the kernel so that the foo
symbol does not exist there.
在将源代码(此处为foo.c
)编译到内核时,必须将makefile集成到内核源代码中.例如.您必须添加
When compiling a source (here: foo.c
) into the kernel, you have to integrate the makefile into the kernel source. E.g. you have to add
obj-y += my-driver/
到先前目录中的makefile并构建整个内核.您可能应该从makefile中删除all:
和clean:
目标,以避免与内核内置规则冲突.
to the makefile in the previous directory and build the whole kernel. You should probably remove your all:
and clean:
targets from the makefile to avoid conflicts with kernel builtin rules.
foo.c
必须包含
EXPORT_SYMBOL(foo);
或
EXPORT_SYMBOL_GPL(foo);
第二个生成文件...
将仅生成仅由foo.c
构建的chardev.ko
模块; chardev.c
不会被使用.当您确实需要此功能时,必须更改文件名.例如
The second makefile...
will generate only the chardev.ko
module which is built from only foo.c
; chardev.c
will not be used for it. When you really want this, you have to change file names; e.g.
obj-m += chardev.o
chardev-objs = chardev-core.o foo.o
这篇关于Linux内核模块-在源文件之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!