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

Nie*_*ein 11 c++ makefile

我有一个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
Run Code Online (Sandbox Code Playgroud)

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

Rob*_*obᵩ 14

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

将前几行更改为:

#CC = g++
LOADLIBES = -lm
CXXFLAGS = -Wall -O2 -g
Run Code Online (Sandbox Code Playgroud)

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

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

OBJS = $(SRC1:.cpp=.o)
Run Code Online (Sandbox Code Playgroud)

main行更改为:

main: $(OBJS) $(SRC2)
#   Built by implicit rules
Run Code Online (Sandbox Code Playgroud)

生成的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
Run Code Online (Sandbox Code Playgroud)

输出应如下所示:

$ 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
Run Code Online (Sandbox Code Playgroud)

为了完整性,这是我在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
Run Code Online (Sandbox Code Playgroud)