使用 gdb 进行调试 - 最佳实践

2022-01-20 00:00:00 makefile gdb c++

我是 GDB 的初学者,我让它正常工作.但是,我想知道如何在大型项目中使用它.我有一个项目,其中使用 makefile 和 g++ 完成构建.为了让 GDB 工作,我们需要使用调试符号进行编译,对吧(g++ -g 文件)?

I am a beginner in GDB and I got it working correctly. However, I am wondering how this is used in big projects. I have a project where build is done using makefile and g++. For GDB to work, we need to compile with debug symbols on, right (g++ -g files)?

问题

  1. 我是否需要在 makefile 中创建一个类似debug"的新目标,以便我可以像 make debug 这样进行调试构建.这是最佳做法吗?
  2. 假设,我只需要调试 foo.cpp,除了构建包括 main 在内的整个程序之外,是否可以只为它生成调试符号?
  1. Do I need to create a new target in makefile something like "debug", so that I can make a debug build like make debug. Is this the best practice?
  2. Suppose, I need to debug only foo.cpp and is it possible to generate debug symbols only for that other than building whole program including main?

有什么想法吗?

推荐答案

  1. 不需要,尽管您可能要考虑始终使用 -g 构建(有时,您甚至可能需要尝试调试优化的(-O1、-O2 等)代码;为什么不保留 -g 呢?对于发布,您始终可以在二进制文件上运行 strip.

  1. Not needed, although you may want to consider always building with -g (sometimes, you may even need to try and debug optimized (-O1, -O2, etc) code; why not leave -g on? For releases, you can always just run strip on the binary.

是的.使用 -g 仅构建该文件.

Yes. Build just that file with -g .

相关文章