Anders Feder
Anders Feder

Reputation: 645

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.

Upvotes: 8

Views: 7519

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 754820

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.

Upvotes: 5

dtyler
dtyler

Reputation: 1207

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'

Upvotes: 9

Related Questions