next up previous contents index
Next: Multiple File Kernel Modules Up: Hello, world Previous: Hello, world

Makefiles for Kernel Modules

A kernel module is not an independant executable, but an object file which will be linked into the kernel in runtime. As a result, they should be compiled with the -c flag. Also, all kernel modules have to be compiled with certain symbols defined.  

There are other symbols which have to be included, or not, depending on the flags the kernel was compiled with. If you're not sure how the kernel was compiled, look it up in /usr/include/linux/config.h      

ex Makefile   

# Makefile for a basic kernel module


hello.o:        hello.c /usr/include/linux/version.h
                $(CC) $(MODCFLAGS) -c hello.c
                echo insmod hello.o to turn it on
                echo rmmod hello to turn if off
                echo X and kernel programming do not mix.
                echo Do the insmod and rmmod from outside X.

So, now the only thing left is to su to root (you didn't compile this as root, did you? Living on the edge1.1...), and then insmod hello and rmmod hello to your heart's content. While you do it, notice your new kernel module in /proc/modules.        

By the way, the reason why the Makefile recommends against doing insmod from X is because when the kernel has a message to print with printk, it sends it to the console. When you don't use X, it just goes to the virtual terminal you're using (the one you chose with Alt-F<n>) and you see it. When you do use X, on the other hand, there are two possibilities. Either you have a console open with xterm -C, in which case the output will be sent there, or you don't, in which case the output will go to virtual terminal 7 -- the one `covered' by X.            

If your kernel becomes unstable you're likelier to get the debug messages without X. Outside of X, printk goes directly from the kernel to the console. In X, on the other hand, printk's go to a user mode process (xterm -C). When that process receives CPU time, it is supposed to send it to the X server process. Then, when the X server receives the CPU, it is supposed to display it -- but an unstable kernel usually means that the system is about to crash or reboot, so you don't want to delay the error messages, which might explain to you what went wrong, for longer than you have to.

next up previous contents index
Next: Multiple File Kernel Modules Up: Hello, world Previous: Hello, world