How to add compile flag -g to a make file?

后端 未结 2 768
别那么骄傲
别那么骄傲 2021-01-04 05:11

I got a C++ program for which someone else made a make file. I want to compile the program with flag -g, but I don\'t know where to add it. Below is the make file.



        
相关标签:
2条回答
  • 2021-01-04 05:45

    You need to uncomment the line:

    #   $(CC) $(CFLAGS) -o $(SRC) $(AUX) 
    

    (remove the hash sigh):

       $(CC) $(CFLAGS) -o $(SRC) $(AUX) 
    

    And change

    CFLAGS = -Wall -O2
    

    to

    CFLAGS = -Wall -O2 -g
    

    But you may find debugging easier if you disable optimization by removing -O2:

    CFLAGS = -Wall -g
    
    0 讨论(0)
  • 2021-01-04 06:02

    $(CC) is used for compiling C programs. $(CXX) is used for compiling C++ programs. Similarly $(CFLAGS) is used for C programs, $(CXXFLAGS) is used for compiling C++.

    Change the first few lines to this:

    #CC = g++
    LOADLIBES = -lm
    CXXFLAGS = -Wall -O2 -g
    

    (But see others' notes about incompatibilities between -O2 and -g.)

    Get rid of the spaces inside the parentheses in this line:

    OBJS = $(SRC1:.cpp=.o)
    

    Change the main lines to this:

    main: $(OBJS) $(SRC2)
    #   Built by implicit rules
    

    The resulting makefile should look like this:

    #CC = g++
    LOADLIBES = -lm
    CXXFLAGS = -Wall -O2 -g
    
    
    SRC1 = Agent.cpp Breeder.cpp CandidateSolution.cpp \
        Cupid.cpp FateAgent.cpp Grid.cpp Reaper.cpp \
        fitness.cpp
    
    SRC2 = main.cpp
    
    SRC  = $(SRC1) $(SRC2)
    
    OBJS = $(SRC1:.cpp=.o)
    
    AUX = $(SRC1:.c=.h)
    
    main: $(OBJS) $(SRC2)
    #   Built by implicit rules
    
    .PHONY: clean
    clean:
        rm -f *.o main
    

    and the output should look like this:

    $ make
    g++ -Wall -O2 -g   -c -o Agent.o Agent.cpp
    g++ -Wall -O2 -g   -c -o Breeder.o Breeder.cpp
    g++ -Wall -O2 -g   -c -o CandidateSolution.o CandidateSolution.cpp
    g++ -Wall -O2 -g   -c -o Cupid.o Cupid.cpp
    g++ -Wall -O2 -g   -c -o FateAgent.o FateAgent.cpp
    g++ -Wall -O2 -g   -c -o Grid.o Grid.cpp
    g++ -Wall -O2 -g   -c -o Reaper.o Reaper.cpp
    g++ -Wall -O2 -g   -c -o fitness.o fitness.cpp
    g++ -Wall -O2 -g    main.cpp Agent.o Breeder.o CandidateSolution.o Cupid.o FateAgent.o Grid.o Reaper.o fitness.o -lm  -o main
    

    For completeness, this is the version of make I am using on Ubuntu 10.04:

    $ make -v
    GNU Make 3.81
    Copyright (C) 2006  Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.
    There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
    PARTICULAR PURPOSE.
    
    This program built for i486-pc-linux-gnu
    
    0 讨论(0)
提交回复
热议问题