#include是否影响程序大小? [英] Does #include affect program size?

查看:221
本文介绍了#include是否影响程序大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的cpp文件使用#include添加一些标题,我的最终程序的大小会变大吗?头部不被认为是编译单元,但头文件的内容由预处理器添加到实际的源文件中,因此输出文件(exe或dll)的大小将受此影响?



编辑:我忘了提及问题不是关于模板/内联函数。我的意思会发生什么,如果我放置一个#include到一个没有任何功能的实现细节的头。感谢

解决方案

您已澄清:


[头没有]模板/内联函数...没有任何函数的实现细节。


一般来说,添加头文件不会影响程序大小。

你可以测试这个。采取已经构建的程序,并检查可执行文件大小。然后进入每个.cpp文件,并包括一个标准的C或C + +头文件,在该文件中实际不需要。生成程序并再次检查可执行文件大小 - 它应该和以前一样大小。



总的来说,影响可执行文件大小的只有那些导致编译器生成不同数量的代码,全局/静态变量初始化或DLL /共享库用法。即使如此,如果有任何这样的项目不需要程序操作,大多数现代的链接器会抛出这些东西。



所以包括头文件,只包含东西像函数原型,没有内联的类/结构定义和枚举的定义不应该改变任何东西。



但是,肯定有例外。这里有一些。



一个是如果你有一个不复杂的链接器。然后,如果添加一个生成程序实际上不需要的头文件,而链接器不会抛出它们,可执行文件的大小会膨胀。 (有些人故意用这种方式构建链接器,因为链接时间可能变得疯狂。)



很多时候,添加或更改预处理器符号定义的头文件会更改编译器生成的内容。例如,assert.h(或cassert)定义了assert()宏。如果在.c / .cpp文件中包含一个头文件来改变NDEBUG预处理器符号的定义,它将改变assert()用法是否生成任何代码,从而改变可执行文件的大小。



此外,添加更改编译器选项的头文件将更改可执行文件大小。例如,许多编译器允许您通过类似于 #pragma pack 的行来更改结构的默认打包。因此,如果您添加一个头文件来改变.c / .cpp文件中的结构打包,编译器会生成不同的代码来处理结构体,从而改变可执行文件的大小。



正如有人指出,当你处理Visual C ++ / Visual Studio时,所有的注单都关闭了。我们应该说,微软有一个独特的视角围绕他们的开发工具,不是由其他平台上编写编译器系统的人共享。


When my cpp file uses #include to add some header, does my final program's size gets bigger? Header aren't considered as compilation units, but the content of the header file is added to the actual source file by the preprocessor, so will the size of the output file (either exe or dll) will be affected by this?

Edit: I forgot to mention that the question is not about templates/inline functions. I meant what will happen if I place an #include to a header that doesn't have any implementation detail of functions. Thanks

解决方案

You clarified that:

[The header has no] templates/inline functions... doesn't have any implementation detail of functions.

Generally speaking, no, adding a header file won't affect program size.

You could test this. Take a program that already builds, and check the executable size. Then go into each .cpp file and include a standard C or C++ header file that isn't actually needed in that file. Build the program and check the executable size again - it should be the same size as before.

By and large, the only things that affect executable size are those that cause the compiler to either generate different amounts of code, global/static variable initializations, or DLLs/shared library usages. And even then, if any such items aren't needed for the program to operate, most modern linkers will toss those things out.

So including header files that only contain things like function prototypes, class/struct definitions without inlines, and definitions of enums shouldn't change anything.

However, there are certainly exceptions. Here are a few.

One is if you have an unsophisticated linker. Then, if you add a header file that generates things the program doesn't actually need, and the linker doesn't toss them out, the executable size will bloat. (Some people deliberately build linkers this way because the link time can become insanely fast.)

Many times, adding a header file that adds or changes a preprocessor symbol definition will change what the compiler generates. For instance, assert.h (or cassert) defines the assert() macro. If you include a header file in a .c/.cpp file that changes the definition of the NDEBUG preprocessor symbol, it will change whether assert() usages generate any code, and thus change the executable size.

Also, adding a header file that changes compiler options will change the executable size. For instance, many compilers let you change the default "packing" of structs via something like a #pragma pack line. So if you add a header file that changes structure packing in a .c/.cpp file, the compiler will generate different code for dealing with structs, and hence change the executable size.

And as someone else pointed out, when you're dealing with Visual C++/Visual Studio, all bets are off. Microsoft has, shall we say, a unique perspective around their development tools which is not shared by people writing compiler systems on other platforms.

这篇关于#include是否影响程序大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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