${function string} or $(function string)
以下開始介紹YC常用的
Makefile function
$(notdir string...)
找出最後一個 / 輸出後續的的字串
$(notdir /mnt/test1.c test2.c)
output result : test1.c test2.c
$(wildcard string...)
在folder下找出所有指定好副檔名
假設/mnt/下有test1.c test2.c test3.o test4.o
$(wildcard /mnt/*.c)
output reslut : test1.c test2.c (*.o不會被輸出)
$(subst from, to, string)
從string中找出from取代成to
$(subst hate, love, I hate linux)
output result : I love linux
$(filter-out pattern, string)
輸出pattern以外的字串
$(filter-out test1.c, test.1.c test2.c)
output result : test2.c
$(foreach var, StringList, $var)
StringList以空白隔開,逐一取得值,將值傳給var,並輸出$var計算後的結果
$(foreach var,a b c d, $(var).x)
output result : a.x b.x c.x d.x
假設/src1目錄/下有test1.c test1.o, /src2目錄下有test2.c test2.o
$(foreach var, /src1 /src2, $(wildcard $(var)/*.c))
output result : test1.c test2.c
變數使用語法
以下是YC常常健忘的語法
:= 語法指定變數的語法,make 會先把整個檔案展開,找出該變數最後一個被賦予的值
x := foo
y := $(x)
x := foobar
y的結果為 foobar
?= 語法
變數已經被指定過,不會再被賦予新值
Makefile隱藏變數
VPATH
告知Makefile原始碼目錄在哪
Makefile sample
# target source
SRCS = main.c \
test1.c #under folder src
TARGET := main
.PHONY : clean
#default Makefile compiler variable VPATH= $(PWD)/src $(PWD) ################################# OBJ_PATH=$(PWD)/obj OBJS :=$(SRCS:%.c=$(OBJ_PATH)/%.o) $(OBJ_PATH)/%.o : %.c @echo compiler $(notdir $<) @[ -e $(OBJ_PATH) ] || mkdir -p $(OBJ_PATH) @$(CC) $(CFLAGS) -o $@ -c $< $(TARGET):$(OBJS) $(CC) -o $(TARGET) $(OBJS) clean: @rm -rf $(TARGET) $(OBJS) @rm -rf $(OBJ_PATH)