我目前正在研究内核模块。这是一个简化模块,可以重现我的问题。
main_module.c:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include "other_functions.h"
MODULE_LICENSE("GPL");
int major = 0;
struct file_operations fops = {
.owner = THIS_MODULE,
};
int __init main_module_init(void) {
printk(KERN_INFO "<main_module> Hello World!\n");
major = register_chrdev(0, "main_module", &fops);
test();
return 0;
}
void __exit main_module_exit(void) {
unregister_chrdev(major, "main_module");
printk(KERN_INFO "<main_module> goodbye\n");
}
module_init(main_module_init);
module_exit(main_module_exit);
other_functions.c:
#include "other_functions.h"
int test(void) {return 0;}
other_functions.h:
#ifndef OTHER_FUNCTIONS
#define OTHER_FUNCTIONS
#include <linux/module.h>
MODULE_LICENSE("GPL");
int test(void);
#endif
生成文件:
obj-m += main_module.o
main_module-objs := other_functions.o
default:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
在我编译模块并加载它之后,dmesg | grep main_module
输出任何内容,而我应该得到<main_module> Hello World!
作为输出。
但是,如果我删除main_module.c
中的第4行:#include "other_functions.h
,
以及Makefile
中的第3行:main_module_objs := ...
。在重新编译模块并重新加载它之后,dmesg | grep main_module
命令将为我提供所请求的输出。
有人可以解释一下如何解决这个问题吗?我真的无法理解这种行为。非常感谢!
PS:我正在使用Ubuntu 12.04而我的内核版本是3.2.0-37-generic-pae
答案 0 :(得分:1)
我终于弄清楚出了什么问题。
问题出在Makefile中。目标文件main_module.o
未链接。
所以我编辑了makefile,解决了这个问题:
生成文件:
obj-m += target.o # Here I changed the target's name so it's not interfering with any other dependency
target-objs := main_module.o other_functions.o # I added main_module to the dependencies
default:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules