Debugging, optimization and profiling - which happens at which stage(s) (compilation and link)
Tim Prince
n8tm@aol.com
Sat Aug 29 13:30:00 GMT 2009
Tim wrote:
> 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
>
> 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?
>
When $(CXX) is used to drive the link step, it's entirely appropriate to
pass it the same flags used at compile time, even though many of them
will be ignored. In this case, the flag -pg must be consistent between
compile and link steps.
I believe most C++ compilers implicitly include -lm, so that is
redundant yere.
More information about the Gcc-help
mailing list