免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4373 | 回复: 11
打印 上一主题 下一主题

郁闷啊,弄了好多天了,还是不能成功编译这个最简单的驱动,请大家帮忙看看,谢谢了! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-13 16:33 |只看该作者 |倒序浏览
10可用积分
小弟刚接触LINUX不久,现在老师有个项目需要写个驱动,我这几天查了很多书,在我们这个论坛上也下了
很多最简单的驱动源码,人家说在内核2.4.20-8下就能编译并能用测试程序测试成功,可我这个连编译都要出错,而且我
改了很多天还是不行,万般无奈之下,菜鸟又只有麻烦一下各位大虾了,谢谢!!源程序如下:

#include "linux/module.h"
#include "linux/kernel.h"
#include "linux/version.h"
#include "linux/config.h"
#include "linux/fs.h"
#include "linux/init.h"

MODULE_LICENSE("GPL";

//读设备
static ssize_t demomodule_read(struct file *file,char *buf,size_t count,loff_t *f_pos)
{
printk("reading my device...\n";
}


//写设备
static ssize_t demomodule_write(struct file *file,const char *buf,size_t count,loff_t *f_pos)
{
printk("writing my device...\n";
}


//文件操作结果,错误提示中好象是这里有问题,但是我确实找不出来啊
static struct file_operations demomodule_fops =
{
read:demomodule_read,     //我改为.read = demomodule_read 还是不行
write:demomodule_write,
};


//初始化模块
int init_module(void)
{
int flag = register_chrdev( 250 , "demomodule" , &demomodule_fops );

if( flag<0 )
{
   printk("the device init is failed !\n";
   return 1;
}
else
   printk("register my device succeed !\n";
return 0;
}


//卸载模块
void clearnup_module(void)
{
int flag = unregister_chrdev( 250 , "demomodule";

if( flag<0 )
   printk("can not unregister the device !\n";
else
   printk("unregister device succeed !\n";
}



上面就是我的源程序,下面是我的makefile文件内容:

compile = gcc -DMODULE -D_KERNEL_DLINUX -I/usr/src/linux-2.4.20-8/include -c demomodule.c
#我改为compile = gcc -DMODULE -D_KERNEL_DLINUX -I/usr/src/linux-2.4/include -c demomodule.c还是不行
demomodule.o : demomodule.c
               $(compile)


我是在winxp的VM下调试的,装的RH9.0,内核是2.4.20-8,出错信息请见下面图片,请各位大虾指点一下吧!
谢谢各位了!!!

编译错误.JPG (67.79 KB, 下载次数: 36)

编译错误.JPG

最佳答案

查看完整内容

我在rh7.2上试了是可以的gcc -D__KERNEL__ -I/usr/include -c drv.c

论坛徽章:
0
2 [报告]
发表于 2007-11-13 16:33 |只看该作者
我在rh7.2上试了是可以的

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. //#include <linux/version.h>
  4. //#include <linux/config.h>
  5. #include <linux/fs.h>
  6. //#include <linux/init.h>

  7. //MODULE_LICENSE("GPL");

  8. //读设备
  9. static ssize_t demomodule_read(struct file *file,char *buf,size_t count,loff_t *f_pos)
  10. {
  11. printk("reading my device...\n");
  12. }


  13. //写设备
  14. static ssize_t demomodule_write(struct file *file,const char *buf,size_t count,loff_t *f_pos)
  15. {
  16. printk("writing my device...\n");
  17. }


  18. //文件操作结果,错误提示中好象是这里有问题,但是我确实找不出来啊
  19. static struct file_operations demomodule_fops =
  20. {
  21. read:demomodule_read,     //我改为.read = demomodule_read 还是不行
  22. write:demomodule_write,
  23. };


  24. //初始化模块
  25. int init_module(void)
  26. {
  27. int flag = register_chrdev( 250 , "demomodule" , &demomodule_fops );

  28. if( flag<0 )
  29. {
  30.    printk("the device init is failed !\n");
  31.    return 1;
  32. }
  33. else
  34.    printk("register my device succeed !\n");
  35. return 0;
  36. }


  37. //卸载模块
  38. void clearnup_module(void)
  39. {
  40. int flag = unregister_chrdev( 250 , "demomodule");

  41. if( flag<0 )
  42.    printk("can not unregister the device !\n");
  43. else
  44.    printk("unregister device succeed !\n");
  45. }

复制代码


gcc -D__KERNEL__ -I/usr/include -c drv.c

论坛徽章:
0
3 [报告]
发表于 2007-11-14 10:23 |只看该作者
只要一个头文件就可以了
#include <linux/fs.h>

[root@filter ~]$ gcc -D__KERNEL__  -I/usr/src/linux-2.4.20-8/include  -c drv.c

报错
/usr/src/linux-2.4.20-8/include/asm/processor.h:83: error: array type has incomplete element type
发现
83行是 extern struct tss_struct init_tss[NR_CPUS];
而struct tss_struct是在该文件的后面定义的.(不解为何内核编译时未报错)
把init_tss移到tss_struct定义后,重新编译,有警告,但是能成功

论坛徽章:
0
4 [报告]
发表于 2007-11-14 10:53 |只看该作者
首先谢谢楼上的!
但是我这个在rh9.0,内核为2.4.20-8下调试始终有错啊!请问你的内核版本是好多啊?
谢谢!

论坛徽章:
0
5 [报告]
发表于 2007-11-14 10:57 |只看该作者
应该是read write赋值的地方出现了错误

论坛徽章:
0
6 [报告]
发表于 2007-11-14 11:08 |只看该作者
谢谢楼上的!
但是我两种方式都试了啊?一种是程序中的写发:
read:demomodule_read,
write:demomodule_write,
另一种写法:
.read = demomodule_read,
.write = demomodule_write,
还是不行啊!:(

不知道是哪里有问题啊?:(

谢谢!

论坛徽章:
0
7 [报告]
发表于 2007-11-14 17:32 |只看该作者
刚才详细查了一下出错信息,发现可能是文件fs.h中的结构体file_operations没有成功地被引用,但我如何才能正确地引用fs.h中的这个结构体呢?我实在想不出其它办法哇!!:(

请各位再帮帮我吧!谢谢了!!

论坛徽章:
0
8 [报告]
发表于 2007-11-14 19:56 |只看该作者
你fs.h包含的有问题、编译过程已经提示你struct file和struct file_operations这两个结构体都不能识别。
别人的系统中那么包含头文件可以、不说明在你的系统中就可以。

论坛徽章:
0
9 [报告]
发表于 2007-11-15 10:00 |只看该作者
把#include ""改成#include <>

论坛徽章:
0
10 [报告]
发表于 2007-11-15 10:19 |只看该作者
谢谢楼上的 qtdszws (异次元空间)  !

我改成了<>,可是还是不得行!:)

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

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP