链接时调试符号丢失了吗?

Pet*_*gan 16 debugging macos gcc valgrind

我正在尝试使用调试符号编译程序,以便valgrind将为我提供行号.我发现如果我一次编译一个简单的测试程序(使用-g),那么它包含符号.但是,如果我在两次传递中编译(即编译然后链接),那么它不包含调试符号.

这是单通案例的编译命令:

g++ -g file.c -o file
Run Code Online (Sandbox Code Playgroud)

两次通过

g++ -g -c file.c -o file.o
g++ -g file.o -o file
Run Code Online (Sandbox Code Playgroud)

实际程序看起来像这样,包含一个简单的无效写入

int main(){
    int* x = new int[10];
    x[10]=1;
Run Code Online (Sandbox Code Playgroud)

}

如果我用一次传递编译,那么valgrind给出以下内容(注意结尾处的行号)

==24114== 40 bytes in 1 blocks are definitely lost in loss record 2 of 9
==24114==    at 0xB823: malloc (vg_replace_malloc.c:266)
==24114==    by 0x5768D: operator new(unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==24114==    by 0x576DA: operator new[](unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==24114==    by 0x100000F09: main (file.c:3)
Run Code Online (Sandbox Code Playgroud)

而如果我在两遍中编译,我得到这个(没有行号):

==24135== 40 bytes in 1 blocks are definitely lost in loss record 2 of 9
==24135==    at 0xB823: malloc (vg_replace_malloc.c:266)
==24135==    by 0x5768D: operator new(unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==24135==    by 0x576DA: operator new[](unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==24135==    by 0x100000F09: main (in ./file)
Run Code Online (Sandbox Code Playgroud)

任何有关这方面的见解将非常感激.我在OS X 10.7.3上使用gcc版本4.2.1

Pet*_*gan 8

最后的评论 - 它确实是OS X特定的"功能",与OS X链接调试信息的方式有关.Valgrind帮助用户通过命令解决问题--dsymutil=yes.

你可以在这里阅读更多相关信息:http://valgrind.org/docs/manual/manual-core.html#manual-core.erropts

感谢Dave Goodell在valgrind用户论坛上向我发送了解决方案.


SDw*_*rfs 2

只是为了将此问题标记为“已回答”(因此其他人不会不必要地打开和阅读)。

=> 答案是“user1288111”对最初问题的评论。