Chinaunix首页 | 论坛 | 博客
  • 博客访问: 27189
  • 博文数量: 9
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 65
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-03 20:13
文章分类

全部博文(9)

文章存档

2015年(1)

2013年(8)

我的朋友

分类: 嵌入式

2013-05-07 17:32:17

《Linux设备驱动程序》第二章的第一个设备驱动程序hello world!。

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");}


    

阅读(442) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~