为什么g ++在编译的二进制文件中存储类名? [英] Why does g++ store class names in the compiled binary?

查看:129
本文介绍了为什么g ++在编译的二进制文件中存储类名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到如果我在我的程序中运行 strings ,它是由 g ++ 编译的,输出包含它使用的各种类。

该程序是用 -O3 编译的,没有 -g -p ,并且当我去掉二进制文件时类名仍然存在。



我想知道为什么需要 g ++ 将这些信息存储在二进制文件中?目前所有的类名似乎都是使用虚拟函数的类,所以我怀疑这是与它有关的。 解决方案

这可能与 RTTI 有关,具体来说,RTTI允许您查询给定变量的类的名称。请参阅 typeid 关键字。如果是这种情况,那么它就可以解释为什么它只发生在具有虚函数的类中 - RTTI仅适用于具有虚函数的类。


I noticed that If I run strings on my program which was compiled by g++ the output contains the names of various classes that it uses.

The program was compiled with -O3 and without -g or -p, and the class names are still present when I strip the binary.

I was wondering why it is necessary for g++ to store this information in the binary? The class names that are present all seem to be classes that use virtual functions, so I suspect this is something to do with it.

解决方案

This might have something to do with RTTI, specifically, RTTI allows you to query the name of the class of a given variable. See the typeid keyword. If this is the case then it would explain why it happens only with classes which have virtual functions - RTTI works only for classes with virtual functions.

这篇关于为什么g ++在编译的二进制文件中存储类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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