Makfile總結( 二 )

Strip$(strip <string>)主要功能去掉字符串 string 首尾的空格 。
findstring$(findstring <find>,<text>)這個函數的作用是從字符串當中尋找字符串,如果找到了字符串就返回字符串 , 否則返回空 。
filter$(filter <pattern...>,<text>)這是一個過濾函數,這個函數執行時,首先會根據空格或者tab鍵或者回車換行符進行分割 , 然后一一的進行filter函數的操作 。然后遍歷每一個被分割出來的字符,如果不滿足pattern的規則的話對應的字符就會被過濾掉 。
s = a.c abo.c s.o s.y x.o x.yss = $(filter %.c %.o, $(s))main: demo.c echo $(ss)filter-out這個函數和filter的用法是一樣的只不過,作用剛好相反 , filter是保存符合條件的字符串,filter-out是保存不符合條件的字符串 。
s = a.c abo.c s.o s.y x.o x.yss = $(filter-out %.c %.o, $(s))main: demo.c echo $(ss)sort這個函數主要是用于幫助字符串排序的,同時還會取出分割之后相同的字符串 。
s = g a b c d e fa a a ass = $(sort $(s))main: demo.c echo $(ss)word$(word <n>,<text>)這個功能很簡單,返回當中第個字符 。
s = g a b c d e fa a a ass = $(word 1, $(s)) # 取出第一個字符main: demo.c echo $(ss)wordlist$(wordlist <start>,<end>,<text>)這個也是從字符串當中取出字符,是取出第個字符一直到第個字符 。
s = g a b c d e fa a a ass = $(wordlist 1, 5, $(s))main: demo.c echo $(ss)words統計單詞的個數 。
s = 1 2 3 4 5ss = $(words $(s))main: demo.c echo $(ss)firstword這個函數主要是用于返回第一個字符串的 。
s = 1 2 3 4 5ss = $(firstword $(s))main: demo.c echo $(ss)文件函數dir與notdir函數dir函數主要書獲取文件路徑當中的目錄部分,而notdir函數主要是獲取文件路徑當中文件名的部分
file = ./files/a.cfdir = $(dir $(file))nfdir = $(notdir $(file))main: demo.c echo $(fdir) echo $(nfdir)suffix函數這個函數主要是用于獲取文件的后綴名 。
file = ./files/a.cfdir = $(dir $(file))nfdir = $(notdir $(file))name = $(suffix $(file))main: demo.c echo $(fdir) echo $(nfdir) echo $(name)basename這個函數用于獲取文件路徑當中除去后綴名的部分 。
file = ./files/a.cbase = $(basename $(file))main: demo.c echo $(base)addsuffix這個函數主要是給文件加上后綴的 。
file = ./files/a.cbase = $(addsuffix .c, $(file))main: demo.c echo $(base)addprefix這個函數的主要作用就是在字符串的前面加上一串字符 。
file = files/a.cbase = $(addprefix ./src/main/, $(file))main: demo.c echo $(base)其他函數循環函數foreachforeach函數的主要使用規則為:
$(foreach <var>,<list>,<text>)我們直接使用一個例子來說明這個情況:
files = a.c b.c c.c d.cnew_files = $(foreach n, $(files), $(n)pp)main: demo.c echo $(new_files)call函數call函數在makefile當中可以用于調用我們自定義的一個表達式 , 他的語法個數如下面所示:
$(call <expression>,<parm1>,<parm2>,...,<parmn>)

  • 表示定義的表達式的名字 。
  • 表示第n個參數,我們在當中可以使用$(n)進行引用 。
a=a.cb=b.cc=$(a)-------$(b)main: demo.c echo $(c)在makefile當中使用shell函數我們在makefile的表達式當中可以使用shell的函數 。
比如現在我們有一個文件叫做test.txt,文件的內容如下所示:
a.c b.c c.c d.ccontent=$(shell cat test.txt) # 將shell命令的輸出內容賦給contentmain: demo.c echo $(content) # 輸出contentorigin函數origin這個函數主要是返回變量的定義方式,使用格式如下:
$(origin <variable>) # 其中 variable 是變量名字 這里不需要使用 $ 符號去引用error函數在makefile當中我們可以使用error函數讓makefie停止執行 。當我們有需求:讓在某種條件下讓makefile停止編譯
data=https://www.huyubaike.com/biancheng/dataifeq ($(data), data)$(error"data =https://www.huyubaike.com/biancheng/= data")endifmain: main.c gcc main.c除此之外還有個warning函數使用方法和error一樣!
總結在本篇文章當中主要給大家總結了一些常見的makefile的使用方法,方便大家查閱復習!
以上就是本篇文章的所有內容了,我是LeHung,我們下期再見?。?!更多精彩闹R鶯霞煞夢氏钅浚篽ttps://github.com/Chang-LeHung/CSCore

推薦閱讀