Makefile debug的经验

1. 使用warning指令

warning 是个不错的命令,可以打印出消息,来判断makefile执行的流程

 

 

如 , 这是一个普通的编译kernel module的Makefile,但是我忘记这个文件的执行顺序了。

 

# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.

#CROSS_COMPILE = /opt/toolchains/uclibc-crosstools-gcc-4.2.3-3/usr/bin/mips-linux-uclibc-
#export CROSS_COMPILE

ifneq ($(KERNELRELEASE),)
$(warning A top-level warning)
    obj-m := hello_world.o
#   hello_world-objs :=
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
$(warning A Bottom-level waring)
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
#KERNELDIR ?= /home/wyang2/WFG500N/bcm963xx/kernel/linux
    PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif

 

 

 

执行的结果是,这样我就发现,实际上时先执行下面那一段,然后再执行上面一段的 。

 

$make
Makefile:14: A Bottom-level waring
make -C /lib/modules/2.6.23.1-42.fc8/build M=/home/wyang2/test/kmodule modules
make[1]: Entering directory `/usr/src/kernels/2.6.23.1-42.fc8-i686'
/home/wyang2/test/kmodule/Makefile:8: A top-level warning
  Building modules, stage 2.
  MODPOST 1 modules
  LD [M]  /home/wyang2/test/kmodule/hello_world.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.23.1-42.fc8-i686'

 

 

2.使用ifeq ifneq

当makefile被多次调用到的时候,如果都输出warning,那么就显得输出内容太多,找不到到底是哪个了。

使用ifeq/ifneq可以选择性输出,对大型的工程比较好用。

ifeq ($(obj),arch/x86/boot)
    $(warning building the bzImage?)
endif

 

3. 输出隐藏的变量

有时候我需要看看,这次make执行中到底定义了哪些变量,一个个打太麻烦,而且打出来的都是自己知道的。

原帖在

http://www.cmcrossroads.com/ask-mr-make/6521-dumping-every-makefile-variable

.PHONY: printvars
printvars:
    @$(foreach V,/
        $(sort $(.VARIABLES)), /
            $(if $(filter-out environment% default automatic, $(origin $V)),/
                $(warning $V=$($V) ($(value $V)))/
             ) /
      )/

printvars1:
#    echo $(.VARIABLES)
    @$(foreach V,/
        $(sort $(.VARIABLES)), $(warning $V=$($V) ($(value $V)) $(origin $V)) /
      )

 

上面两个例子分别打印了,在文件中定义的,和所有的makefile变量。

 


4。 使用-p选项

使用-p选项可以输出规则和变量。


5。 命令行的目标

makefile中可以用变量 MAKECMDGOALS 打印这次make时命令行目标。



  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值