C++ 即使使用 -g 标志编译,gdb 中也没有调试符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4568461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:46:26  来源:igfitidea点击:

No debugging symbols in gdb even when compiling with -g flag

c++gccmakefilegdbdebug-symbols

提问by delaccount992

I am trying to compile my program with debugging symbols for use in gdb. I have added the -g flag to my makefile but I still get "Reading symbols from ...(no debugging symbols found)" when I load the program in gdb. What is wrong??

我正在尝试使用调试符号编译我的程序以在 gdb 中使用。我已将 -g 标志添加到我的 makefile 中,但是当我在 gdb 中加载程序时,我仍然收到“从...读取符号(未找到调试符号)”。怎么了??

Here is a stripped down example of my makefile which should have the relevant bits:

这是我的 makefile 的一个精简示例,它应该具有相关位:

CPP = g++
CFLAGS = -c -g -Wall

$(BIN): $(OBJ)
 $(CPP) $(LDFLAGS) $(OBJ) -o $(BIN) $(LIBS)

<test.o>: <test.cpp>
 $(CPP) $(CFLAGS) <test.cpp> -o <test.o>

If you'd like to see the whole thing you can go here instead, though I don't thinkit's necessary:

如果你想看到整个事情,你可以走,而不是在这里,但我不认为这是必要的:

http://pastebin.com/vGNjy0ga

http://pastebin.com/vGNjy0ga

Miscellaneous notes.. I'm compiling with MinGW on Windows and I have SFML and OpenGL as dependencies.

其他说明.. 我正在 Windows 上使用 MinGW 进行编译,并且我将 SFML 和 OpenGL 作为依赖项。

And no, the -s flag is nowhere to be found in my makefile.

不,在我的 makefile 中找不到 -s 标志。

回答by delaccount992

Ahh. I'm very sorry. It turns out the "clean:" portion of my makefile is broken. Thus when I used make clean nothing happened. Deleting the .o files manually fixed the problem. The flags work perfectly now. Thanks to everyone who posted anyway! This can be deleted now.

啊。我非常抱歉。事实证明,我的 makefile 的“干净:”部分已损坏。因此,当我使用 make clean 时什么也没发生。手动删除 .o 文件修复了该问题。标志现在完美地工作。感谢所有发帖的人!现在可以删除了。

回答by Peter Mitrano

I had the same issue when using a makefile I inherited on some old F77 code. I tried all the flags people recommend (-g -ggdb ...etc.) the solution was run make cleanIf you don't have that or know what it means, basically delete all the compiled (.o) files.

使用我在一些旧的 F77 代码上继承的 makefile 时,我遇到了同样的问题。我尝试了人们推荐的所有标志(-g -ggdb ...等),解决方案是运行make clean如果你没有那个或知道它意味着什么,基本上删除所有编译的 (.o) 文件。

the makefile didn't know to recompile since only flags were changed, so I wasn't actually compiling with -g or -ggdb when I thought it was. Hope this helps someone!

makefile 不知道要重新编译,因为只更改了标志,所以当我认为是时,我实际上并没有使用 -g 或 -ggdb 进行编译。希望这可以帮助某人!

回答by ppan

I think you need -gwhen linking the object into a binary code.

我认为-g将对象链接到二进制代码时需要。

CPP = g++
CFLAGS = -g -Wall

$(BIN): $(OBJ)
 $(CPP) $(CFLAGS) $(OBJ) -o $(BIN) $(LDFLAGS) $(LIBS)

<test.o>: <test.cpp>
 $(CPP) $(CFLAGS) -c <test.cpp> -o <test.o>

回答by Alam

try to replace

尝试更换

$(BIN): $(OBJ)
 $(CPP) $(LDFLAGS) $(OBJ) -o $(BIN) $(LIBS)

with

$(BIN): $(OBJ)
 $(CPP) $(CFLAGS) -o $(BIN) $(OBJ) $(LDFLAGS) $(LIBS)

(edit) Note: -c option will not work with executable

(编辑)注意:-c 选项不适用于可执行文件

回答by binW

I dont have much experience with Mingw but try replacing -g with -ggdb. This may solve your problem. According to gcc man page

我对 Mingw 没有太多经验,但尝试用 -ggdb 替换 -g。这可能会解决您的问题。根据 gcc 手册页

Produce debugging information for use by GDB. This means to use the most expressive format available (DWARF 2, stabs, or the native format if neither of those are supported), including GDB extensions if at all possible.

生成供 GDB 使用的调试信息。这意味着使用最具表现力的可用格​​式(DWARF 2、stabs 或本机格式,如果两者都不支持),如果可能,包括 GDB 扩展。

回答by binW

I am not sure, but I think you need -g even while linking.

我不确定,但我认为即使在链接时也需要 -g。