当前位置: 技术问答>linux和unix
请教一个linux字符设备文件的问题
来源: 互联网 发布时间:2016-11-11
本文导语: 我刚开始学习内核编程,看的是The Linux Kernel Module Programming Guide,2.6版的,并且重新编译了内核2.6.36。我编译Example4-1.chardev.c那个程序的时候出现了以下的问题: chardev.c:In function init_module: chardev.c:30:error:implicit de...
我刚开始学习内核编程,看的是The Linux Kernel Module Programming Guide,2.6版的,并且重新编译了内核2.6.36。我编译Example4-1.chardev.c那个程序的时候出现了以下的问题:
chardev.c:In function init_module:
chardev.c:30:error:implicit declaration of function register_chrdev
chardev.c:In function cleanup_module:
chardev.c:47:error:implicit declaration of function unregister_chrdev
chardev.c:At top level:
chardev.c:54:warning:struct inode declared inside parameter list
chardev.c:54:error:conflicting types for device_open
chardev.c:8:error:prevoius declaration of device_open was here
chardev.c:66:warning:struct inode declared inside parameter list
chardev.c:66:error:conflicting types for device_release
chardev.c:9:error:prevoius declaration of device_release was here
chardev.c:89:error:conflicting types for device_write
chardev.c:11:error:prevoius declaration of device_write was here
请问各位高手是什么原因?非常感谢!
源代码如下:
/*
* chardev.c: Creates a read-only char device that says how many times
* you've read from the dev file
*/
#include
#include
#include
#include /* for put_user */
/*
* Prototypes - this would normally go in a .h file
*/
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
#define SUCCESS 0
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
#define BUF_LEN 80 /* Max length of the message from the device */
/*
* Global variables are declared as static, so are global within the file.
*/
static int Major; /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open?
* Used to prevent multiple access to device */
static char msg[BUF_LEN]; /* The msg the device will give when asked */
static char *msg_Ptr;
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
/*
* This function is called when the module is loaded
*/
int init_module(void)
{
Major = register_chrdev(0, DEVICE_NAME, &fops);
if (Major /dev/hello
*/
static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
{
printk(KERN_ALERT "Sorry, this operation isn't supported.n");
return -EINVAL;
}
chardev.c:In function init_module:
chardev.c:30:error:implicit declaration of function register_chrdev
chardev.c:In function cleanup_module:
chardev.c:47:error:implicit declaration of function unregister_chrdev
chardev.c:At top level:
chardev.c:54:warning:struct inode declared inside parameter list
chardev.c:54:error:conflicting types for device_open
chardev.c:8:error:prevoius declaration of device_open was here
chardev.c:66:warning:struct inode declared inside parameter list
chardev.c:66:error:conflicting types for device_release
chardev.c:9:error:prevoius declaration of device_release was here
chardev.c:89:error:conflicting types for device_write
chardev.c:11:error:prevoius declaration of device_write was here
请问各位高手是什么原因?非常感谢!
源代码如下:
/*
* chardev.c: Creates a read-only char device that says how many times
* you've read from the dev file
*/
#include
#include
#include
#include /* for put_user */
/*
* Prototypes - this would normally go in a .h file
*/
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
#define SUCCESS 0
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
#define BUF_LEN 80 /* Max length of the message from the device */
/*
* Global variables are declared as static, so are global within the file.
*/
static int Major; /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open?
* Used to prevent multiple access to device */
static char msg[BUF_LEN]; /* The msg the device will give when asked */
static char *msg_Ptr;
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
/*
* This function is called when the module is loaded
*/
int init_module(void)
{
Major = register_chrdev(0, DEVICE_NAME, &fops);
if (Major /dev/hello
*/
static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
{
printk(KERN_ALERT "Sorry, this operation isn't supported.n");
return -EINVAL;
}
|
linux-2.6.23版本上把这个返回值从int改为void了。
commit e53252d97e670a38b1d2e9723b48077bba11ddda
Author: Akinobu Mita
Date: Thu Jul 19 01:47:51 2007 -0700
unregister_chrdev() return void
unregister_chrdev() does not return meaningful value. This patch makes it
return void like most unregister_* functions.
Signed-off-by: Akinobu Mita
Signed-off-by: Andrew Morton
Signed-off-by: Linus Torvalds
commit e53252d97e670a38b1d2e9723b48077bba11ddda
Author: Akinobu Mita
Date: Thu Jul 19 01:47:51 2007 -0700
unregister_chrdev() return void
unregister_chrdev() does not return meaningful value. This patch makes it
return void like most unregister_* functions.
Signed-off-by: Akinobu Mita
Signed-off-by: Andrew Morton
Signed-off-by: Linus Torvalds