Debugging, optimization and profiling - which happens at which stage(s) (compilation and link)

Tim timlee126@yahoo.com
Fri Aug 28 21:57:00 GMT 2009


Hi,

In most Makefiles I have seen, CXXFLAGS is used in the compile rule, either implicitly or explicitly. A few days ago, however I saw a Makefile that uses CXXFLAGS in link stage. Its compile rule is implicit which I suppose to use CXXFLAGS as well. The Makefile is as follows:

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

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

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

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

all: test

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

Does it mean that the optimization, debug options or/and profiling options are necessary in both compile and link stages? So is it better to use CXXFLAGS in link stage too?

I have got some answers from different people but they are not consistent. Some said the optimization option is only needed in compile, the debugging options is for both compile and link stages. Some said the opposite, i.e. the debugging option is only for compile stage, and the optimization option is for both compile and link stages. Some said the profiling option is only for link stage and needs to be moved from CXXFLAGS to LDFLAGS, but I found in the document of Make, -pg appears in CXXFLAGS in some examples.

If possible, could you please point me to some reference about which thing is done on which stage(s)?

Thanks and regards!

-Tim


      



More information about the Gcc-help mailing list