yezi_wyf
驱动牛犊
驱动牛犊
  • 注册日期2004-04-20
  • 最后登录2009-05-25
  • 粉丝0
  • 关注0
  • 积分3分
  • 威望30点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1952回复:5

急:一个linux字符驱动的编译问题,关于file_operations

楼主#
更多 发布于:2004-05-20 20:26

一个简单的字符设备程序,大致如下
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/kernel.h>

struct file_operations test_fops = {
read :read_test;
write write_test;
open:open_test;
release:realse_test;
};
static int read_test(struct inode *node,struct file *file,char *buf,int count)
static int write_test(struct inode *inode,struct file *file,const char *buf,int count);

static int open_test(struct inode *inode,struct file *file );

void init_module()
{
}
void clean_module()
{

}



什么也没做,编译用 gcc -DMODULE -D_KERNEL_ test.c

老是说storage of test_fops size isn\'t known ,
我在我rh9 的/usr/include/linux/fs.h里没找到file_operations 的定义,在/usr/src/linux-2.4.8-20/include/linux/fs.h里有file_operations 的定义,但我如果指出库文件路径/usr/src/linux-2.4.8-20/include,就由很多和多的fs.h里的错误出来,
问题比较低级,但我不知道为什么,大家救救我吧


yezi_wyf
驱动牛犊
驱动牛犊
  • 注册日期2004-04-20
  • 最后登录2009-05-25
  • 粉丝0
  • 关注0
  • 积分3分
  • 威望30点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-05-20 21:34
代码
# define __NO_VERSION__
# include <linux/module.h>
# include <linux/fs.h>
# include <linux/kernel.h>

struct file_operations test_fops= {
 read : read_test,
 write: write_test,
 open: open_test,
 release: release_test,
 };
unsigned int test_major = 0;

static int read_test(struct inode *node,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 int open_test(struct inode *inode,struct file *file )

{

 return 0;

}


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


 

int init_module(void)

{

int result;
prinrtk(\"<1>hello world\");

result = register_chrdev(0, \"test\", &test_fops);

if (result < 0) {

printk(\"test: can\'t get major number \");

return result;

}

if (test_major == 0) test_major = result;

return 0;

}



void cleanup_module(void)

{

prinrtk(\"<1>bye bye\");

   unregister_chrdev(test_major, \"test\");

}



错误:

moduletest.c:48: variable `test_fops\' has initializer but incomplete type
moduletest.c:49: unknown field `read\' specified in initializer
moduletest.c:49: warning: excess elements in struct initializer
moduletest.c:49: warning: (near initialization for `test_fops\')
moduletest.c:50: unknown field `write\' specified in initializer
moduletest.c:50: warning: excess elements in struct initializer
moduletest.c:50: warning: (near initialization for `test_fops\')
moduletest.c:51: unknown field `open\' specified in initializer
moduletest.c:51: warning: excess elements in struct initializer
moduletest.c:51: warning: (near initialization for `test_fops\')
moduletest.c:52: unknown field `release\' specified in initializer
moduletest.c:52: warning: excess elements in struct initializer
moduletest.c:52: warning: (near initialization for `test_fops\')
moduletest.c: In function `init_module\':
moduletest.c:58: warning: implicit declaration of function `prinrtk\'
moduletest.c:59: warning: implicit declaration of function `register_chrdev\'
moduletest.c:61: warning: implicit declaration of function `printk\'
moduletest.c: In function `cleanup_module\':
moduletest.c:71: warning: implicit declaration of function `unregister_chrdev\'
moduletest.c: At top level:
moduletest.c:48: storage size of `test_fops\' isn\'t known

我在我rh9 的/usr/include/linux/fs.h里没找到file_operations 的定义,在/usr/src/linux-2.4.8-20/include/linux/fs.h里有file_operations 的定义,但我如果指出库文件路径/usr/src/linux-2.4.8-20/include,就由很多和多的fs.h里的错误出来.
yezi_wyf
驱动牛犊
驱动牛犊
  • 注册日期2004-04-20
  • 最后登录2009-05-25
  • 粉丝0
  • 关注0
  • 积分3分
  • 威望30点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-05-20 22:00
报警信息:
gcc -D__KERNEL__ -DMODULE -I/usr/src/linux-2.4.20-8/include\\ -O -Wall   -c -o moduletest.o moduletest.c
In file included from /usr/include/linux/fs.h:23,
                 from moduletest.c:3:
/usr/include/linux/string.h:8:2: warning: #warning Using kernel header in userland!



我用了-I/usr/src/linux-2.4.20-8/include\\ ,为什么n file included from /usr/include/linux/fs.h:23,好奇怪.
llqm1202
驱动牛犊
驱动牛犊
  • 注册日期2003-07-29
  • 最后登录2006-10-16
  • 粉丝0
  • 关注0
  • 积分40分
  • 威望6点
  • 贡献值0点
  • 好评度2点
  • 原创分0分
  • 专家分0分
地板#
发布于:2004-05-26 10:51
我刚开始时也出现过此错误,我把usr/include下的linux链接文件重新链接产生新的,再编译时就没有这些错误了。
chopin_1998
驱动牛犊
驱动牛犊
  • 注册日期2004-04-01
  • 最后登录2005-03-30
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2004-05-26 11:51
这位大哥,您是用的自己编译的内核吧?
如果是这样,您需要制定头文件路径到新内核。或者修改/usr/inlucde/linux,使其链接到新内核。
Linux Power!
yezi_wyf
驱动牛犊
驱动牛犊
  • 注册日期2004-04-20
  • 最后登录2009-05-25
  • 粉丝0
  • 关注0
  • 积分3分
  • 威望30点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
5楼#
发布于:2004-05-27 12:36
谢谢大家,这个问题解决了,主要是我的makefile文件写的格式不对
,并没有执行-I/后的路径,而继续采用默认路径的问题。就是TAB键出了问题。
游客

返回顶部