主程序:
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk("<1> Hello, world/n");
return 0;
}
static void hello_exit(void)
{
printk("<1> Goodbye, cruel world/n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile:
#KERNELDIR = /home/blan/build/kernel
KERNELDIR = /usr/src/linux-headers-2.6.32-21-generic
# The current directory is passed to sub-makes as argument
PWD := $(shell pwd)
INSTALLDIR = /lib/modules
CC = gcc
obj-m := helloworld.o
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
cp helloworld.ko $(INSTALLDIR)
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
本文介绍了一个简单的Linux模块示例,包括主程序和Makefile配置。该模块在加载时打印“Hello, world”,卸载时打印“Goodbye, cruel world”。通过这个例子可以了解Linux内核模块的基本结构和构建过程。

863

被折叠的 条评论
为什么被折叠?



