标签:
本文摘自http://blog.csdn.net/lufeiop02/article/details/6446343
Linux驱动一般以模块module的形式来加载,首先需要把驱动编译成模块的形式。简单的例子,
#include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> static int __init test_init(void) { printk("init module/n"); return 0; } static void __exit test_exit(void) { printk("exit modules/n"); } module_init(test_init); module_exit(test_exit);
Makefile为:
PWD = $(shell pwd) KERNEL_SRC = /usr/src/linux-source-2.6.15/ obj-m := test.o module-objs := test.o all: $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules clean: rm *.ko rm *.o
在test.c和Makefile所在的目录下运行make,如果看到类似输出
make -C /usr/src/linux-source-2.6.15/ M=/home/vmeth modules make[1]: Entering directory `/usr/src/linux-source-2.6.15‘ CC [M] /home/vmeth/hello.o Building modules, stage 2. MODPOST CC /home/vmeth/hello.mod.o LD [M] /home/vmeth/hello.ko make[1]: Leaving directory `/usr/src/linux-source-2.6.15‘
一般用下面的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 #KDIR目录其实是链接到上面那个Makefile中的那个 /usr/src/linux-source-2.6.15/*中 all: $(MAKE) -C $(KDIR) M=$(PWD) clean: rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions endif
标签:
原文地址:http://www.cnblogs.com/hjj801006/p/4552422.html