从其构建内核模块和向其插入模块的内核应具有相同的版本。如果您不想照顾这件事,可以使用以下Makefile。
obj?m += hello?world.o
all:
make ?C /lib/modules/$(shell uname ?r)/build M=$(PWD) modules
clean:
make ?C /lib/modules/$(shell uname ?r)/build M=$(PWD) clean
现在,您可以构建并尝试插入模块。
我建议您尽可能在此行之前成为root
$ sudo cp /boot/config-2.6.38-8-generic ./.config
$su
#cp /boot/config-2.6.38-8-generic ./.config
#insmod hello_world.ko
另外,您也可以使用以下make文件
TARGET := hello-world
WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC := gcc-3.0
${TARGET}.o: ${TARGET}.c
.PHONY: clean
clean:
rm -rf ${TARGET}.o