问题 如何将make flag -g添加到make文件中?


我有一个C ++程序,其他人制作了一个make文件。我想用标志-g编译程序,但我不知道在哪里添加它。下面是make文件。

CC = g++
LOADLIBES = -lm
CFLAGS = -Wall -O2


SRC1 = Agent.cpp Breeder.cpp CandidateSolution.cpp \
    Cupid.cpp FateAgent.cpp Grid.cpp Reaper.cpp \
    fitness.cpp

SRC2 = main.cpp

SRC  = $(SRC1) $(SRC2)

OBJS = $(SRC1:.cpp = .o)

AUX = $(SRC1:.c = .h)


main: $(OBJS) 
#   $(CC) $(CFLAGS) -o $(SRC) $(AUX) 

.PHONY: clean
clean:
    rm -f *.o main

我应该在哪里添加我想要使用-g?


3316
2017-10-15 14:55


起源

这个问题实际上与C ++无关,所以我删除了[c ++]标签 - John Dibling
根据Robᵩ的说法 - Niek de Klein
这个问题是关于如何使用Makefile。关于C ++语言,这个问题没有任何内容。您碰巧使用make来构建用C ++编写的程序,但它用C ++编写的事实是正交的。您的代码也可能是用FORTRAN编写的。 - John Dibling
@JohnDibling我不同意C ++与这个Makefile正交。正如答案所证明的那样,Makefile具有特定于C ++的特殊“烘焙”语法,因此Makefile与C ++具有内在关系,比FORTRAN更多,特别是当像OP那样使用这些C ++特定的标志时。 - Magnus


答案:


$(CC)用于编译C程序。 $(CXX)用于编译C ++程序。类似地,$(CFLAGS)用于C程序,$(CXXFLAGS)用于编译C ++。

将前几行更改为:

#CC = g++
LOADLIBES = -lm
CXXFLAGS = -Wall -O2 -g

(但请参阅其他人关于-O2和-g之间不兼容性的说明。)

摆脱这一行中括号内的空格:

OBJS = $(SRC1:.cpp=.o)

改变 main 这条线:

main: $(OBJS) $(SRC2)
#   Built by implicit rules

生成的makefile应如下所示:

#CC = g++
LOADLIBES = -lm
CXXFLAGS = -Wall -O2 -g


SRC1 = Agent.cpp Breeder.cpp CandidateSolution.cpp \
    Cupid.cpp FateAgent.cpp Grid.cpp Reaper.cpp \
    fitness.cpp

SRC2 = main.cpp

SRC  = $(SRC1) $(SRC2)

OBJS = $(SRC1:.cpp=.o)

AUX = $(SRC1:.c=.h)

main: $(OBJS) $(SRC2)
#   Built by implicit rules

.PHONY: clean
clean:
    rm -f *.o main

输出应如下所示:

$ make
g++ -Wall -O2 -g   -c -o Agent.o Agent.cpp
g++ -Wall -O2 -g   -c -o Breeder.o Breeder.cpp
g++ -Wall -O2 -g   -c -o CandidateSolution.o CandidateSolution.cpp
g++ -Wall -O2 -g   -c -o Cupid.o Cupid.cpp
g++ -Wall -O2 -g   -c -o FateAgent.o FateAgent.cpp
g++ -Wall -O2 -g   -c -o Grid.o Grid.cpp
g++ -Wall -O2 -g   -c -o Reaper.o Reaper.cpp
g++ -Wall -O2 -g   -c -o fitness.o fitness.cpp
g++ -Wall -O2 -g    main.cpp Agent.o Breeder.o CandidateSolution.o Cupid.o FateAgent.o Grid.o Reaper.o fitness.o -lm  -o main

为了完整性,这是我在Ubuntu 10.04上使用的make版本:

$ make -v
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for i486-pc-linux-gnu

13
2017-10-15 15:09



那输出 #g++ -o Agent.cpp Breeder.cpp CandidateSolution.cpp Cupid.cpp FateAgent.cpp Grid.cpp Reaper.cpp fitness.cpp main.cpp Agent.cpp Breeder.cpp CandidateSolution.cpp Cupid.cpp FateAgent.cpp Grid.cpp Reaper.cpp fitness.cpp 但不会产生主文件。 - Niek de Klein
请参阅我的编辑与实际的makefile和实际输出。 - Robᵩ
Makefile:23: *** missing separator. Stop. 我忘了提到我用的是Mac GNU Make 3.81 Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This program built for i386-apple-darwin10.0 - Niek de Klein
第23行的第一个字符必须是TAB,而不是一系列空格。 - Robᵩ


您需要取消注释该行:

#   $(CC) $(CFLAGS) -o $(SRC) $(AUX) 

(删除哈希叹息):

   $(CC) $(CFLAGS) -o $(SRC) $(AUX) 

并改变

CFLAGS = -Wall -O2

CFLAGS = -Wall -O2 -g

但是如果通过删除禁用优化,您可能会发现调试更容易 -O2

CFLAGS = -Wall -g

3
2017-10-15 14:58



这给了我一个ld错误并删除了Agent.cpp - Niek de Klein