Linux 2.6 下内核模块的Makefile 收藏
<script type="text/javascript"> </script>
Linux 2.6 下内核模块的Makefile
# Makefile 2.6
obj-m += hello.o
KDIR:=/lib/modules/$(shell uname -r)/build
# PWD=$(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
obj-m := hello.o表示编译后生成hello.o模块。
$(KDIR) 指定了内核源码的路径,“M=”表示这是个外部模块,M=$(PWD) 指定了该模块文件所在的路径。
注: makefile 预定义了$(PWD) 变量,此处可以不必重复定义。
如果是多个源文件编译出一个模块,假设模块名是test.ko,那么源文件名不能有test.c
obj-m := test.o
test-objs := file1.o file2.o file3.o
KDIR := /lib/modules/$(shell uname -r)/build
#PWD := $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
一个简单的内核模块示例:hello.c
#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
static int __iinit hello_init(void)
{
printk("hello, world/n");
return 0;
}
static void __exit hello_exit(void)
{
printk("goodbe/n");
}
module_init(hello_init);
module_exit(hello_exit);