Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)
edited by

I am working on the Linux Kernel Module Programming Guide, and meanwhile I got stuck in between on the character device drivers example.

Consider Makefiles in the previous examples which were provided, using that example I tried to make one:

obj-m += chardev.o

all:

    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:

    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

The output I'm getting is:

maciej@jadwiga:~/Projects/os/chardev$ make

make -C /lib/modules/2.6.26-2-686/build M=/home/maciej/Projects/os/chardev modules

make[1]: Entering directory `/usr/src/linux-headers-2.6.26-2-686'

  CC [M] /home/maciej/Projects/os/chardev/chardev.o

/home/maciej/Projects/os/chardev/chardev.c: In function ‘cleanup_module’:

/home/maciej/Projects/os/chardev/chardev.c:72: error: void value not ignored as it ought to be

make[2]: *** [/home/maciej/Projects/os/chardev/chardev.o] Error 1

make[1]: *** [_module_/home/maciej/Projects/os/chardev] Error 2

make[1]: Leaving directory `/usr/src/linux-headers-2.6.26-2-686'

make: *** [all] Error 2

Can anybody help?

The file I want to compile is:

/*

 * chardev.c: Creates a read-only char device that says how many times

 * you've read from the dev file

 */

#include <linux/kernel.h>

#include <linux/module.h>

#include <linux/fs.h>

#include <asm/uaccess.h> /* 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,

1 Answer

0 votes
by (36.8k points)

You can use the below command:

obj-m += chardev.o

Come and join Linux training to gain great knowledge. 

Do check out the video below

 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Feb 17, 2020 in DevOps and Agile by anmolj (9k points)
0 votes
1 answer
asked Jan 24, 2021 in Linux by dev_sk2311 (45k points)

Browse Categories

...