How do I disable GCC optimization when using makefiles?
Solution 1
In some standard makefile settings you could
make -j10 -e CPPFLAGS=-O0
But the makefile might use other substitution variables or override the environment. You need to show us the Makefile in order to propose edits
Solution 2
The simplest (useful) makefile that allows debug/release mode is:
#
# Define the source and object files for the executable
SRC = $(wildcard *.cpp)
OBJ = $(patsubst %.cpp,%.o, $(SRC))
#
# set up extra flags for explicitly setting mode
debug: CXXFLAGS += -g
release: CXXFLAGS += -O3
#
# Link all the objects into an executable.
all: $(OBJ)
$(CXX) -o example $(LDFLAGS) $(OBJ) $(LOADLIBES) $(LDLIBS)
#
# Though both modes just do a normal build.
debug: all
release: all
clean:
rm $(OBJ)
Usage Default build (No specified optimizations)
> make
g++ -c -o p1.o p1.cpp
g++ -c -o p2.o p2.cpp
g++ -o example p1.o p2.o
Usage: Release build (uses -O3)
> make clean release
rm p1.o p2.o
g++ -O3 -c -o p1.o p1.cpp
g++ -O3 -c -o p2.o p2.cpp
g++ -o example p1.o p2.o
Usage: Debug build (uses -g)
> make clean debug
rm p1.o p2.o
g++ -g -c -o p1.o p1.cpp
g++ -g -c -o p2.o p2.cpp
g++ -o example p1.o p2.o
inline
Updated on June 09, 2022Comments
-
inline almost 2 years
I've just started learning Linux and I'm having some trouble disabling GCC's optimization for one of my C++ projects.
The project is built with makefiles like so...
make -j 10 && make install
I've read on various sites that the command to disable optimization is something along the lines of...
gcc -O0 <your code files>
Could someone please help me apply this to makefiles instead of individual code? I've been searching for hours and have come up empty handed.
-
Admin about 12 years
CPPFLAGS
often stands for C preprocessor flags,CXXFLAGS
are the flags for the C++ compiler. -
Martin York about 12 yearsNobody writes a makefile like that. In fact you don't even need a file for that just type
make main
-
rasmus about 12 yearsOf course, it's not a usable makefile. Just a minimal example of how to enable/disable optimizations.
-
sehe about 12 years@Fanael while that may be true,
CPPFLAGS
is usually added toCXXFLAGS
andCFLAGS
which makes it the more general entry point. -
Martin York about 12 years@Fanael: The default rule for C++ files is
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)
-
Martin York about 12 years@sehe: You are making assumptions about the current shell. You should specify the shell you are using as the OP is obviously not fluent in *nix. Also note a simpler way for bash (and family) is
CPPFLAGS=-O0 make -e -j10
as the sets CPPFLAGS only for the current command not in a persistant way that will affect other commands. -
Maxim Egorushkin about 12 yearsNote, that your target specific variables won't work when a target other than
debug
orrelease
specified. A more robust way is stackoverflow.com/a/5153406/412080 -
Martin York about 12 years@MaximYegorushkin: It works fine with no target specified. It is designed to use no variables when you don't specify anything. It will use the first rule
all
which just builds the code. As Shown Above. I see no advantage to the makefile you link too. It is definitely not more robust. It just does the same thing using a different technique (except unless you specify something it defaults to -g (which is not what I would want)). -
sehe about 12 years@LokiAstari: What? We are psychic-debugging an entire Makefile out of thin air and you are concerned with assumptions about a shell. I know that? :). PS. Make that
make -e CPPFLAGS=-O0
then, anyway, will fix -
Maxim Egorushkin about 12 yearsRight, it won't work for
make this that
, only formake
,make debug
andmake release
. -
Martin York about 12 years@MaximYegorushkin: Exactly. That is a good thing not a bad thing. IT may seem like flexibility is a good thing. But in reality flexibility is bain on robustness.
-
Maxim Egorushkin about 12 yearsThis is because
CXXFLAGS
you customize fordebug
andrelease
only apply when you build one of these targets. They don't apply if you specify that you want to build one executable out of say 10 you have in total. That's why this way of customizing the build is not robust. -
Martin York about 12 yearsIf you are building 10 executable out of one directory you have other problems in your code organization that go for beyond the problems associated with makefiles :-)
-
Maxim Egorushkin about 12 yearsI build 100 executables and 300 shared libraries using non-recursive make dude. Some sub-projects build 10s of executables and I can build a sub-project only, but sometimes I want to build just one executable. Make a sub-project/directory for each executable would be maintenance nightmare.
-
Martin York about 12 yearsI build many more. And for some strange reason I tend not to stick all my source files in the same directory. My make file in each directory looks like this:
TARGET=app <newline> include $(BASE)/Make.global
So each makefile is about two lines long and very easy to maintain. -
Maxim Egorushkin about 12 yearsReminds me of: no matter how many you build, i build +1 more. ) If you build many more I'd expect you to get the build flags right...
-
Maxim Egorushkin about 12 yearsOkay, so to build 3 executables only you need to invoke make 3 times, right?
-
Martin York about 12 years@MaximYegorushkin: I am not super human. I tend to work on one project at a time. When finished I submit it to the build system for validation which builds everything and validates. The maintenance night mare seems not to be the makefile though I am sure it looks horrible but in your whole approach to dumping everything into one directory. You should take a leaf out of the tried and true methods used by any major project. There are pleanty of open source projects out there. They don't do it like you and for a good reason. Please take the time and have a look. You may learn something.
-
Maxim Egorushkin about 12 yearsI didn't say all my sources in one directory, but I said "non-recursive make", which is a different thing. Majority of open source projects have poor structure and horrible makefiles. You may like miller.emu.id.au/pmiller/books/rmch
-
Maxim Egorushkin about 12 yearsYou don't need
-e
though,make
command line variable assignments override those from the makefile. -
Martin York about 12 years@MaximYegorushkin: That's like saying most car drivers are bad. I agree. But you can still study the best car drivers and improve your own driving technique from them. There are plenty of big open source projects with a good makefile layout.