Chinaunix首页 | 论坛 | 博客
  • 博客访问: 104579
  • 博文数量: 26
  • 博客积分: 1060
  • 博客等级: 少尉
  • 技术积分: 300
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-04 10:32
文章分类

全部博文(26)

文章存档

2013年(1)

2011年(21)

2010年(4)

分类: LINUX

2010-11-08 17:38:15

今天学习编写了linux的驱动,很简单的helloworld驱动的编译和编写。
遇到了几个问题, 记录下来。
1。编码阶段
 
编码阶段很简单, 按照书上的写了一遍代码:
 
 

/*****************************************************
* one simple driver;
*/


#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
    printk(KERNEL_ALERT "Hello World enter\n");
    return 0;
}
static init hello_exit(void)
{
    printf(KERNEL_ALERT "Hello World exit\n");
    return 0;
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR("ZTL");
MODULE_DESCRIPTION("A simple Hello World example Module");
MODULE_ALIAS("A simple module");

2.编译阶段

主要是这个阶段遇到的问题。

2.1 首先是按照书本上的 编写Makefile

obj-m := myhello.o
myhello-objs := hello.o

#KDIR := /lib/modules/$(shell uname -r)/build

KDIR := /home/arm/uClinux-dist

PWD := $(shell pwd)

default:
        make -C $(KDIR) M=$(PWD) modules
clean:
        rm -rf *.o *.cmd *.ko *.mod.c .temp_versions

这样执行 make却提示好多错误。

看了一下,发现不对,执行make之后,根本没有对hello.c进行编译。

最后查了一篇文章:

《Linux 2.4和2.6内核模块编译的差别》

http://blog.sina.com.cn/s/blog_591a183f010002ji.html

才晓得,linux2.4和linux2.6的模块编译是有区别的。

然后执行 

[root@localhost dirvertest]# gcc -c hello.c -D__KERNEL__ -DMODULE -I /usr/src/linux-2.4.20-8/include/ -O -Wall


这里需要注意的是 要加上 “/include” 不然会提示错误,

还有一点需要注意的是 源文件要加上#include <linux/kernel.h>

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