目录
一、shell
二、subst
三、patsubst
四、foreach
五、dir
六、notdir
七、filter
八、filter-out
九、basename
十、wildcard
一、shell
$(shell <command> <arguments>)# 名称:shell 命令函数
# 功能:调用 shell 命令 command
# 返回:函数返回 shell 命令 command 的执行结(base) [root@localhost 06_test]# ls
makefile objs src
(base) [root@localhost 06_test]# vim makefile
(base) [root@localhost 06_test]# cat makefile
cpp_src = $(shell find src -name "*.cpp")debug:@echo $(cpp_src)
(base) [root@localhost 06_test]# find src -name *.cpp
src/main.cpp
(base) [root@localhost 06_test]# make debug
src/main.cpp
(base) [root@localhost 06_test]#
二、subst
$(subst <from>,<to>,<text>)# 名称:字符串替换函数
# 功能:把字串 <text> 中的 <from> 字符串替换成 <to>
# 返回:函数返回被替换过后的字符串
# 不允许递归调用(base) [root@localhost 06_test]# vim makefile
(base) [root@localhost 06_test]# cat makefile
cpp_src = $(shell find src -name "*.cpp")
tmp = $(subst src/,objs/,$(cpp_src))
cpp_obj = $(subst cpp,o,$(tmp))debug:@echo $(cpp_src)@echo $(cpp_obj)
(base) [root@localhost 06_test]# make debug
src/main.cpp
objs/main.o
(base) [root@localhost 06_test]#
三、patsubst
$(patsubst <pattern>,<replacement>,<text>)# 名称:模式字符串替换函数
# 功能:通配符 %,表示任意长度的字串,从 text 中取出 patttern, 替换成 replacement
# 返回:函数返回被替换过后的字符串(base) [root@localhost 06_test]# vim makefile
(base) [root@localhost 06_test]# cat makefile
cpp_src = $(shell find src -name "*.cpp")
cpp_obj = $(patsubst src%.cpp,objs%.o,$(cpp_src))debug:@echo $(cpp_src)@echo $(cpp_obj)
(base) [root@localhost 06_test]# make debug
src/main.cpp
objs/main.o
(base) [root@localhost 06_test]#
四、foreach
$(foreach <var>,<list>,<text>)# 名称:循环函数
# 功能:把字串<list>中的元素逐一取出来,执行<text>包含的表达式
# 返回:<text>所返回的每个字符串所组成的整个字符串(以空格分隔)(base) [root@localhost 06_test]# vim makefile
(base) [root@localhost 06_test]# cat makefile
library_paths := /datav/shared/100_du/03.08/lean/protobuf-3.11.4/lib \/usr/local/cuda-10.1/lib64library_paths := $(foreach item,$(library_paths),-L$(item))debug:@echo $(library_paths)
(base) [root@localhost 06_test]# make debug
-L/datav/shared/100_du/03.08/lean/protobuf-3.11.4/lib -L/usr/local/cuda-10.1/lib64
(base) [root@localhost 06_test]#
五、dir
$(dir <names...>)# 名称:取目录函数
# 功能:从文件名序列中取出目录部分。目录部分是指最后一个反斜杠(“/”)之前 的部分。如果没有反斜#
# 杠,那么返回“./”
# 返回:返回文件名序列的目录部分(base) [root@localhost 06_test]# vim makefile
(base) [root@localhost 06_test]# cat makefile
cpp_file := $(shell find src -name *.cpp)
cpp_src := $(dir $(cpp_file))debug:@echo $(cpp_file)@echo $(cpp_src)
(base) [root@localhost 06_test]# make debug
src/main.cpp
src/
(base) [root@localhost 06_test]#
六、notdir
$(notdir <names...>)# 名称:除目录函数
# 功能:将文件路径的目录部分除去,只保留文件名
# 返回:返回除去目录部分的文件名(base) [root@localhost 06_test]# tree .
.
├── makefile
├── objs
│ └── main.o
└── src└── main.cpp2 directories, 3 files
(base) [root@localhost 06_test]# vim makefile
(base) [root@localhost 06_test]# cat makefile
libs := $(shell find ./ -name *.*)debug:@echo $(libs)
(base) [root@localhost 06_test]# make debug
./ ./src/main.cpp ./objs/main.o
(base) [root@localhost 06_test]# vim makefile
(base) [root@localhost 06_test]# cat makefile
libs := $(notdir $(shell find ./ -name *.*))debug:@echo $(libs)
(base) [root@localhost 06_test]# make debug
main.cpp main.o
(base) [root@localhost 06_test]#
七、filter
$(filter <names...>)# 名称:过滤器函数
# 功能:从数据列表中过滤出指定的字符串元素
# 返回:返回过滤的结果(base) [root@localhost 06_test]# vim makefile
(base) [root@localhost 06_test]# cat makefile
libs := $(notdir $(shell find /usr/lib -name lib*))
a_libs := $(filter %.a, $(libs))
so_libs := $(filter %.so, $(libs))debug:@echo $(a_libs)@echo $(so_libs)
(base) [root@localhost 06_test]# make debug
libasan.a libatomic.a libgcc.a libgcc_eh.a libgcov.a libgomp.a libitm.a libmudflap.a libmudflapth.a libquadmath.a libstdc++.a libsupc++.a libgcc.a libgcc_eh.a libgcov.a libgomp.a
libasan.so libatomic.so libgcc_s.so libgomp.so libitm.so libmudflap.so libmudflapth.so libquadmath.so libstdc++.so libasan.so libatomic.so libgcc_s.so libgomp.so libtsan.so libstdc++.so
(base) [root@localhost 06_test]#
八、filter-out
$(filter-out <del_str> > <str_list>)# 名称:过滤删除函数
# 功能:在字符串列表中过滤出需要的元素并将之删除
# 返回:返回过滤删除后的字符串列表(base) [root@localhost 06_test]# tree .
.
├── makefile
├── objs
│ └── main.o
└── src└── main.cpp2 directories, 3 files
(base) [root@localhost 06_test]# vim makefile
(base) [root@localhost 06_test]# cat makefile
cpp_files := src/main.cpp objs/main.o
source_files := $(filter-out %.o, $(cpp_files))hello: $(source_files)g++ -o $@ $^ debug:@echo $(source_files)
(base) [root@localhost 06_test]# make debug
src/main.cpp
(base) [root@localhost 06_test]# make
g++ -o hello src/main.cpp
(base) [root@localhost 06_test]# ls
hello makefile objs src
(base) [root@localhost 06_test]# ./hello
hello world
(base) [root@localhost 06_test]#
九、basename
$(basename <names...>)# 名称:去后缀函数
# 功能:去掉指定文件列表的后缀
# 返回:返回去掉后缀后的文件列表名称(base) [root@localhost 06_test]# vim makefile
(base) [root@localhost 06_test]# cat makefile
libs := $(notdir $(shell find /usr/lib -name lib*))
a_libs := $(subst lib,,$(basename $(filter %.a,$(libs))))
so_libs := $(subst lib,,$(basename $(filter %.so,$(libs))))test_files := $(basename a.out.exe test1.a.cpp)debug:@echo $(a_libs)@echo $(so_libs)@echo $(test_files)
(base) [root@localhost 06_test]# make debug
asan atomic gcc gcc_eh gcov gomp itm mudflap mudflapth quadmath stdc++ supc++ gcc gcc_eh gcov gomp
asan atomic gcc_s gomp itm mudflap mudflapth quadmath stdc++ asan atomic gcc_s gomp tsan stdc++
a.out test1.a
(base) [root@localhost 06_test]#
十、wildcard
$(wildcard <name...>)# 名称:通配符函数
# 功能:通配符函数将符合匹配模式的文件名的用空格分隔扩展为列表
# 返回:返回扩展后的列表(base) [root@localhost 06_test]# tree .
.
├── hello
├── makefile
├── objs
│ └── main.o
└── src├── add.c├── hello.java├── main.c├── main.cc└── main.cpp2 directories, 8 files
(base) [root@localhost 06_test]# vim makefile
(base) [root@localhost 06_test]# cat makefile
cpp_srcs := $(wildcard src/*.cc src/*.cpp src/*.c)debug:@echo $(cpp_srcs)
(base) [root@localhost 06_test]# make debug
src/main.cc src/main.cpp src/main.c src/add.c
(base) [root@localhost 06_test]#