In order to compile the driver, it is necessary to have the Linux Kernel source tree.
Assuming the sources are at /lib/modules/<kernel-version>
, the following Makefile will compile the file driver.c
into the driver.ko
Kernel Object
obj-m := driver.o
KDIR := /lib/modules/$(shell uname -r)/build/
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
Notice how this Makefile calls make
in the build directory of the Kernel.
When the compilation step finishes successfully, the src directory of the driver will look somewhat like this:
driver.c driver.ko driver.mod.c driver.mod.o driver.o Makefile modules.order Module.symvers
In order to "run" the module, it is necessary to insert into the running kernel:
$ insmod driver.ko
$ dmesg | tail -n 1
[133790.762185] Hello, World!
$ rmmod driver.ko
$ dmesg | tail -n 1
[133790.762185] Goodbye, cruel world...