2011-02-01 64 views
8

考慮以下生成文件:如何有條件地將文件包含到Makefile中?

# <include global configuration Makefile> 

INCL = -I../include \ 
     -I<whatever> 

CPPFLAGS=$(DEFS) $(INCL) 
CXXFLAGS = -O0 -g -Wall -fmessage-length=0 

SRCS = $(wildcard *.cpp) 

OBJS = $(SRCS:.cpp=.o) 

all: $(OBJS) 

%.o: %.cpp 
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o [email protected] $< 


depend: .depend 

.depend: $(SRCS) 
    $(CPP) $(CPPFLAGS) -M $^ > [email protected] 

clean: 
    rm -f $(OBJS) 
    rm .depend 


-include .depend 

此生成的文件創建使用g++ -M命令的#include依賴關係鏈,幷包括它。這可能是一個相當長的過程。問題是即使調用了make clean,該文件仍會被生成,但仍然會刪除該文件。是否有條件地包含這個文件,並且如果幹淨的目標運行時不打擾創建它?

+0

這回答詳細[​​這裏] [1]。 [1]:http://stackoverflow.com/questions/3714041/why-does-this-makefile-execute-a-target-on-make-clean/3714054#3714054 – 2011-02-02 09:17:05

回答

12

事情是這樣的:

ifneq ($(MAKECMDGOALS),clean) 
-include .depend 
endif 

更多信息,請參見make manual page on Goals

編輯:-include不能縮進標籤,否則將被忽略。

1

在編譯過程中,您可以對免費(即無運行成本)執行此類依賴關係。當您運行clean時,依賴關係自然不會重新創建。參見Paul Smith的Advanced Auto-Dependency Generation論文中的合併編譯和依賴生成部分。

相關問題