LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 866|回复: 5

求助:第一次写驱动

[复制链接]
发表于 2008-4-19 18:50:40 | 显示全部楼层 |阅读模式
第一次学写驱动,试着按照 linuxdevicedriver3 里说的写的一个驱动

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/version.h>
#include<linux/types.h>
#include<linux/fs.h>
#include<linux/mm.h>
#include<linux/error.h>
#include<linux/config.h>
#include<asm/segment.h>
unsigned int test_major = 0;
static int read_test(struct inode *inode,struct file *file,char *buf,int count){
int left;
if (verify_area(VERIFY_WRITE,buf,count) == -EFAULT)
return -EFAULT;
for (left = count; left > 0 ; left--){
__put_user(1,buf,1);
buf++;
}
return count;
}

static int write_test(struct inode *inode,struct file *file,const char *buf, int count){
return count;
}

static void release_test(struct inode *inode,struct file *file,){
MOD_DEC_USE_COUNT;
}

struct file_operations test_fops = {
NULL,
read_test,
write_test,
NULL,
NULL,
NULL,
NULL,
open_test,
release_test,
NULL,
};

int init_module(void){
int result;
result = regster_chrdev(0,"test",&test_fops);
if(result < 0){
printk(KERN_INFO "test:can't get major number!\n");
return result;
}
if(test_major == 0)test_major = result;
return 0;
}

void cleanup_module(void){
unregister_chrdev(test_major,"test");
}

一般设备驱动写这样一个.c文件就可以了吗?(本人实在不太懂,都没有说这样些一个完整驱动的例子),????
然后就执行:
gcc -O2 -DMODULE -D__KENEL__ -c test.c

出错:
linux/module.h:No such file or director
linux/mm.h:No such file or directory
linux/error.h:No such file or directory
linux/config.h:No such file or directory
asm/segment.h:No such file or directory

为什么会这样?是gcc找不到lib文件?我有装 linux-headers-
发表于 2008-4-19 19:02:24 | 显示全部楼层
得指定linux头文件的路径, gcc的时候要加 -I
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-4-19 21:12:40 | 显示全部楼层
比如我在/usr/src下有linux-headers-2.6.20-15-generic/
然后在gcc后加 -I /usr/src/linux-headers-2.6.20-15-generic/include/linux
为什么不可以呢?
一直折腾都不能解决。还有,想问下这位大哥,怎样才不用每次gcc编译的时候都加库文件呢?

谢谢赐教呵
回复 支持 反对

使用道具 举报

发表于 2008-4-19 21:46:53 | 显示全部楼层
楼主去学学make,这种活make能帮我们完成。
回复 支持 反对

使用道具 举报

发表于 2008-4-20 01:25:29 | 显示全部楼层
如果 include <linux/...> 这样的话, 那么就
-I /usr/src/linux-headers-2.6.20-15-generic/include
www1862 讲得对, 用 make 吧。如果经常需要开发 linux 的软件,应该学学 autotools ( autoconf, automake, libtool)
回复 支持 反对

使用道具 举报

发表于 2008-4-21 20:29:56 | 显示全部楼层

你用的是那个版本的内核,不同版本的内核,对驱动编译头文件的要求不一样!

你用的是那个版本的内核,不同版本的内核,对驱动编译头文件的要求不一样!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表