C++ 的大小对什么有影响?目标文件取决于?

发布于 2024-12-02 22:27:39 字数 194 浏览 0 评论 0原文

每当我们编译一个c++文件时,就会生成一个obj文件。我想知道obj文件的大小取决于什么因素?

只是为了让我的问题更清楚, 例如,一个 C++ 文件包含一个类声明,该类有 1 个整型变量作为数据成员,并且还有一些成员函数。如果我编译这个文件,那么将会创建一些 X 大小的 obj 文件。现在假设我添加了更多的数据成员和成员函数,那么obj文件的大小会改变吗?

Whenever we compile a c++ file, an obj file is generated. I want to know that on what factors does the size of the obj file depend?

Just to make my question more clear,
For example a c++ file contains a class declaration and this class has 1 integer variable as data member and also has some member functions. If i compile this file then some obj file will be created of some X size. Now assume that i add more data members and member functions, then will the size of obj file change?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜雨飘雪 2024-12-09 22:27:39

这取决于一百万个不同的因素,并且完全取决于平台、编译器和设置。

目标文件必须包含具有外部链接的函数的函数体的所有程序集,以及具有外部链接的所有全局变量。具有内部链接的任何内容可能会或可能不会保证在目标文件中单独条目,因为这些内容可能已被优化并直接集成到其调用站点中。这在很大程度上取决于优化设置。

GCC 还有一个“链接时间优化”选项,该选项实质上将整个源代码的副本添加到目标文件中,并显着增加其大小。

调试符号还添加了大量额外数据。

对于您的 C++ 特定问题:类定义本身在程序集中并不真正可见。非内联成员函数只是更多需要编译的函数,而数据成员只是被视为与原始数据成员相同 - 如果您声明该类型的实例,它们将位于调用堆栈上,但它们不直接影响汇编代码......除非你用常量初始化东西;当然,常量确实会进入代码。

That depends on a million different factors and is entirely platform and compiler and settings dependent.

The object file has to contain all the assembly for function bodies for functions with external linkage, as well as all the global variables with external linkage. Anything with internal linkage may or may not warrant a separate entry in the object file, as those may have been optimized out and integrated directly into their call site. This depends heavily on the optimization settings.

GCC also has an option for "link time optimizations" which essentially adds a copy of the entire source code to the object file and increases its size dramatically.

Debug symbols also add a lot of extra data.

For your C++-specific question: A class definition itself isn't really visible in the assembly. Non-inlined member functions are just more functions that have to be compiled, while data members are just treated the same as primitive data members - they'll be on the call stack if you declare instances of that type, but they don't directly impact the assembly code ... unless you're initializing things with constants; constants do go into the code, of course.

別甾虛僞 2024-12-09 22:27:39

目标文件包含(除其他外)源代码编译之后、链接之前的机器代码。因此,目标文件的大小基本上取决于代码的复杂性。

大多数编译器都提供在编译期间添加调试符号的选项,以使调试更容易,但会增加目标文件的大小。在 GCC 中,您可以使用 -g 选项添加调试符号,并使用 -s 选项删除它们。 Visual Studio 有类似的东西。

The object file contains (among others) the machine code after your source has been compiled, but before it is linked. The size of the object file therefore basically depends on the complexity of the code.

Most compilers gives you the option of adding debugging symbols during compilation to make debugging easier, but will increase the size of your object file. You can add the debug symbols with the -g option and strip them with the -s option in GCC. Visual Studio has something similar.

蒲公英的约定 2024-12-09 22:27:39

它严重依赖于编译器,但是。

目标文件将包含可执行代码、链接存根和元数据。元数据可以是任何依赖于编译器的东西,所以这是不可能的——任何事情都可能发生,具体取决于编译器的想法。链接存根是为具有外部链接的所有实体以及对其他对象文件中的实体的所有传出引用创建的 - 这不会受到将成员添加到同一对象文件中的类的直接影响。

现在开始编码。当你添加一个成员变量时,它必须被构造和销毁——这将需要一个构造函数和一个析构函数,这两个函数可能很简单,也可能很简单。如果它们不是微不足道的,这将导致代码大小略有增加,因为现在必须做一些额外的事情来处理这些成员变量,并且需要额外的代码。

It is heavily compiler dependent, but.

The object file will contain executable code, linkage stubs and metadata. Metadata can be whatever compiler dependent stuff, so it's out of the question - anything can happen to it depending on what the compiler thinks. Linkage stubs are created for all entities with external linkage and and for all outgoing references to entities in other object files - that isn't directly affected by adding members into the class in the same object file.

Now to code. When you add a member variable it has to be constructed and destroyed - that will require a constructor and a destructor both of which might be trivial or not. If they are not trivial this will lead to slight growth in code size since now something extra must be done to deal with those member variables and that requires extra code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文