Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a program compiled with 'make'?

Tutorials for gdb suggest compiling with 'gcc -g' to compile the program with debug symbols.

However, I want to debug a program compiled with make. How can I instruct make to compile with debugging symbols?

Thanks.

like image 504
Anders Feder Avatar asked Oct 06 '11 00:10

Anders Feder


2 Answers

In order to change your compile options you need to edit the file 'Makefile' in the directory from which you run 'make'. Inside that file look for one of the following things:

  1. The variable which defines you compiler, probably something like:

    CC='gcc'

  2. The actual line where your compiler gets called (more likely in hand-made Makefiles).

  3. Variables called CFLAGS or CXXFLAGS

In the first two cases, just add '-ggdb' after 'gcc', in the third case it's even easier just add '-ggdb' like:

CFLAGS='-ggdb'
like image 143
dtyler Avatar answered Oct 06 '22 02:10

dtyler


The makefiles I have to deal with (created by others) frequently don't make it easy to change the options to the compiler. Simply setting CFLAGS on the command line is easy but clobbers many other important compilation options. However, you can often deal with the issues by overriding the compiler macro on the make command line:

make CC="gcc -g" ...other arguments...

You need to ensure everything you're interested in debugging is compiled with the debug flag. You might use make cleanup or make clean to clear the debris, or you might resort to simpler measures (rm *.o *.a *.so or its equivalent). Or, if you have GNU Make, then use -B or --always-make to force it to rebuild everything.

If you have multi-directory builds, you need to do this in all the relevant directories.

like image 32
Jonathan Leffler Avatar answered Oct 06 '22 04:10

Jonathan Leffler