196

我的项目有以下生成文件,我想将其配置为发布和调试版本。在我的代码中,我有很多#ifdef DEBUG宏,因此只需设置此宏并将-g3 -gdwarf2标志添加到编译器即可。我怎样才能做到这一点?

$(CC) = g++ -g3 -gdwarf2
$(cc) = gcc -g3 -gdwarf2

all: executable

executable: CommandParser.tab.o CommandParser.yy.o Command.o
    g++ -g -o output CommandParser.yy.o CommandParser.tab.o Command.o -lfl

CommandParser.yy.o: CommandParser.l 
    flex -o CommandParser.yy.c CommandParser.l
    gcc -g -c CommandParser.yy.c

CommandParser.tab.o: CommandParser.y
    bison -d CommandParser.y
    g++ -g -c CommandParser.tab.c

Command.o: Command.cpp
    g++ -g -c Command.cpp

clean:
    rm -f CommandParser.tab.* CommandParser.yy.* output *.o

澄清一下,当我说发布/调试构建时,我希望能够只键入make并获取发布构建或make debug获取调试构建,而无需手动注释掉 makefile 中的内容。

4

7 回答 7

214

您可以使用特定于目标的变量值。例子:

CXXFLAGS = -g3 -gdwarf2
CCFLAGS = -g3 -gdwarf2

all: executable

debug: CXXFLAGS += -DDEBUG -g
debug: CCFLAGS += -DDEBUG -g
debug: executable

executable: CommandParser.tab.o CommandParser.yy.o Command.o
    $(CXX) -o output CommandParser.yy.o CommandParser.tab.o Command.o -lfl

CommandParser.yy.o: CommandParser.l 
    flex -o CommandParser.yy.c CommandParser.l
    $(CC) -c CommandParser.yy.c

请记住在所有编译命令中使用 $(CXX) 或 $(CC)。

然后,'make debug' 将有额外的标志,如 -DDEBUG 和 -g,而'make' 不会。

附带说明一下,您可以像其他帖子建议的那样使您的 Makefile 更加简洁。

于 2009-07-03T17:07:52.857 回答
57

这个问题在搜索类似问题时经常出现,所以我觉得有必要提供一个完全实施的解决方案。特别是因为我(我会假设其他人)一直在努力将所有不同的答案拼凑在一起。

下面是一个示例 Makefile,它支持不同目录中的多种构建类型。图示的示例显示了调试和发布版本。

支持...

  • 特定构建的单独项目目录
  • 轻松选择默认目标构建
  • 静默准备目标以创建构建项目所需的目录
  • 特定于构建的编译器配置标志
  • GNU Make 确定项目是否需要重新构建的自然方法
  • 模式规则而不是过时的后缀规则

#
# Compiler flags
#
CC     = gcc
CFLAGS = -Wall -Werror -Wextra

#
# Project files
#
SRCS = file1.c file2.c file3.c file4.c
OBJS = $(SRCS:.c=.o)
EXE  = exefile

#
# Debug build settings
#
DBGDIR = debug
DBGEXE = $(DBGDIR)/$(EXE)
DBGOBJS = $(addprefix $(DBGDIR)/, $(OBJS))
DBGCFLAGS = -g -O0 -DDEBUG

#
# Release build settings
#
RELDIR = release
RELEXE = $(RELDIR)/$(EXE)
RELOBJS = $(addprefix $(RELDIR)/, $(OBJS))
RELCFLAGS = -O3 -DNDEBUG

.PHONY: all clean debug prep release remake

# Default build
all: prep release

#
# Debug rules
#
debug: $(DBGEXE)

$(DBGEXE): $(DBGOBJS)
    $(CC) $(CFLAGS) $(DBGCFLAGS) -o $(DBGEXE) $^

$(DBGDIR)/%.o: %.c
    $(CC) -c $(CFLAGS) $(DBGCFLAGS) -o $@ $<

#
# Release rules
#
release: $(RELEXE)

$(RELEXE): $(RELOBJS)
    $(CC) $(CFLAGS) $(RELCFLAGS) -o $(RELEXE) $^

$(RELDIR)/%.o: %.c
    $(CC) -c $(CFLAGS) $(RELCFLAGS) -o $@ $<

#
# Other rules
#
prep:
    @mkdir -p $(DBGDIR) $(RELDIR)

remake: clean all

clean:
    rm -f $(RELEXE) $(RELOBJS) $(DBGEXE) $(DBGOBJS)
于 2013-12-29T21:41:07.003 回答
43

如果通过配置发布/构建,您的意思是每个 makefile 只需要一个配置,那么这很简单,解耦 CC 和 CFLAGS:

CFLAGS=-DDEBUG
#CFLAGS=-O2 -DNDEBUG
CC=g++ -g3 -gdwarf2 $(CFLAGS)

根据您是否可以使用 gnu makefile,您可以使用 conditional 来使它更漂亮,并从命令行控制它:

DEBUG ?= 1
ifeq ($(DEBUG), 1)
    CFLAGS =-DDEBUG
else
    CFLAGS=-DNDEBUG
endif

.o: .c
    $(CC) -c $< -o $@ $(CFLAGS)

然后使用:

make DEBUG=0
make DEBUG=1

如果你需要同时控制这两种配置,我认为最好有一个构建目录,一个构建目录/config。

于 2009-07-03T15:28:33.200 回答
26

请注意,您还可以同时使 Makefile 更简单:

DEBUG ?= 1
ifeq (DEBUG, 1)
    CFLAGS =-g3 -gdwarf2 -DDEBUG
else
    CFLAGS=-DNDEBUG
endif

CXX = g++ $(CFLAGS)
CC = gcc $(CFLAGS)

EXECUTABLE = output
OBJECTS = CommandParser.tab.o CommandParser.yy.o Command.o
LIBRARIES = -lfl

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CXX) -o $@ $^ $(LIBRARIES)

%.yy.o: %.l 
    flex -o $*.yy.c $<
    $(CC) -c $*.yy.c

%.tab.o: %.y
    bison -d $<
    $(CXX) -c $*.tab.c

%.o: %.cpp
    $(CXX) -c $<

clean:
    rm -f $(EXECUTABLE) $(OBJECTS) *.yy.c *.tab.c

现在您不必到处重复文件名。任何 .l 文件都将通过 flex 和 gcc 传递,任何 .y 文件将通过 bison 和 g++ 传递,任何 .cpp 文件将仅通过 g++ 传递。

只需列出您希望最终得到的 .o 文件,Make 将完成确定哪些规则可以满足需求的工作......

作为记录:

  • $@目标文件的名称(冒号前的那个)

  • $<第一个(或唯一一个)必备文件的名称(冒号后的第一个)

  • $^所有必备文件的名称(空格分隔)

  • $*词干(与%规则定义中的通配符匹配的位。

于 2009-07-03T16:18:07.530 回答
3

你可以有一个变量

DEBUG = 0

那么你可以使用条件语句

  ifeq ($(DEBUG),1)

  else

  endif
于 2009-07-03T15:46:21.503 回答
2

完成前面的答案...您需要引用您在命令中定义 info 的变量...

DEBUG ?= 1
ifeq (DEBUG, 1)
    CFLAGS =-g3 -gdwarf2 -DDEBUG
else
    CFLAGS=-DNDEBUG
endif

CXX = g++ $(CFLAGS)
CC = gcc $(CFLAGS)

all: executable

executable: CommandParser.tab.o CommandParser.yy.o Command.o
    $(CXX) -o output CommandParser.yy.o CommandParser.tab.o Command.o -lfl

CommandParser.yy.o: CommandParser.l 
    flex -o CommandParser.yy.c CommandParser.l
    $(CC) -c CommandParser.yy.c

CommandParser.tab.o: CommandParser.y
    bison -d CommandParser.y
    $(CXX) -c CommandParser.tab.c

Command.o: Command.cpp
    $(CXX) -c Command.cpp

clean:
    rm -f CommandParser.tab.* CommandParser.yy.* output *.o
于 2009-07-03T15:59:06.230 回答
1

您还可以在 Makefile 中添加一些简单的内容,例如

ifeq ($(DEBUG),1)
   OPTS = -g
endif

然后编译调试

make DEBUG=1

于 2020-03-13T11:12:55.013 回答