C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-27-2009, 09:35 AM   #1
Registered User
 
Join Date: Jan 2009
Posts: 156
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!
lehe is offline   Reply With Quote
Old 08-27-2009, 09:45 AM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 08-27-2009, 09:52 AM   #3
Registered User
 
Join Date: Jan 2009
Posts: 156
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)
?
lehe is offline   Reply With Quote
Old 08-27-2009, 12:43 PM   #4
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Quote:
Originally Posted by lehe View Post
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.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 08-27-2009, 01:13 PM   #5
Registered User
 
Join Date: Jan 2009
Posts: 156
Thank you!
Is it possible to point me to some references of which option is done in which stage?
Thanks!
lehe is offline   Reply With Quote
Old 08-27-2009, 01:34 PM   #6
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
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.
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, etc.

New project: nort
dwks is offline   Reply With Quote
Reply

Tags
compile, debug, link, makefile, optimization

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 01:34 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22