日期:2014-05-16  浏览次数:20753 次

Linux下编译驱动,出了很多问题,希望各位帮我看看!
[root@localhost root]# gcc -c -nostdinc firstmodule.c
firstmodule.c: In function `init_module':
firstmodule.c:13: warning: `struct file' declared inside parameter list
firstmodule.c:13: warning: its scope is only this definition or declaration, which is probably not what you want
firstmodule.c:13: warning: `struct inode' declared inside parameter list
firstmodule.c:19: warning: `struct file' declared inside parameter list
firstmodule.c:19: warning: `struct inode' declared inside parameter list
firstmodule.c: In function `release':
firstmodule.c:23: warning: `return' with a value, in function returning void
firstmodule.c: In function `init_module':
firstmodule.c:28: warning: `struct file' declared inside parameter list
firstmodule.c:28: warning: `struct inode' declared inside parameter list
firstmodule.c: In function `read':
firstmodule.c:31: `VERIFY_WRITE' undeclared (first use in this function)
firstmodule.c:31: (Each undeclared identifier is reported only once
firstmodule.c:31: for each function it appears in.)
firstmodule.c:31: `DEFAULT' undeclared (first use in this function)
firstmodule.c: In function `init_module':
firstmodule.c:40: warning: `struct file' declared inside parameter list
firstmodule.c:40: warning: `struct inode' declared inside parameter list

程序源码如下所示:

#include <linux/kernel.h> // 在内核模块中共享
#include <linux/module.h> // 一个模块
#include <linux/init.h>
//#include <linux/fs.h>
//处理CONFIG_MODVERSIONS
#if CONFIG_MODVERSIONS == 1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
int init_module() // 初始化模块  
{
printk("Hello! This is a testing module! \n");
int open(struct inode *inode, struct file *filp)
{
MOD_INC_USE_COUNT; // 增加该模块的用户数目
printk("This module is in open!\n");
return 0;
}
void release(struct inode *inode, struct file *filp)
{
MOD_DEC_USE_COUNT; //该模块的用户数目减1
printk("This module is in release!\n");
return 0;
#ifdef DEBUG 
  printk(“release(%p,%p)\n”,inode,filp);
#endif 
}
int read(struct inode *inode, struct file *filp, char *buf, int count)
{
int leave;
if(verify_area(VERIFY_WRITE, buf, count) == DEFAULT)
return DEFAULT;
for(leave=count; leave>0; leave--)
{
__put_user(1,buf,1);
buf++;
}
return count;
}
int write(struct inode *inode, struct file *filp, const char *buf, int count)
{
return count;
}
return 0;
}
void cleanup_module()//取消init_module()函数所做的打印功能操作 
{
printk("Sorry!The testing module is unloading now! \n");
}


------解决方案--------------------
你不能在定义一个函数里面定义另外一个函数(个人理解,仅供参考)。具体如下:
举例:
int init_module() // 初始化模块

printk("Hello! This is a testing module! \n"); 
int open(struct inode *inode, struct file *filp) 

MOD_INC_USE_COUNT; // 增加该模块的用户数目 
printk("This module is in open!\n"); 
return 0; 

你在init_module这个函数里面定义了另外一个函数int open(struct inode *inode, struct file *filp) 这个是在c
语言中不被允许的。如果你的open函数只是调用那请把int去掉。后面的函数同第一个。请版主改一下再试试!