测试环境:fedora 10,devkit8000
源代码
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile如下:
ARCH=arm
CROSS_COMPILE=arm-none-linux-gnueabi-
obj-m := hello.o
# KERNELDIR ?= /lib/modules/$(shell uname -r)/build
KERNELDIR ?= /home/omap/linux-2.6.28-omap
# The current directory is passed to sub-makes as argument
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
测试环境是arm,如果是在计算机上测试,则要改2个地方:
(1)去掉
ARCH=arm
CROSS_COMPILE=arm-none-linux-gnueabi-
(2)用KERNELDIR ?= /lib/modules/$(shell uname -r)/build替代
KERNELDIR ?= /home/omap/linux-2.6.28-omap
编译下
[omap@localhost ldd3]$ sudo make
[sudo] password for omap:
make -C /home/omap/linux-2.6.28-omap M=/mnt/hgfs/linux_share/ldd3 modules
make[1]: Entering directory `/home/omap/linux-2.6.28-omap'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/home/omap/linux-2.6.28-omap'
[omap@localhost ldd3]$
测试一下:
[\u\@\h \W]# ls
hello.ko
[\u\@\h \W]# insmod hello.ko
Hello, world
[\u\@\h \W]# rmmod hello
Goodbye, cruel world
rmmod: module 'hello' not found
[\u\@\h \W]#
rmmod: module 'hello' not found这句话不需要管,如果要深究的话,改下busybox源程序就可以了。