Thread: how to debug multiple files /w using makefile

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    11

    how to debug multiple files /w using makefile

    Hi there,

    I am using a simple makefile to build a project that needs debugging, but the methods I need to look at are not in the same file as main(). Can some explain to me how to do this, using gdb and make? Here is the makefike:


    all: Swara

    Swara: swara.o synth.o unit.o sinewave.o
    cc -g swara.o synth.o unit.o sinewave.o -o Swara -lstdc++ -lportaudio -lsndfile

    swara.o: swara.cpp swara.hpp
    cc -g -c swara.cpp swara.hpp -lstdc++

    synth.o: synth.cpp synth.hpp
    cc -g -c synth.cpp synth.hpp -lportaudio -lsndfile

    unit.o: unit.cpp unit.hpp
    cc -g -c unit.cpp unit.hpp

    sinewave.o: sinewave.cpp sinewave.hpp
    cc -g -c sinewave.cpp sinewave.hpp


    thanks if you can help!

    rich

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    GDB will automatically move between files. You don't have to do anything special.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    swara.o: swara.cpp swara.hpp
    cc -g -c swara.cpp swara.hpp -lstdc++
    
    synth.o: synth.cpp synth.hpp
    cc -g -c synth.cpp synth.hpp -lportaudio -lsndfile
    
    unit.o: unit.cpp unit.hpp
    cc -g -c unit.cpp unit.hpp
    
    sinewave.o: sinewave.cpp sinewave.hpp
    cc -g -c sinewave.cpp sinewave.hpp
    You almost certainly don't want to specify the .hpp files as inputs to gcc, and when using the -c flag, linking isn't done, so you also shouldn't give -lsomething to gcc when using the -c flag (and yes, you are using the -c flag properly).

    I would also recommend something like this:
    Code:
    CFLAGS = -g -Wall -pedantic
    CC = cc ${CFLAGS}
    ...
    swara.o: swara.cpp swara.hpp
    ${CC} ${CFLAGS} -c swara.cpp
    ...
    -Wall will enable warnings - eliminate any warnings you get, as that will most likely make your code more reliable / correct.

    A question: Does none of your other .cpp fiels include any of the .hpp files, e.g. I would expect sinewave.hpp to be included in some other file to enable the use of it's content - if so, you should add this header to the dependencies of the other .cpp files that include it. There's few things worse than building a project, and finding out after several debug attempts that the OTHER modules never got rebuilt when you changed a header file, so you're using the wrong (old) data format in the other modules - this is sometimes VERY OBVIOUS, but at other times not so obvious, and you just get very weird bugs where data gets overwritten in random ways...

    --
    Mats

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    11
    Ah... thanks for helping me clean it up! It appears that adding header files to the cc command was not allowing the debugger to see the files? As you can see, I'm quite new to makefiles still, just been trying to piece it together through the use of google.

    Would this be a good place to ask some questions about the problems I'm having using the Autotools? I'd like to get that working with my project before too long, this was just a temporary solution.

    thanks Mats, others,
    rich

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I suggest using a Makefile like this. You'll have to replace the spaces with tabs.
    Code:
    OBJECTS = swara.o synth.o unit.o sinewave.o
    LDFLAGS = -lstdc++ -lportaudio -lsndfile
    TARGET = Swara
    
    # Default target: all
    all: $(TARGET)
    
    $(TARGET): $(OBJECTS)
            $(CC) $(CFLAGS) $(OBJECTS) -o $(TARGET) $(LDFLAGS)
    
    # Source files
    swara.o: swara.cpp swara.hpp
    synth.o: synth.cpp synth.hpp
    unit.o: unit.cpp unit.hpp
    sinewave.o: sinewave.cpp sinewave.hpp
    Make has built-in rules that automatically know how to build .c, .cpp, and other source files.

    The blue code can be automatically generated (with the proper dependencies) with
    Code:
    gcc -MM *.cpp
    Also see matsp's suggestions.

    And you might perhaps want some targets like this:
    Code:
    clean:
            rm $(OBJECTS) $(TARGET)
    run: $(TARGET)
            $(TARGET)
    debug: $(TARGET)
            gdb $(TARGET)
    Finally, if you want to set a breakpoint or whatever on line 80 of a different file from the current one in gdb, use
    Code:
    (gdb) break otherfile.cpp:80
    GDB has a pretty extensive help system. You can type help or help breakpoints etc to get more information. And there's always some online manuals and tutorials for gdb. (One on this very site.)

    [edit] Oh yes, and if you create a file called, say, all, then the all target won't work properly. You'll have to add
    Code:
    .PHONY: all clean run
    etc if you create files by those names. [/edit]
    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

Similar Threads

  1. header files and multiple files
    By Dedalus in forum C Programming
    Replies: 5
    Last Post: 06-16-2009, 09:21 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Makefile producing a list of files.
    By leonm54 in forum Tech Board
    Replies: 1
    Last Post: 07-23-2007, 09:54 AM
  4. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  5. Opening Multiple Files in sequence
    By wujiajun in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2006, 08:47 PM