如何将这样的Makefile段落对重写为一个段落?我有:
$(BUILD_DIR_DEBUG)/args.o: \
$(SRC)/args.c \
$(SRC)/args.h \
$(SRC)/something_else.h
$(CC) $(CFLAGS) $(CFLAGS_DEBUG) -c -o $@ $<
$(BUILD_DIR_RELEASE)/args.o: \
$(SRC)/args.c \
$(SRC)/args.h \
$(SRC)/something_else.h
$(CC) $(CFLAGS) $(CFLAGS_RELEASE) -c -o $@ $<
我很乐意得到这样的东西(不要假设正确的语法),但是我不知道如何定义这样的东西:
# ${BUILD_TYPE} to be iterated as "DEBUG" and "RELEASE" to get two current
# runtime rules from one actually written rule.
$(BUILD_DIR_$(BUILD_TYPE))/args.o: \
$(SRC)/args.c \
$(SRC)/args.h \
$(SRC)/something_else.h
$(CC) $(CFLAGS) $(CFLAGS_$(BUILD_TYPE)) -c -o $@ $<
ifeq ($(DEBUG), 1)
BUILD_DIR = $(BUILD_DIR_DEBUG)
CFLAGS = $(CFLAGS_DEBUG)
else
BUILD_DIR = $(BUILD_DIR_RELEASE)
CFLAGS = $(CFLAGS_RELEASE)
endif
$(BUILD_DIR)/args.o: \
$(SRC)/args.c \
$(SRC)/args.h \
$(SRC)/something_else.h
$(C) $(CFLAGS) -c -o $@ $<
并根据需要设置变量DEBUG
。例如make DEBUG=1
另请参见:
How can I configure my makefile for debug and release builds?
https://www.gnu.org/software/make/manual/html_node/Conditional-Example.html