/*hello.c*/#include<linux/kernel.h>#include<linux/module.h>Static intHello_init (void) {PRINTK (Kern_alert"Hello init"\ n); return 0;}Static voidHello_exit (void) {PRINTK (Kern_alert"Hello exit\n");} Module_init (Hello_init); Module_exit (Hello_exit); Module_license ("Dual BSD/GPL");
A Makefile format before linux2.6 (would encountered with Error:include/linux/linkage.h:5:25:fatal Error:asm/linkage.h: No such file or directory)
#Makefile #makefile fora basic kernel MODULECC=GCCModcflags:=-wall-dmodule-d__kernel__-Dlinuxinclude:= -ipathtolinuxkernelsrcincludehello.o:hello.c $ (CC) $ (modcflags) $ (INCLUDE)-C hello.cEcho Insmodhello.o to turn it onEchoRmmod Hello to turnifoffEcho EchoX and kernel programming DoNot mix. EchoDo theInsmodand Rmmod from outside x# note:a"Tab"Before"$ (CC) XXX"Means this is a shell command
A Makefile format as kbuild after linux2.6
# Makefileobj-M: = hello.o
# use "make-c/opt/linux-3.0.4 subdirs= $PWD modules" to compile this driver
# Insmod Hello.ko
# Cat/proc/modules
# Lsmod
For Kbuild kernel driver programming, pls reference to the Linux Kernel Module Programming Guide
A General ' Hello World ' src code for linux3.0 driver programing