Thread: optimization and debugging options in Makefile

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    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. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thank you!
    Is it possible to point me to some references of which option is done in which stage?
    Thanks!

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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, nort, etc.

Popular pages Recent additions subscribe to a feed

Tags for this Thread