/* hello.c */ #include <linux/kernel.h> #include <linux/module.h> static int hello_init(void) { printk(KERN_ALERT "hello init"\n); return 0; } static void hello_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 (will encountered with error: include/linux/linkage.h:5:25: fatal error: asm/linkage.h: No such file or directory)
#Makefile #Makefile for a basic kernel module CC=gcc MODCFLAGS:= -Wall -DMODULE -D__KERNEL__ -DLINUX INCLUDE:= -IpathToLinuxKernelSrcInclude hello.o:hello.c $(CC) $(MODCFLAGS) $(INCLUDE) -c hello.c echo insmod hello.o to turn it on echo rmmod hello to turn if off echo echo X and kernel programming do not mix. echo Do the insmod and rmmod from outside X # Note: A "tab" before "$(CC) xxx" means this is a shell command
A Makefile format as KBuild after linux2.6
# Makefile obj-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
时间: 2024-12-27 20:12:29