Using GNU Make to build both debug and release targets at the same time

前端 未结 2 2015
抹茶落季
抹茶落季 2021-02-02 13:40

I\'m working on a medium sized project which contains several libraries with interdependence\'s which I\'ve recently converted over to build using a non-recursive makefile. My

相关标签:
2条回答
  • 2021-02-02 13:57

    One of the most persistent problems with Make is its inability to handle more than one wildcard at a time. There is no really clean way to do what you ask (without resorting to recursion, which I don't think is really so bad). Here is a reasonable approach:

    CXXFLAGS=-Wall -Wextra -Werror -DLINUX
    CXX_DEBUG_FLAGS=-g3 -DDEBUG_ALL 
    CXX_RELEASE_FLAGS=-O3 
    
    .PHONY: debug  
    debug: CXXFLAGS+=$(CXX_DEBUG_FLAGS)  
    debug: HelloWorldD
    
    .PHONY: release  
    release: CXXFLAGS+=$(CXX_RELEASE_FLAGS)
    release: HelloWorld
    
    DEBUG_OBJECTS = $(addprefix $(PROJECT_ROOT_DIR)/debug_obj/,$(SOURCES:.cpp=.o))
    RELEASE_OBJECTS = $(addprefix $(PROJECT_ROOT_DIR)/release_obj/,$(SOURCES:.cpp=.o))
    
    HelloWorldD: $(DEBUG_OBJECTS)
    HelloWorld: $(RELEASE_OBJECTS)
    
    # And let's add three lines just to ensure that the flags will be correct in case
    # someone tries to make an object without going through "debug" or "release":
    
    CXX_BASE_FLAGS=-Wall -Wextra -Werror -DLINUX
    $(DEBUG_OBJECTS): CXXFLAGS=$(CXX_BASE_FLAGS) $(CXX_DEBUG_FLAGS)
    $(RELEASE_OBJECTS): CXXFLAGS=$(CXX_BASE_FLAGS) $(CXX_RELEASE_FLAGS)
    
    0 讨论(0)
  • 2021-02-02 14:10

    Use VPATH to make debug and release builds use the same set of source files. The debug and release build can have their own directory, which means they'll have their object files separated.

    Alternatively, use a build tool that supports out-of-source builds natively, like automake or (ugh) cmake.

    If you enable the automake option subdir-objects (as in AM_INIT_AUTOMAKE([foreign subdir-objects])), you can write a non-recursive Makefile.am.

    0 讨论(0)
提交回复
热议问题