Makefile实例与shell脚本符号区别

本文深入探讨Makefile的编写技巧,包括变量定义、函数使用、规则制定等核心内容。通过具体示例展示如何利用Makefile自动化构建过程。
cat Makefile 
# Automatically generated by /home/exapp/zengzhihao/amlogic/buildroot-openlinux-20170814/buildroot/support/scripts/mkmakefile: don't edit
#lastword既可作为变量也可作为函数,作为变量调用时需要传入1="xxx yyy"
lastword = $(word $(words $(1)),$(1))
#$(call fun,$(pa1))  调用fun函数传入pa1参数及调用lastword函数,返回$(words Makefile out/Makefile)      $(word 2, Makefile out/Makefile)
#返回out/Makefile
#makedir := out
makedir := $(dir $(call lastword,$(MAKEFILE_LIST)))

MAKEARGS := -C /home/exapp/zengzhihao/amlogic/buildroot-openlinux-20170814/buildroot


#$(if $(patsubst /%,,$(makedir)),$(CURDIR)/)没有符合/%返回原值,则不为空返回$(CURDIR)/
MAKEARGS += O=$(if $(patsubst /%,,$(makedir)),$(CURDIR)/)$(patsubst %/,%,$(makedir))
#--no-print-directory是:不要再屏幕上打印"Entering directory.."
MAKEFLAGS += --no-print-directory

.PHONY: _all $(MAKECMDGOALS)
#MAKECMDGOALS命令行参数出执行命令make外带入的其它参数如make  are you则MAKECMDGOALS=(are you)
all	:= $(filter-out Makefile,$(MAKECMDGOALS))

#Makefile output/mesongxl_p241_kernel49//Makefile
_all:
	echo $(call lastword,$(MAKEFILE_LIST))
	echo $(CURDIR)
	echo $(makedir)
	echo $(patsubst /%,,$(makedir))
	echo $(if $(patsubst /%,,$(makedir)),$(CURDIR)/)
	umask 0022 && $(MAKE) $(MAKEARGS) $(all)

Makefile:;
#@关闭会显
#@ echo 33不会显示echo 33
#@: echo ad>good.ccc区别@ echo ad>good.ccc
#@:关闭回显以及输出,@仅仅关闭回显
$(all): _all
	@:

%/: _all
	@:




如果编写一个规则,并不产生目标文件,则其命令在每次make 该目标时都执行。例如:
  clean:
  rm *.o temp
因为"rm"命令并不产生"clean"文件,则每次执行"makeclean"的时候,该命令都会执行。如果目录中出现了"clean"文件,则规则失效了:没有依赖文件,文件"clean"始终是最新的,命令永远不会执行;为避免这个问题,可使用".PHONY"指明该目标。如:
  .PHONY : clean

  这样执行"make clean"会无视"clean"文件存在与否。


makefile中符号含义

https://www.cnblogs.com/ydxt/p/5642331.html


makefile符号例子

$@:目标文件,引号左边

$%:目标文件中括号内

$<:第一个依赖文件

$^:所有依赖文件,以空格隔开

$(@D):目标文件中除去文件的目录,相当于$(dir $@)    D=directory

$(@F):目标文件中除去目录的文件,相当于$(notdir $@)  F=file

cat Makefile 
$(shell touch c.c d.h e.o)
file=$(shell ls)
a_val=/home/test/a_val

dir=$(dir $(a_val))
file=$(notdir $(a_val))

$(a_val)(b_val.o):c.c d.h e.o
    #echo $(file)
    @echo "a_val(b_val.o):c.c d.h e.o"
    #echo "$$@="$@ 
    #echo "$$%="$%
    #echo "$$<="$<
    #echo "$$^="$^
    #echo "$$(@D)="$(@D)
    #echo "$$(@F)="$(@F)
    #echo "dir="$(dir)
    #echo "file="$(file)
zengzhihao@yingyongbu:~/work_place/test/fuu$ make
#echo a_val
a_val(b_val.o):c.c d.h e.o
#echo "$@="/home/test/a_val 
#echo "$%="b_val.o
#echo "$<="c.c
#echo "$^="c.c d.h e.o
#echo "$(@D)="/home/test
#echo "$(@F)="a_val
#echo "dir="/home/test/
#echo "file="a_val




cat test.sh 
#!/bin/sh
echo '$#='$#"----all args number except itself"
echo '$0='$0"----first parameter"
echo '$1='$1"----senond parameter"
echo '$*='$*"----all parameter"
echo '$$='$$"----pid"

./good
echo '$?='$?"----last shell execute result:0->sucess,others->fail"
pwd
echo '$?='$?"----last shell execute result:0->sucess,others->fail"

zengzhihao@yingyongbu:/home/exapp/zengzhihao/amlogic/buildroot-openlinux-20170814/output/mesongxl_p241_kernel49/target$ ./test.sh 1_val 2_val 3_val
$#=3----all args number except itself
$0=./test.sh----first parameter
$1=1_val----senond parameter
$*=1_val 2_val 3_val----all parameter
$$=4876----pid
./test.sh: line 8: ./good: No such file or directory
$?=127----last shell execute result:0->sucess,others->fail
/home/exapp/zengzhihao/amlogic/buildroot-openlinux-20170814/output/mesongxl_p241_kernel49/target
$?=0----last shell execute result:0->sucess,others->fail

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值