makefile实例

#.PHONY:cleanall cleanobj cleandiff


#cleanall:cleandiff cleanobj
#	rm program

#cleanobj:
#	rm obj.c
#	echo 'cleaning........'
#cleandiff:
#	rm diff.c

#命令cd和pwd同时执行
exec1:
	cd ../
	pwd

#先执行命令cd再执行pwd
exec2:
	cd ../;pwd
foo := a.o b.o c.o
bar := $(foo:.o=.c)

#替换函数
exec3:
	echo '$(bar)'
a := afrom
b := $(subst from,to,$a)
exec4:
	@echo '$b'

#取所在目录路径

dirs := $(dir src/foo.c hacks)
exec5:
	@echo '$(dirs)' 


#取文件名后缀
suf1 := $(suffix a.c a.o)
exec6:
	@echo '$(suf1)'

#取前缀
bas = $(basename foo.x a.o)
exec7:
	@echo '$(bas)'

#添加后缀
addsux = $(addsuffix .c,foo bar)
exec8:
	@echo '$(addsux)'

#添加前缀
addpre = $(addprefix aa,foo bar)
exec9:
	@echo '$(addpre)'

#字串连接函数
pjoin = $(join aa, bb cc)
exec10:
	@echo '$(pjoin)'

#查找字串函数
findstr = $(findstring ab,dddabf)
exec11:
	@echo '$(findstr)'

#过滤函数
sources:=foo.c bar.c baz.s ugh.h
ss=$(filter %.c %.s,$(sources))
exec12:
	@echo '$(ss)'

#循环函数
names := a b c d
#表示将names的值依次放进n中,然后以.o的形式输出
files := $(foreach n,$(names),$(n).o)
exec13:
	@echo '$(files)'

reverse = $(2)$(1)
foo = $(call reverse,a,b)
exec14:
	@echo '$(foo)'
reverse = $(2)$(1)
foo = $(call reverse,a,b)
exec15:
	@echo '$(foo)'

#查看变量的类型
aa = $(origin CC)
exec16:
	@echo '$(aa)'
  

  

posted @ 2018-01-10 10:34  轻轻的吻  阅读(292)  评论(1编辑  收藏  举报