views:

90

answers:

1

I am reading a Makefile from someone else as follows.

LDFLAGS=-lm -ljpeg -lpng

ifeq ($(DEBUG),yes)
        OPTIMIZE_FLAG = -ggdb3 -DDEBUG -fno-omit-frame-pointer
else
        OPTIMIZE_FLAG = -ggdb3 -O3
endif

CXXFLAGS = -Wall $(OPTIMIZE_FLAG)

all: test

test: test.o source1.o source2.o
        $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

Makefile.depend: *.h *.cc Makefile
        $(CC) -M *.cc > Makefile.depend

clean:
        \rm -f test *.o Makefile.depend

-include Makefile.depend

Here are my questions:

  1. Although not explicitly, is $(CXXFLAGS) used by the implicit rule not shown in this Makefile during compilation to generate object files?

  2. I am also wondering why $(CXXFLAGS) appears in the linkage stage? I think it is only for compilation stage? Can I remove $(CXXFLAGS) from "$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)"? If I am wrong, does it mean g++ also generate debugging info and doing optimization at linkage?

  3. why use -ggdb3 -O3 together for nondebugging purpose? What whould its purpose be? If merely considering to improve speed, then isn't using -O3 only better?

  4. for debugging purpose, how using -ggdb3 -fno-omit-frame-pointer together will do better than -ggdb3 alone? I trying to understand the purpose of -fno-omit-frame-pointer by reading gcc document but still confused.

  5. can I move " -include Makefile.depend" to be just under "$(CC) -M *.cc > Makefile.depend" and above "clean"? Does its position in Makefile matter?

+2  A: 

CXXFLAGS is used by the implicit rule. Do make -n -p to see the complete list of variables and rules that Make generates from its internal rule sets and the make file.

-g debugging options are only used at the compile stage. -O options are used at the compile and link stages, potentially. You can use both together. From my gcc man page:

GCC allows you to use -g with -O. The shortcuts taken by optimized code may occasionally produce surprising results: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may not be executed because they compute constant results or their values were already at hand; some state- ments may execute in different places because they were moved out of loops.

Nevertheless it proves possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs.

-fno-omit-frame-pointer actually reduces the optimization. The optimization can interfere with debugging and the person who wrote this makefile clearly intended to run the debugger on optimized code. If it's unclear to you from the gcc manual how it works, you might want to sit down some Saturday with the Intel or AMD architecture reference manual and learn about function calls and argument passing at the instruction level. (Then again, maybe not. :) )

Position matters in Makefiles. I would leave that include file at the end. To do otherwise risks breaking the include file dependency checking.

Steve K
Thank you!Do you mean that it is "-ggdb3 -O3" that is intended to run the debugger on optimized code? For non-debugging purpose, "-ggdb3 -DDEBUG -fno-omit-frame-pointer" doesn't involve optimization, so why "-fno-omit-frame-pointer actually reduces the optimization" if there is no optimized code?
Tim
I can't say for sure. There is no guarantee this makefile is fully coherent. Perhaps that set of flags is leftover from another change or the person who wrote it didn't fully understand their meaning.
Steve K