2015年11月23日 星期一

[Linux 文章收集] Makefile - wildcard notdir patsubst 的簡單介紹

Source From Here 
Introduction 
1、$(wildcard pattern) :擴展通配符 
The argument pattern is a file name pattern, typically containing wildcard characters (as in shell file name patterns). The result of wildcard is a space-separated list of the names of existing files that match the pattern. See Using Wildcard Characters in File Names.

2、$(notdir names…) :去除路徑 
Extracts all but the directory-part of each file name in names. If the file name contains no slash, it is left unchanged. Otherwise, everything through the last slash is removed from it. A file name that ends with a slash becomes an empty string. This is unfortunate, because it means that the result does not always have the same number of whitespace-separated file names as the argument had; but we do not see any other valid alternative.

3、$(patsubst pattern,replacement,text) :替換通配符 
Finds whitespace-separated words in text that match pattern and replaces them with replacement. Here pattern may contain a ‘%’ which acts as a wildcard, matching any number of any characters within a word. If replacement also contains a ‘%’, the ‘%’ is replaced by the text that matched the ‘%’ in pattern. Only the first ‘%’ in the pattern and replacement is treated this way; any subsequent ‘%’ is unchanged.


Learn by example 
建立一個測試目錄,在測試目錄下建立一個名為 sub 的子目錄: 
$ mkdir test 
$ cd test 
$ mkdir sub

在 test 下,建立 a.c 和 b.c2 個文件,在sub目錄下,建立 sa.c 和 sb.c2 個文件. 


接著建立一個簡單的測試 Makefile: 
  1. SRC = $(wildcard *.c ./sub/*.c)  
  2. DIR = $(notdir $(SRC))  
  3. OBJ = $(patsubst %.c, %.o, $(DIR))  
  4.   
  5.   
  6. all:  
  7.     @echo $(SRC)  
  8.     @echo $(DIR)  
  9.     @echo $(OBJ)  
  10.     @echo "end"  
接著執行: 
# make
a.c ./sub/sa.c
a.c sa.c
a.o sa.o
end

第一行的 "a.c ./sub/sa.c" 為 wildcard 展開的結果; 第二行的 "a.c sa.c" 為 notdir 將第一行結果的目錄資訊拿掉, 只留下檔名; 第三行的結果 "a.o sa.o" 是透過 patsubst 將 ".c" 結尾的項目換成 ".o". 

Supplement 
GNU Make Doc - Functions for File Names 
GNU Make Doc - Functions for String Substitution and Analysis

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...