我已经尝试了几个小时来创建我的第一个 Hello World Kernel 模块,可惜没有成功。我的简单 C 代码 (hello.c) 和 makefile 位于我系统上的 /Downloads 中,如果这很重要的话。
你好ç
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void) {
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void) {
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
生成文件
obj−m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
这是我尝试通过在终端中输入“make”(不带引号)来构建内核模块时收到的错误消息:
make -C /lib/modules/4.2.0-16-generic/build
M=/home/username/workspace/test/Module modules make[1]:
Entering directory '/usr/src/linux-headers-4.2.0-16-generic'
scripts/Makefile.build:44:
/home/username/workspace/test/Module/Makefile: No such file or
directory make[2]: *** No rule to make target
'/home/username/workspace/test/Module/Makefile'. Stop.
Makefile:1398: recipe for target
'_module_/home/username/workspace/test/Module' failed make[1]:
*** [_module_/home/username/workspace/test/Module] Error 2 make[1]: Leaving directory '/usr/src/linux-headers-4.2.0-16-generic'
makefile:4: recipe for target 'all' failed make: *** [all] Error 2
我已经尝试了几乎每一个解决方案,并遵循了可以在谷歌上找到的关于这个问题的每一个建议。没有什么可以解决我的问题......如果有人知道我可以尝试的任何事情,我将非常感激!
- 我应该将我的 hello.c 和 makefile 移动到 /usr/src 吗?
- 我的文件中是否有任何语法错误?
- 它应该在makefile中说hello.c而不是hello.o吗?
- 我的 Ubuntu 可能会丢失一些东西吗?
- ...