Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable GCC optimization when using makefiles?

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.

like image 779
inline Avatar asked Feb 13 '12 14:02

inline


2 Answers

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

like image 138
sehe Avatar answered Oct 20 '22 18:10

sehe


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
like image 41
Martin York Avatar answered Oct 20 '22 19:10

Martin York