我想做一个非常简单的模块。所以,我刚做了一个hello_module.c
文件和 Makefile
.Makefile
:
obj-m := hello_module.o
KERNEL_DIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) clean
hello_module.c
:#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kthread.h> // for thread
#include <linux/slab.h> // for kmalloc
#include <linux/delay.h>
int test_thread(void * _arg) {
int * arg = (int * ) _arg;
printk("argument : %d\n", * arg);
kfree(arg);
return 0;
}
void thread_create(void) {
int i;
/* thread create */
for (i = 0; i < 10; i++) {
int * arg = (int * ) kmalloc(sizeof(int), GFP_KERNEL);
* arg = i;
kthread_run( & test_thread, (void * ) arg, "test_thread");
}
}
int __init hello_module_init(void) {
thread_create();
printk(KERN_EMERG "Hello Module\n");
return 0;
}
但是,它没有产生任何 .ko
文件。此外,没有错误。结果如下:
rashta@rashta-VirtualBox:~/Desktop/ehh$ sudo make
[sudo] password for rashta:
make -C /lib/modules/5.4.71yujin/build SUBDIRS=/home/rashta/Desktop/ehh modules
make[1]: Entering directory '/usr/src/linux-5.4.71'
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
DESCEND objtool
CHK kernel/kheaders_data.tar.xz
Building modules, stage 2.
MODPOST 5483 modules
make[1]: Leaving directory '/usr/src/linux-5.4.71'
我究竟做错了什么?
最佳答案
您的 Makefile
是错的。为了构建内核模块,您需要通过 M=
,而不是 SUBDIRS=
.这应该有效:
obj-m := hello_module.o
KERNEL_DIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) clean
此外,您的模块缺少一些成功编译的部分,即 module_init()
定义使用哪个函数的宏。将以下内容添加到您的代码中:void __exit hello_module_exit(void) {
// Not required, but needed to be able to unload the module.
return;
}
module_init(hello_module_init);
module_exit(hello_module_exit);
MODULE_VERSION("0.1");
MODULE_DESCRIPTION("Test module");
MODULE_AUTHOR("You");
MODULE_LICENSE("GPL");
关于c - 内核 Makefile 不会为我的模块创建任何 .ko 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64685005/