SRC_FILES = $(wildcard src/*.c)返回src目錄下所有.c文件列表。
SRC_OBJ = $(patsubst %.c, %.o, $(SRC_FILES))將SRC_FILES中所有.c文件替換為.o返回給變量SRC_OBJ。
$(objects:.c=.o) $(patsubst %.c,%.o,$( src_files))4. 過濾函數—filter。
# make make[1]: Entering directory `/home/Myprojects/example_make/version-3.1/src/ipc' make[1]: Leaving directory `/home/Myprojects/example_make/version-3.1/src/ipc' make[1]: Entering directory `/home/Myprojects/example_make/version-3.1/src/tools' make[1]: Leaving directory `/home/Myprojects/example_make/version-3.1/src/tools' make[1]: Entering directory `/home/Myprojects/example_make/version-3.1/src/main' make[1]: Leaving directory `/home/Myprojects/example_make/version-3.1/src/main' #發現只有進入目錄和退出目錄的顯示,這樣很難知道目前編譯過程。其實我們可以在規則命令處加入一行類似打印:
$(OBJDIR) : >---@echo " MKDIR $(notdir $@)..." >---@mkdir -p $@ ifneq ($(SRC_BIN),) $(BINDIR)/$(SRC_BIN) : $(SRC_OBJ) >---@echo " LINK $(notdir $@)..." >---@$(CC) -o $@ $^ $(LDFLAGS) endif ifneq ($(SRC_LIB),) $(LIBDIR)/$(SRC_LIB) : $(SRC_OBJ) >---@echo " ARCHIVE $(notdir $@)..." >---@$(AR) rcs $@ $^ >---@echo " COPY $@ to $(SRC_BASE)/libs" >---@cp $@ $(SRC_BASE)/libs endif $(SRC_OBJ) : $(OBJDIR)/%.o : %.c >---@echo " COMPILE $(notdir $<)..." >---@$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
# make make[1]: Entering directory `/home/Myprojects/example_make/version-3.1/src/ipc' COMPILE ipc.c... ARCHIVE libipc.a... COPY ../../build/unix_dbg/lib/libipc.a to ../../libs make[1]: Leaving directory `/home/Myprojects/example_make/version-3.1/src/ipc' make[1]: Entering directory `/home/Myprojects/example_make/version-3.1/src/tools' COMPILE base64.c... COMPILE md5.c... COMPILE tools.c... ARCHIVE libtools.a... COPY ../../build/unix_dbg/lib/libtools.a to ../../libs make[1]: Leaving directory `/home/Myprojects/example_make/version-3.1/src/tools' make[1]: Entering directory `/home/Myprojects/example_make/version-3.1/src/main' COMPILE main.c... LINK target_bin... make[1]: Leaving directory `/home/Myprojects/example_make/version-3.1/src/main' #
$(BUILDDIR): >---@echo " Create directory $@ ..." >---mkdir -p $(BUILDDIR)/bin $(BUILDDIR)/lib $(MODULES): >---@$(MAKE) -C $(DIR)/$@ MODULE=$@ --no-print-directory main:tools ipc clean : >---@for subdir in $(MODULES); \ >---do $(MAKE) -C $(DIR)/$$subdir MODULE=$$subdir $@ --no-print-directory; \ >---done 編譯輸出: # make COMPILE ipc.c... ARCHIVE libipc.a... COPY ../../build/unix_dbg/lib/libipc.a to ../../libs COMPILE base64.c... COMPILE md5.c... COMPILE tools.c... ARCHIVE libtools.a... COPY ../../build/unix_dbg/lib/libtools.a to ../../libs COMPILE main.c... LINK target_bin... # make clean rm -f ../../build/unix_dbg/obj/ipc/ipc.o ../../build/unix_dbg/lib/libipc.a rm -f ../../build/unix_dbg/obj/main/main.o ../../build/unix_dbg/bin/target_bin rm -f ../../build/unix_dbg/obj/tools/base64.o ../../build/unix_dbg/obj/tools/md5.o ../../build/unix_dbg/obj/tools/tools.o ../../build/unix_dbg/lib/libtools.a #
這樣看上去輸出清爽多了。其實我們也可以使用make -s 來全面禁止命令的顯示。