【原】第一个Linux设备驱动程序hello world!
《Linux设备驱动程序》第二章的第一个设备驱动程序hello world!。
define MODULE #include <linux/module.h> 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 <linux/module.h>下面加上这一句。 4、再次编译,执行gcc -c hello.c,再执行insmod hello.o ,有时候会体会模块已经存在。就用rmmod hello 再insmod,可以用lsmod查看模块是否存在。 5、会发现不管加载驱动还是卸载驱动都会没有输出。最后改printk后面优先级为0即可,再次运行能正常输出。 最终的代码:
#define MODULE #include <linux/module.h>
MODULE_LICENSE("GPL"); int init_module(void) {printk("<0>Hello,world\n"); return 0;} void cleanup_module(void) {printk("<0>Goodbye cruel world\n");}
更多相关文章
- 【思维导图】Linux0.11内核流程(从“开机上电” 到 “sh解释器”)
- linux内核模块的强制删除-结束rmmod这类disk sleep进程
- linux系统启动过程详解-开机加电后发生了什么 --linux内核剖析(零
- Linux-2.6 所有版本内核源码下载
- linux3.1内核移植到S3C2416的过程和问题。
- linux 内核编译配置内核make menuconfig 出错解决方法
- Linux内核中实现生产者与消费者(避免无效唤醒)
- Linux下符号版本原理及实现
- Linux2.6内核下键盘输入设备驱动的实现