且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux内核模块-在源文件之间共享变量

更新时间:2023-09-12 09:28:46

您的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