将错误别名为未定义的符号devone_init

时间:2018-02-03 05:12:24

标签: c gcc makefile driver linux-device-driver

我刚刚制作了一个驱动程序,当我执行make时,它说 将错误别名为未定义的符号devone_init 我发现很多网站都找不到相同的错误。因为我是新手 设备编程,我不清楚init_module。

#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/sched.h>
#include <asm/current.h>
#include <asm/uaccess.h>

MODULE_LICENCE("Dual BSD/GPL");

#define DRIVER_NAME "devone";

static int devone_devs = 2;
static int devone_major = 0;
module_param(devone_major. uint, 0);
static struct cdev devone_cdev;

static int devone_open(struct inode *inode, struct file *file)
{
    printk("%s: major %d minor %d (pid %d)\n", __func__, imajor(inode), iminor(inode), current->pid);
    inode->i_private = inode;
    file->private_data = file;

    printk("i_private=%p private_data=%p\n", inode->i_private, file->private_data);

    return 0;
}

static int devone_close(struct inode *inode, struct file *file)
{
    printk("%s: major %d minor %d (pid %d)\n", __func__, imajor(inode), iminor(inode), current->pid);
    inode->i_private = inode;
    file->private_data = file;

    printk("i_private=%p private_data=%p\n", inode->i_private, file->private_data);

    return 0;
}

struct file_operations devone_fops = {
    .open = devone_opens;
    .release = devone_close;
}

static int devone_init(void)
{
    dev_t dev = MKDEV(devone_major, 0);
    int alloc_ret = 0;
    int major;
    int cdev_err = 0;

    alloc_ret = alloc_chrdev_region(&dev, 0, devone_devs, DRIVER_NAME);
    if(alloc_ret)
        goto error;
    devone_major = major = MAJOR(dev);

    cdev_init(&devone_cdev, &devone_fops);
    devone_cdev.owner = THIS_MODULE;

    cdev_err = cdev_add(&devone_cdev, MKDEV(devone_major, 0), devone_devs);

    if(cdev_err)
        goto error;

    printk(KERN_ALERT "%s driver(%d) loaded\n", DRIVER_NAME, major);
    return 0;

error:
    if(cdev_err == 0)
        cdev_del(devone_cdev);

    if(alloc_ret == 0)
        unregister_chrdev_region(dev, devone_devs);

    return -1;
}

static void devone_exit(void)
{
    dev_t dev = MKDEV(devone_major, 0);

    cdev_del(&devone_cdev);

    cdev_del(&devone_cdev);
    unregister_chrdev_region(dev, devone_devs);

    printk(KERN_ALERT "%s driver unloaded\n", DRIVER_NAME);
}

module_init(devone_init);
module_exit(devone_exit);

这是我的makefile。当我添加cflags时,它返回了cflags被更改。

#CFLAGS += -Wall
CFILES = devone.c

obj-m += sample.o

KDIR =/lib/modules/$(shell uname -r)/build
sample-objs := $(CFILES:.c=.o)

all:
    $(MAKE) -C $(KDIR)  M=$(shell pwd)  modules
clean:
    rm  -rf *.o *.ko    *.mod.* *.symvers   *.order

1 个答案:

答案 0 :(得分:-1)

代码中有几个小问题阻止了编译。正确编译的修改代码如下。你可以比较并找出差异。

#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/sched.h>
#include <asm/current.h>
#include <asm/uaccess.h>


#define DRIVER_NAME "devone"

static int devone_devs = 2;
static int devone_major = 0;
module_param(devone_major, uint, 0);
static struct cdev devone_cdev;

static int devone_open(struct inode *inode, struct file *file)
{
    printk("%s: major %d minor %d (pid %d)\n", __func__, imajor(inode), iminor(inode), current->pid);
    inode->i_private = inode;
    file->private_data = file;

    printk("i_private=%p private_data=%p\n", inode->i_private, file->private_data);

    return 0;
}

static int devone_close(struct inode *inode, struct file *file)
{
    printk("%s: major %d minor %d (pid %d)\n", __func__, imajor(inode), iminor(inode), current->pid);
    inode->i_private = inode;
    file->private_data = file;

    printk("i_private=%p private_data=%p\n", inode->i_private, file->private_data);

    return 0;
}

struct file_operations devone_fops = {
    .open = devone_open,
    .release = devone_close,
};

static int devone_init(void)
{
    dev_t dev = MKDEV(devone_major, 0);
    int alloc_ret = 0;
    int major;
    int cdev_err = 0;

    alloc_ret = alloc_chrdev_region(&dev, 0, devone_devs, DRIVER_NAME);
    if(alloc_ret)
        goto error;
    devone_major = major = MAJOR(dev);

    cdev_init(&devone_cdev, &devone_fops);
    devone_cdev.owner = THIS_MODULE;

    cdev_err = cdev_add(&devone_cdev, MKDEV(devone_major, 0), devone_devs);

    if(cdev_err)
        goto error;

    printk(KERN_ALERT "%s driver(%d) loaded\n", DRIVER_NAME, major);
    return 0;

error:
    if(cdev_err == 0)
        cdev_del(&devone_cdev);

    if(alloc_ret == 0)
        unregister_chrdev_region(dev, devone_devs);

    return -1;
}

static void devone_exit(void)
{
    dev_t dev = MKDEV(devone_major, 0);

    cdev_del(&devone_cdev);

    cdev_del(&devone_cdev);
    unregister_chrdev_region(dev, devone_devs);

    printk(KERN_ALERT "%s driver unloaded\n", DRIVER_NAME);
}

module_init(devone_init);
module_exit(devone_exit);



MODULE_LICENSE("Dual BSD/GPL");