As discussed earlier a Linux loadable kernel module (LKM) is different from other binary executables. Hence, it cannot be compiled the way we compile normal C files. Compiling a Linux module is a separate process. We use the help of kernel Makefile for compilation. The makefile we will have contents as given below. When you are done with make command, it will generate ‘mychardev.ko’ module in the working directory.
obj-m := mychardev.o
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
The following set of commands will load the driver modules and mount them as a device (character device with major# — 251 and minor# — 0)
sudo insmod mychardev.ko
sudo mknod /dev/mychardev c 251 0
sudo chmod 777 /dev/mychardev
sudo rmmod mychardev
sudo rm /dev/mychardev
After compiling and loading the driver, Write a simple c program to open file ‘/dev/mychardev’ in read mode and read content print it. It should give driver implemented functionality.