第一个例子:hello.c
#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 "Goodbay, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile:
# Makefile2.6
ifneq ($(KERNELRELEASE),)
#kbuild syntax. dependency relationshsip of files and target modules are listed here.
mymodule-objs := hello.o
obj-m := hello.o
else
PWD := $(shell pwd)
KVER ?= $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
$(MAKE) -C $(KDIR) M=$(PWD)
clean:
rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions
endif
执行make
root@dw:/home/dengwei/eclipse_workspace/hello# make
make -C /lib/modules/2.6.31-20-generic/build M=/home/dengwei/eclipse_workspace/hello
make[1]: 正在进入目录 `/usr/src/linux-headers-2.6.31-20-generic'
LD /home/dengwei/eclipse_workspace/hello/built-in.o
CC [M] /home/dengwei/eclipse_workspace/hello/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/dengwei/eclipse_workspace/hello/hello.mod.o
LD [M] /home/dengwei/eclipse_workspace/hello/hello.ko
make[1]:正在离开目录 `/usr/src/linux-headers-2.6.31-20-generic'
root@dw:/home/dengwei/eclipse_workspace/hello# ls
built-in.o hello.ko hello.mod.o Makefile Module.markers Module.symvers
hello.c hello.mod.c hello.o Makefile~ modules.order
root@dw:/home/dengwei/eclipse_workspace/hello# insmod ./hello.ko
root@dw:/home/dengwei/eclipse_workspace/hello# insmod | grep hello
Usage: insmod filename [args]
root@dw:/home/dengwei/eclipse_workspace/hello# cat /var/log/syslog |grep world
Apr 12 14:34:43 dw kernel: [ 8728.921670] Hello, world
到此第一个模块建立成功。
参考网址:http://shmilyyforever.popo.blog.163.com/blog/static/93463612009128002485/
其中出现了很多错误,makefile文件值得好好分析。
从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响
http://www.ibm.com/developerworks/cn/linux/l-module26/#author
======================================================================
在tq2440上的结果:
[root@(none) hello]# insmod hello.ko
hello: module license 'DUal BSD/GPL' taints kernel.
Disabling lock debugging due to kernel taint
Hello, world
[root@(none) hello]#
阅读(552) | 评论(0) | 转发(0) |