doctorx第3季百度云:模块驱动 Makefile 模板

来源:百度文库 编辑:偶看新闻 时间:2024/05/03 18:22:32

模块驱动 Makefile 模板

(2010-06-22 12:02:56)转载

TARGET=test_demo
#CROSS_COMPILE=arm-linux-     不用交叉环境       

CC=$(CROSS_COMPILE)gcc
STRIP=$(CROSS_COMPILE)strip
#CFLAGS=-O2

ifeq($(KERNELRELEASE),)          #若KERNELRELEASE没有定义

   
     KERNELDIR?=/lib/modules/$(shell uname-r)/build   #PC驱动路径.
    #上面的具体地址也就是 KERNELDIR?=/usr/src/kernels/2.6.9-42.EL-smp-i686 

    # KERNELDIR?=/up-Star2410/kernel/linux-2.6.24.4/   #ARM驱动.

    PWD := $(shellpwd)   #取当前路径

all:  $(TARGET)modules

$(TARGET):
       $(CC) -o $(TARGET)$(TARGET).c     #用gcc编译应用程序

modules:
       $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

#-C $(KERNELDIR)指明跳转到内核源码目录下读取那里的Makefile
#M=$(PWD) 表明然后返回到当前目录继续读入、执行当前的Makefile

modules_install:
       $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
       rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions$(TARGET)

.PHONY:modules modules_installclean

 

#当从内核源码目录返回时,第二次进入Makefile时KERNELRELEASE已被被定义,所以可以执行以下的else语句
else
   obj-m :demo.o 
#obj-m后面为我们要编译的目标名,主makefile这样就会为我们编译该模块了

endif  

 

另一种是倒过来的:

ifneq($(KERNELRELEASE),)   #ifneq就倒过来了,上面是ifeq,所以一开始执行else,
obj-m:=hello.o
else
KERNELDIR:=/lib/modules/$(shell uname -r)/build
PWD:=$(shell pwd)
default:
       $(MAKE) -C $(KERNELDIR)  M=$(PWD) modules
clean:
       rm -rf *.o *.mod.c *.mod.o *.ko
endif

 

看了LDD-3也是上面一种,复制过来看一下

# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
    obj-m := hello.o 
 
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
 
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD  := $(shell pwd)
 
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
 
endif

 

Once again, we are seeing the extended GNU makesyntax in action. This makefile is read twice on a typical build.When the makefile is invoked from the command line, it notices thatthe KERNELRELEASE variable has not been set. It locatesthe kernel source directory by taking advantage of the fact thatthe symbolic link build in the installed modules directorypoints back at the kernel build tree. If you are not actuallyrunning the kernel that you are building for, you can supply aKERNELDIR= option on the command line, set theKERNELDIR environment variable, or rewrite the line thatsets KERNELDIR in the makefile. Once the kernel sourcetree has been found, the makefile invokes the default:target, which runs a second make command(parameterized in the makefile as $(MAKE)) to invoke thekernel build system as described previously. On the second reading,the makefile sets obj-m, and the kernel makefiles takecare of actually building the module.

This mechanism for building modules may strike you as a bitunwieldy and obscure. Once you get used to it, however, you willlikely appreciate the capabilities that have been programmed intothe kernel build system. Do note that the above is not a completemakefile; a real makefile includes the usual sort of targets forcleaning up unneeded files, installing modules, etc. See the makefiles in theexample source directory for a complete example.

 

 

总结: 

Makefiel要做两件事:

1,如果内核没有被编译,先编译内核。

2,然后编译内核完后,编译模块。

就这么简单。。。。

分享 分享到新浪Qing

0

阅读(157) 评论 (0) 收藏(0) 转载(0) 打印举报 已投稿到: 排行榜 圈子

转载列表:

转载 前一篇:对一个简单的模块驱动的理解 demo.c 后一篇:一根网线搞定PC间数据大量传输