2012-08-14 34 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