Friday, April 17, 2009

Kernel: Compiling a simple module

Here a simple for beginner to compile their first kernel module:

1. Create a simple Makefile:

obj-m += mymodule.o

2. Compile and link the module, notice you should replace your kernel source:

$make -C /usr/src/linux SUBDIRS=$PWD modules

3. Use insmod or rmmod to insert and remove your module into kernel space.
4. Watch your module log message in the kernel ring buffer via dmesg or tailing /var/log/messages only if you did some debugging info.

Update on 09/09/2011

Another method is to create complete Makefile script for example

KERNEL_DIR = /usr/src/linux
SOURCE_DIR = /project/cpu

obj-m += cpu.o

    make -C ${KERNEL_DIR} SUBDIRS=${SOURCE_DIR} modules

    make -C ${KERNEL_DIR} SUBDIRS=${SOURCE_DIR} clean

Have many fun...

Linux Kernel Project