2012-08-14 35 views
14

作爲makefile的一部分,我想生成目標的調試版本或發行版本。發出文件警告,覆蓋目標的命令

功能,使運行時,一切工作,但是,我得到的警告

12 SRC := $(shell echo src/*.cpp) 
13 SRC += $(shell echo $(TEST_ROOT)/*.cpp) 
14 
15 D_OBJECTS = $(SRC:.cpp=.o)  # same objects will need to be built differently 
16 R_OBJECTS = $(SRC:.cpp=.o)  # same objects will need to be built differently 

22 all: $(TARGET) 
23 
25 $(TARGET): $(D_OBJECTS) 
26 $(CC) $(D_OBJECTS) -o $(TARGET) 
27 
28 $(D_OBJECTS) : %.o: %.cpp      # ----- run with debug flags 
29 $(CC) $(COMMON_FLAGS) $(DEBUG_FLAGS) -c $< -o [email protected] 
30 
31 release: $(R_OBJECTS) 
32 $(CC) $(R_OBJECTS) -o $(TARGET) 
33 
34 $(R_OBJECTS) : %.o: %.cpp      # ----- run with release flags 
35 $(CC) $(COMMON_FLAGS) $(RELEASE_FLAGS) -c $< -o [email protected] 

當我make我得到的調試版本,當我make release我得到發行版本。

但我也得到警告:

Makefile:35: warning: overriding commands for target `src/Timer.o' 
Makefile:29: warning: ignoring old commands for target `src/Timer.o' 
Makefile:35: warning: overriding commands for target `test/TimerTest.o' 
Makefile:29: warning: ignoring old commands for target `test/TimerTest.o' 

有了這個2個問題:

  1. 任何方式忽略警告
  2. 我在做正確的事情?需要進行哪些更改?

回答

10

這樣做的最常見方法之一是將發佈對象和調試對象放在單獨的子目錄中。這樣你就不會重新定義對象的規則,因爲它們會有不同的名稱。這樣的事情:

D_OBJECTS=$(SRC:%.cpp=debug/%.o) 
R_OBJECTS=$(SRC:%.cpp=release/%.o) 

RTARGET = a.out 
DTARGET = a.out.debug 

all : dirs $(RTARGET) 

debug : dirs $(DTARGET) 

dirs : 
    @mkdir -p debug release 

debug/%.o : %.c 
    $(CC) $(DEBUG_CFLAGS) -o [email protected] -c $< 

release/%.o : %.c 
    $(CC) $(RELEASE_CFLAGS) -o [email protected] -c $< 

$(DTARGET) : $(D_OBJECTS) 
    $(CC) $(DEBUG_CFLAGS) -o [email protected] $(D_OBJECTS) 

$(RTARGET) : $(R_OBJECTS) 
    $(CC) $(RELEASE_CFLAGS) -o [email protected] $(R_OBJECTS) 
+1

你知道如何做到這一點在自動生成Makefile的netbeans? – 2013-06-23 17:52:18

+0

或創建一個庫,如果你有多個二進制代碼相同的代碼 – baptx 2014-05-17 15:38:25