头文件中的代码会增加二进制大小吗?
考虑一下:
class Foo{
void func1(){
/*func1 code*/
}
void func2(){
/*func2 code*/
}
};
案例 1:Foo.h 中的 Foo 类
案例 2:Foo 类在 Foo.h 和 Foo.cpp 之间很好地分离
其他各种 cpp 文件包括 Foo.h
我的问题是...案例 1 会导致更大的二进制文件吗?
Consider this:
class Foo{
void func1(){
/*func1 code*/
}
void func2(){
/*func2 code*/
}
};
Case 1: class Foo in Foo.h
Case 2: class Foo nicely seperated among Foo.h and Foo.cpp
Various other cpp files include Foo.h
My question is...Will Case 1 lead to a bigger binary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许会,也许不会。它确实与头文件无关。这里重要的是你的成员函数是在类定义中定义的。当成员函数这样定义时,它们被视为内联函数。如果编译器决定不实际内联对这些函数的任何调用,则不会对代码大小产生任何影响。如果编译器决定内联任何(或所有)调用,答案将是“这取决于”。对小函数的内联调用可能会导致代码大小增加或减少。这一切都取决于函数本身和编译器的能力(特别是优化能力)。
Maybe it will, maybe it won't. It really has nothing to do with header files. What matters here is that your member functions are defined in the class definition. When member functions are defined like that, they are treated as inline functions. If the compiler decides not to actually inline any calls to these functions, there won't be any impact on code size. If the compiler decides to inline any (or all) of the calls, the answer would be "it depends". Inlining calls to small functions might result in increased code size as well as in decreased code size. This all depends on the function itself and on the compiler's capabilities (optimization capabilities specifically).
如果编译器决定不内联这些函数,并为它们生成单独的主体,这些主体将出现在使用它们的每个目标文件中,但带有链接器的特殊标志 - “弱符号”。当链接器找到此标志时,它会将具有该名称的所有符号组合成一个结果符号(如果这些符号的主体或大小不同,可能会产生错误消息)
此外,RTTI 信息和 vtable 也使用相同的场景。
对于动态库,如果它们使用相同的类,则可能会在运行时发生弱符号连接。
If compiler decides not to inline those functions, and generate separate body for them, these bodies will appear in each object file who uses them, but with special flag for linker - 'weak symbol'. When linker finds this flag, it will combine all symbols with that name into only one resulting symbol (maybe it will produce error message if bodies or sizes of such symbols are different)
Also RTTI info and vtables also use same scenario.
With dynamic libraries, weak symbol joining may happen at run-time, if they uses the same class.
如果头文件中的函数被声明为静态,那么包含该头文件的每个模块(源文件)都会在目标文件中存储该函数的副本,并且最终的可执行文件会更大尺寸...
If the functions in the header are declared as static, than yes, each module (source file) that includes that header file will store a copy of that function in the object file and the final executable will be bigger in size...
如果标头中有代码定义,则每当您包含 .h 时,编译器可能会创建每个函数的冗余副本。这些冗余副本也可能会触发链接器的错误,因此除了内联函数之外,这种做法通常不受欢迎。
If you have the code definition in the header, the compiler might create redundant copies of each function whenever you include the .h. Those redundant copies might also trigger errors from the linker, so the practice is generally frowned upon except for inline functions.
如果函数代码内联包含在标头中,则编译器可以使用它来定义每个单独源文件的目标代码中的函数,或者将函数代码直接嵌入到调用函数的位置。根据您的编译器和链接器以及对 C++ 的支持,这可能会给您留下比全部单独定义的函数更大的代码。如果内联函数足够小,您可以通过避免函数调用开销来节省空间。然而,这样的函数必须非常小。
If the code for functions is included inline in the headers, then the compiler can use that to define the functions in the object code for each separate source file, or embed the function code directly where the functions are called. Depending on your compiler and linker and the support for C++ generally, that may leave you with larger code than you would have with the functions all defined separately. If the inline functions are small enough, you may save space by avoiding function call overhead. However, such functions have to be very small.