views:

413

answers:

1

I wonder where to put the optimization and debugging options in Makefile: linking stage or compiling stage? I am reading a Makefile:

ifeq ($(STATIC),yes)
  LDFLAGS=-static -lm -ljpeg -lpng -lz
else
  LDFLAGS=-lm -ljpeg -lpng
endif

ifeq ($(DEBUG),yes)
  OPTIMIZE_FLAG = -ggdb3 -DDEBUG
else
  OPTIMIZE_FLAG = -ggdb3 -O3
endif

ifeq ($(PROFILE),yes)
  PROFILE_FLAG = -pg
endif

CXXFLAGS = -Wall $(OPTIMIZE_FLAG) $(PROFILE_FLAG) $(CXXGLPK)

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

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

clean:
    \rm -f absurdity *.o Makefile.depend TAGS

-include Makefile.depend

What surprises me is CXXFLAGS is used in linking. I know it is also used in the implicit rule for compiling to generate .o files but is it necessary to use it again for linking? Specifically, where should I put optimization and debugging: linking stage or compiling stage?

+1  A: 

Short answer:

  • optimization: needed at compiler time

  • debug flag: needed at compile time

  • debugging symbols: need at both compile and linking time

Take note that the linker decides what bits of each object file and library need to be included in the final executable. It could throw out the debugging symbols (I don't know what the default behavior is), so you need to tell it not to.

Further, the linker will silently ignore options which do not apply to it.


To the comments:

The above are very general claims based on knowing what happens at each stage of compilation, so no reference.

A few more details:

  • optimization: takes two major forms: peephole optimization can occur very late, because it works on a few assembly instructions at a time (I presume that in the GNU tool chain the assembler is responsible for this step), but the big gains are in structural optimizations that are generally accomplished by re-writing the Abstract Syntax Tree (AST) which is only possible during compilation.

  • debug flag: In your example this is a preprocessor directive, and only affects the first part of the compilation process.

  • debugging symbols: Look up the ELF file format (for instance), you'll see that various bits of code and data are organized into different blocks. Debugging symbols are stored in the same file along as the code they relate to, but are necessarily kept separate from the actual code. As such, any program that manipulates these files could just dump it. Therefore both the compiler and the linker need to know if you want them or not.

dmckee
So is CXXFLAGS better to be always specified in both linking and compiling?
Tim
Yes .
dmckee
Thank you so much!Do you have some reference for "which thing is done in which stage"?About "the linker could throw out the debugging symbols", do you mean add -fno-omit-frame-pointer to CXXFLAGS in the case of debugging? Isn't it auto set with -g or -O0?
Tim