如何调试用'make'编译的程序?

时间:2023-02-08 22:54:26

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

gdb的教程建议使用'gcc -g'进行编译,以使用调试符号编译程序。

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

但是,我想调试用make编译的程序。如何指示make使用调试符号进行编译?

Thanks.

2 个解决方案

#1


7  

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:

要更改编译选项,您需要在运行'make'的目录中编辑文件'Makefile'。在该文件中查找以下内容之一:

  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).

    调用编译器的实际行(更可能在手工制作的Makefile中)。

  3. Variables called CFLAGS or CXXFLAGS

    变量称为CFLAGS或CXXFLAGS

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

在前两种情况下,只需在'gcc'之后添加'-ggdb',在第三种情况下,更容易添加'-ggdb',如:

CFLAGS='-ggdb'

#2


3  

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:

我必须处理的makefile(由其他人创建)经常不容易将选项更改为编译器。简单地在命令行上设置CFLAGS很容易,但是很多其他重要的编译选项。但是,您通常可以通过在make命令行上覆盖编译器宏来处理这些问题:

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.

您需要确保使用调试标志编译您对调试感兴趣的所有内容。您可以使用make cleanup或make clean来清除碎片,或者您可以采用更简单的措施(rm * .o * .a * .so或其等效物)。或者,如果你有GNU Make,那么使用-B或--always-make来强制它重建所有东西。

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

如果您有多目录构建,则需要在所有相关目录中执行此操作。

#1


7  

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:

要更改编译选项,您需要在运行'make'的目录中编辑文件'Makefile'。在该文件中查找以下内容之一:

  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).

    调用编译器的实际行(更可能在手工制作的Makefile中)。

  3. Variables called CFLAGS or CXXFLAGS

    变量称为CFLAGS或CXXFLAGS

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

在前两种情况下,只需在'gcc'之后添加'-ggdb',在第三种情况下,更容易添加'-ggdb',如:

CFLAGS='-ggdb'

#2


3  

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:

我必须处理的makefile(由其他人创建)经常不容易将选项更改为编译器。简单地在命令行上设置CFLAGS很容易,但是很多其他重要的编译选项。但是,您通常可以通过在make命令行上覆盖编译器宏来处理这些问题:

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.

您需要确保使用调试标志编译您对调试感兴趣的所有内容。您可以使用make cleanup或make clean来清除碎片,或者您可以采用更简单的措施(rm * .o * .a * .so或其等效物)。或者,如果你有GNU Make,那么使用-B或--always-make来强制它重建所有东西。

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

如果您有多目录构建,则需要在所有相关目录中执行此操作。