C++ 如何将编译标志 -g 添加到 make 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12898287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add compile flag -g to a make file?
提问by Niek de Klein
I got a C++ program for which someone else made a make file. I want to compile the program with flag -g, but I don't know where to add it. Below is the make file.
我得到了一个 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
Where should I add that I want to use -g?
我应该在哪里添加我想使用-g?
回答by Rob?
$(CC) is used for compiling C programs. $(CXX) is used for compiling C++ programs. Similarly $(CFLAGS) is used for C programs, $(CXXFLAGS) is used for compiling C++.
$(CC) 用于编译 C 程序。$(CXX) 用于编译 C++ 程序。类似地,$(CFLAGS) 用于 C 程序,$(CXXFLAGS) 用于编译 C++。
Change the first few lines to this:
将前几行更改为:
#CC = g++
LOADLIBES = -lm
CXXFLAGS = -Wall -O2 -g
(But see others' notes about incompatibilities between -O2 and -g.)
(但请参阅其他人关于 -O2 和 -g 之间不兼容的注释。)
Get rid of the spaces inside the parentheses in this line:
去掉这一行括号内的空格:
OBJS = $(SRC1:.cpp=.o)
Change the main
lines to this:
将main
行更改为:
main: $(OBJS) $(SRC2)
# Built by implicit rules
The resulting makefile should look like this:
生成的 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
and the output should look like this:
输出应如下所示:
$ 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
For completeness, this is the version of make I am using on Ubuntu 10.04:
为了完整起见,这是我在 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
回答by piokuc
You need to uncomment the line:
您需要取消注释该行:
# $(CC) $(CFLAGS) -o $(SRC) $(AUX)
(remove the hash sigh):
(去除散列叹气):
$(CC) $(CFLAGS) -o $(SRC) $(AUX)
And change
并改变
CFLAGS = -Wall -O2
to
到
CFLAGS = -Wall -O2 -g
But you may find debugging easier if you disable optimization by removing -O2
:
但是,如果您通过删除来禁用优化,您可能会发现调试更容易-O2
:
CFLAGS = -Wall -g