使用GNU Make同时构建调试和发布目标 [英] Using GNU Make to build both debug and release targets at the same time

查看:83
本文介绍了使用GNU Make同时构建调试和发布目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个中等大小的项目中,该项目包含几个具有相互依赖性的库,最近我将它们转换为使用非递归makefile进行构建.我的下一个目标是允许同时在同一源代码树之外构建调试和发布构建(make debug; make release).我的第一步是制作包含正确构建标记的调试和发布目标.我使用目标特定变量来完成此操作,如下所示: CXXFLAGS =-墙-Wextra -Werror -DLINUX

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 next goal is to enable building of both debug and release builds out of the same source tree at the same time (make debug;make release). My first step was to make debug and release targets which contained the correct build flags. I did this using target specific variables, like this: CXXFLAGS=-Wall -Wextra -Werror -DLINUX

CXX_DEBUG_FLAGS=-g3 -DDEBUG_ALL
CXX_RELEASE_FLAGS=-O3

.PHONY: debug 
debug: CXXFLAGS+=$(CXX_DEBUG_FLAGS) 
debug: build

.PHONY: release 
release: CXXFLAGS+=$(CXX_RELEASE_FLAGS) 
release: build

这很好用,但是您只能同时构建调试或发布版本,而不能同时构建两者.同时,我并不是说在相同的构建过程中,我的意思是在同一源代码树中背对背(make debug; make release).为此,我需要将目标文件放在特定于调试/发布的目录中,以免它们相互覆盖,并且我需要用"D"修饰调试目标二进制名称.尽管这很容易,因为我可以再次使用目标特定的变量,如下所示: CXXFLAGS =-墙-Wextra -Werror -DLINUX

This worked fine, but you could only build debug, or release, not both at the same time. And by same time, I don't mean during the same build I mean back to back in the same source tree (make debug;make release). In order to do this I need to place the object files in a debug/release specific directory so they don't overwrite each other and I need to mangle the debug target binary name with a 'D'. I though this would be easy as I could just use target specific variables again, like this: CXXFLAGS=-Wall -Wextra -Werror -DLINUX

CXX_DEBUG_FLAGS=-g3 -DDEBUG_ALL
CXX_RELEASE_FLAGS=-O3

.PHONY: debug 
debug: CXXFLAGS+=$(CXX_DEBUG_FLAGS) 
debug: MODULE_BLD_TYPE=D
debug: OUT_DIR=debug_obj
debug: build

.PHONY: release 
release: CXXFLAGS+=$(CXX_RELEASE_FLAGS) 
release: MODULE_BLD_TYPE:=
release: OUT_DIR=release_obj
release: build

.PHONY: build
build: TARGET_NAME=HelloWorld$(MODULE_BLD_TYPE)
build: TARGET_BUILD_DIR=$(PROJECT_ROOT_DIR)/$(OUT_DIR)
build: TARGET_BUILD_OBJS=$(addprefix $(TARGET_BUILD_DIR)/,$(SOURCES:.cpp=.o))
build: $(TARGET_NAME)

您已经让专家阅读此书,因为它不能使用特定于目标的变量来创建实际目标,所以这已经行不通了.它们对我的CXXFLAGS var效果很好,因为该变量未在目标名称中使用.

You make experts reading this already know this won't work because you can't use target specific variables to create actual targets. They worked fine for my CXXFLAGS var because the variable wasn't used in a target name.

是否存在使用非递归Makefile管理调试/发布版本的设计模式和/或最佳实践?具体来说,如何构建目标文件目录路径和目标名称(基于目标构建目标)?

Is there a design pattern and or best practice to managing debug/release builds using non-recursive makefiles? Specificly, how do I build the object file directory path and target name (build a target based on a target)?

推荐答案

Make的最持久的问题之一是它无法一次处理多个通配符.没有真正干净的方法可以完成您要问的事情(不求助于递归,我认为这并不那么糟糕).这是一种合理的方法:

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)

这篇关于使用GNU Make同时构建调试和发布目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆