是什么-g选项,GCC做 [英] What does -g option do in gcc

查看:130
本文介绍了是什么-g选项,GCC做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到GDB很多教程要求使用-g选项在编译C程序。我不明白是什么-g选项实际上做。


解决方案

这让编译器添加调试信息生成的二进制代码。此信息允许调试器的说明在code。与源$ C ​​$ C文件和行号相关联。有调试符号,使某些类型的调试(如通过code步进)容易得多,如果没有可能的。

-g 选项实际上有几个可调参数,检查手册。另外,如果你不优化code,所以用它最有用 -O0 -oG (在新版本中) - 优化打破指令和源$ C ​​$ C之间的连接。 (更重要的是你要的的距离的函数调用,这是一个普遍使用的优化,但基本上完全废墟走路调用堆栈的能力省略帧指针。)

自己编写的一种标准化的语言调试符号(我认为这是DWARF2),还有用于读取库。一个程序甚至可以在运行时读取自己的调试符号,例如。

调试符号(以及其他类型的像函数名的符号),可以从二进制后来与命令删除。但是,因为你通常会结合调试符号与unoptimizied建立,有在这没有多大意义 - 相反,你会建立与不同的优化释放二进制的不从一开始的符号。

其他的编译器,如MSVC不包括在二进制本身的调试信息,而是将其存储在一个单独的文件和/或符号服务器 - 所以,如果家庭用户的应用程序崩溃,你会得到核心转储,你可以拉起来,从你的服务器的符号,并得到一个可读的堆栈跟踪。 GCC可能会添加类似,在未来的一个特征;我见过一些讨论一下吧。

I see many tutorials on gdb asking to use -g option while compiling c program. I fail to understand what does the -g option actually do.

解决方案

It makes the compiler add debug information to the resulting binaries. This information allows a debugger to associate the instructions in the code with source code files and line numbers. Having debug symbols makes certain kinds of debugging (like stepping through code) much easier, if not possible at all.

The -g option actually has a few tunable parameters, check the manual. Also, it's most useful if you don't optimize the code, so use -O0 or -Og (in newer versions) - optimizations break the connection between instructions and source code. (Most importantly you have to not omit frame pointers from function calls, which is a popular optimization but basically completely ruins the ability to walk up the call stack.)

The debug symbols themselves are written in a standardized language (I think it's DWARF2), and there are libraries for reading that. A program could even read its own debug symbols at runtime, for instance.

Debug symbols (as well as other kinds of symbols like function names) can be removed from a binary later on with the strip command. However, since you'll usually combine debug symbols with unoptimizied builds, there's not much point in that - rather, you'd build a release binary with different optimizations and without symbols from the start.

Other compilers such as MSVC don't include debug information in the binary itself, but rather store it in a separate file and/or a "symbol server" -- so if the home user's application crashes and you get the core dump, you can pull up the symbols from your server and get a readable stack trace. GCC might add a feature like that in the future; I've seen some discussions about it.

这篇关于是什么-g选项,GCC做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆