# optimization and debugging options in Makefile

This is a discussion on optimization and debugging options in Makefile within the C++ Programming forums, part of the General Programming Boards category; Hi, I wonder where to put the optimization and debugging options in Makefile: linking stage or compiling stage? I am ...

1. ## optimization and debugging options in Makefile

Hi,
I wonder where to put the optimization and debugging options in Makefile: linking stage or compiling stage? I am reading a Makefile:
Code:
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?

Thanks and regards!

2. Given that optimization happens only at compilation and not at linking, and that debugging information happens only at compilation and not at linking, I'm not sure why you're even asking the question.

3. Thanks! I am learning to use someone's Makefile
So it is not use to include CXXFLAG in linking as in his Makefile
Code:
test: test.o rgb_image.o
$(CXX)$(CXXFLAGS) -o $@$^ $(LDFLAGS) ? 4. Originally Posted by lehe Thanks! I am learning to use someone's Makefile So it is not use to include CXXFLAG in linking as in his Makefile Code: test: test.o rgb_image.o$(CXX) $(CXXFLAGS) -o$@ $^$(LDFLAGS)
?
The $(CXXFLAGS) are almost redundant here. However, the linker does need access to$(PROFILE_FLAG) and so that should be added to the $(LDFLAGS). Code: ifeq ($(STATIC),yes)
LDFLAGS=-static -lm -ljpeg -lpng -lz $(PROFILE_FLAG) else LDFLAGS=-lm -ljpeg -lpng$(PROFILE_FLAG)
endif
Then you should be able to remove \$(CXXFLAGS) from the link line.

5. Thank you!
Is it possible to point me to some references of which option is done in which stage?
Thanks!

6. Well, you could try the manual. It has some categories of options, but it's pretty detailed. I guess the best way to understand it is to know what debugging information is. Debugging information is just extra information that the compiler adds to the program so that you can tell which line of C code an assembly instruction corresponded to, for example.

The linker doesn't care about debugging information; all it does is take the already-existing object files and link them together. Its primary job is to figure out what external symbols refer to; it has to resolve all references in one object file to somewhere else, so that a coherent executable is formed in the end.