我已经试了几个小时来创建我的第一个Hello World内核模块,但遗憾的是没有成功。我的简单C代码(hello.c)和makefile位于我的系统中的/Downloads目录下,如果这有任何重要性的话。
hello.c
#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);
Makefile
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是否缺失某些东西?
- ...
.ko
文件。 - cadaniluk