#include<linux/init.h> #include<linux/module.h> static int __init hello_init(void) { printk("Hello init "); return 0; } static int __exit hello_exit(void) { printk("Hello exit "); } module_init(hello_init); module_exit(hello_exit);
交叉编译 Makefile文件(ARM):
ifneq ($(KERNELRELEASE),) obj-m := hello.o else KDIR := /usr/src/linux2.6.28 all: make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux- clean: rm -f *.ko *.o *.mod.o *.mod.c *.symvers modul* endif
X86:
## Makefile template. obj-m := hello.o UNAME := $(shell uname -r) PWD := $(shell pwd) ADVMOD := hello defualt: @make -C /lib/modules/$(UNAME)/build SUBDIRS=$(PWD) modules clean: @rm -f *.o @rm -f *.ko @rm -f *.mod.c @rm -f .*.cmd @rm -rf .tmp_versions #endif
Linux下:
OBJ = qudong ifneq ($(KERNELRELEASE),) obj-m := $(OBJ).o else KDIR := /usr/src/linux-headers-3.2.0-20-generic-pae all: rmmod $(OBJ) make -C $(KDIR) M=$(PWD) modules insmod $(OBJ).ko clean: rm -f *.ko *.o *.mod.o *.mod.c *.symvers modul* endif