Thread: compiling for gdb using make

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    3

    compiling for gdb using make

    I am learning C++ in a Linux environment.
    I have a simple program consisting of 3 files as follows;

    carClassApp.cpp //contains main()
    Car.cpp // class implementation file
    Car.h // class header file

    I am trying to compile it with a makefile as follows;

    //////////////////////
    car: Car.o carClassApp.o
    g++ Car.o carClassApp.o –o car

    Car.o: Car.cpp
    g++ -c Car.cpp

    carClassApp.o: carClassApp.cpp
    g++ -c carClassApp.cpp
    ///////////////////

    Does this look correct?

    Now I want to compile it so I can use gdb to debug it.
    Do I simply delete all *.o files then edit the makefile by replacing the two –c flags with –g flags and then run make again? i.e.

    //////////////////////
    car: Car.o carClassApp.o
    g++ Car.o carClassApp.o –o car

    Car.o: Car.cpp
    g++ -g Car.cpp

    carClassApp.o: carClassApp.cpp
    g++ -g carClassApp.cpp
    ///////////////////

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not replace -c, just add "-g" to gcc. And while you are at it, perhaps you'd want to add "-Wall" to give you warnings, in case you do something the compiler can help you find.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    3
    Thanks. Should the -g flag be used in all stages of the compilation then? i.e.;

    Code:
    car: Car.o carClassApp.o
    g++ Car.o carClassApp.o –o -g car
    
    Car.o: Car.cpp
    g++ -c -g Car.cpp
    
    carClassApp.o: carClassApp.cpp
    g++ -c -g carClassApp.cpp
    Also, should Car.h be referenced specifically in the makefile? i.e.
    Code:
    car: Car.o carClassApp.o
    g++ Car.o carClassApp.o –o -g car
    
    Car.o: Car.cpp Car.h
    g++ -c -g Car.cpp Car.h
    
    carClassApp.o: carClassApp.cpp
    g++ -c -g carClassApp.cpp
    Car.cpp contains the line;
    Code:
    #include "Car.h"
    Compiling and building has me a little confused all round. Does the position of the flags in the command matter?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yes, for debug builds use -g for each file.

    Also, you need to put a TAB at the beginning of each command line (i.e. the ones where you have "g++ ...". Must be a tab, not spaces.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    I haven't tried your make script but...

    First, if I remember correctly, indentation matters in a makefile.
    Code:
    carClassApp.o: carClassApp.cpp
    
    g++ -c -g carClassApp.cpp
    I don't know how much it should be indented but if you use an editor like emacs, you won't have to worry about how many spaces (or one tab) you need for proper indentation.

    Also, I bet you'd need to fix:
    Code:
    g++ Car.o carClassApp.o –g -o car
    The output option "-o" should always immediately precede the output file.

    Also, the object file targets do have to be explicitly indicated
    Code:
    Car.o: Car.cpp Car.h
    
    g++ -c -g Car.cpp Car.h -o Car.o
    However, if you use automatic variables
    Code:
    Car.o: Car.cpp Car.h
    
    g++ -c -g $^ -o $@
    it becomes simpler. "$^" means replace this with everything in the dependency list and "$@" means replace this with the target.

    You can even reduce the entire segment:
    Code:
    Car.o: Car.cpp Car.h
    g++ -c -g Car.cpp Car.h
    
    carClassApp.o: carClassApp.cpp
    g++ -c -g carClassApp.cpp
    with
    Code:
    %.o: %.cpp
    
    g++ -c -g $< -o $@
    However, for the above to work, you need another target which uses this target as a dependency. So the final version would work like
    Code:
    SRC = carClass.cpp Car.cpp
    OBJ = ${SRC:%.cpp=%.o}
    
    car: $(OBJ)
    
    g++ -o $@ $^
    %.o: %.cpp
    g++ -c -g $< -o $@
    Last edited by cunnus88; 11-29-2007 at 11:52 PM.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Come to think of it, I don't think you have to specify output when compiling. The compiler will probably automatically convert "Cars.cpp" to "Cars.o" without you having to explicitly state it.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    3

    Post

    Thanks all. I will digest what you have said.
    One thing I have found is that I tried;
    Code:
    car: Car.o carClassApp.o
              g++ Car.o carClassApp.o  -Wall –g -o car
    And problems occurred. I found I had to use ;
    Code:
    car: Car.o carClassApp.o
              g++ Car.o carClassApp.o  -o car
    Use of -Wall and -g caused problems when linking. Maybe they should only be used when compiling source code to create object files.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by enuenu View Post
    Thanks all. I will digest what you have said.
    One thing I have found is that I tried;
    Code:
    car: Car.o carClassApp.o
              g++ Car.o carClassApp.o  -Wall –g -o car
    And problems occurred. I found I had to use ;
    Code:
    car: Car.o carClassApp.o
              g++ Car.o carClassApp.o  -o car
    Use of -Wall and -g caused problems when linking. Maybe they should only be used when compiling source code to create object files.
    You _NEED_ to use -g at the link stage, otherwise the compiler will not produce the debug symbols that gdb needs. You can still use gdb, but you won't get any source or detailed symbol information.

    -Wall is only really useful in the compile stage [together with -c].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make tarball (.tgz) for app?
    By Marcux in forum Linux Programming
    Replies: 7
    Last Post: 06-22-2007, 06:02 PM
  2. trying to make a KenGen ( for a game tool )
    By lonewolfy in forum C# Programming
    Replies: 4
    Last Post: 03-28-2007, 08:23 AM
  3. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  4. help compiling genome assemblers
    By bluegirl in forum C Programming
    Replies: 1
    Last Post: 03-29-2006, 03:25 PM
  5. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM