分类: 嵌入式
2013-05-07 17:32:17
define MODULE
#include
int init_module(void) {printk("<1>Hello,world\n"); return 0;}
void cleanup_module(void) {printk("<1>Goodbye cruel world\n");}
源代码编辑完后,保存为hello.c
按照书上,用gcc -c hello.c 命令编译后出现错误:
hello.c:1: parse error before "MODULE"
In file included from hello.c:2:
/usr/include/linux/module.h:26: syntax error before "struct"
解决方案如下:
1、第一行define MODULE 前加“#”(C语言格式)。重新编译。
2、提示内核版本与编译器版本不一致:
kernel-module version mismatch
hello.o was compiled for kernel version 2.4.20
while this kernel is version 2.4.20-8
这里修改文件vi /usr/include/linux/version.h
第一行“2.4.20”改为“2.4.20-8”
(内核源码版本/usr/src/linux-2.4.20-8/include/version.h)
这样不会报错。
3、有些版本中没有MODULE_LICENSE("GPL");这一句,执行insmod内核会抱怨,所以在#include
4、再次编译,执行gcc -c hello.c,再执行insmod hello.o ,有时候会体会模块已经存在。就用rmmod hello 再insmod,可以用lsmod查看模块是否存在。
5、会发现不管加载驱动还是卸载驱动都会没有输出。最后改printk后面优先级为0即可,再次运行能正常输出。
最终的代码:
#define MODULE
#include
MODULE_LICENSE("GPL");
int init_module(void) {printk("<0>Hello,world\n"); return 0;}
void cleanup_module(void) {printk("<0>Goodbye cruel world\n");}