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.
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:
The variable which defines you compiler, probably something like:
CC='gcc'
The actual line where your compiler gets called (more likely in hand-made Makefiles).
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'
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.
精彩评论