简体   繁体   English

为什么用C ++为“ Hello World”程序生成的代码比用C语言生成的代码大10倍?

[英]Why is the code generated for “Hello World” program 10 times larger for C++ than for C?

This in is the FAQ I recently read on Dr. Bjarne Stroustrup's web site . 这是我最近在Bjarne Stroustrup博士的网站上阅读的FAQ。 I also tested a simple "Hello World" program in C and then in C++, and surprisingly the size of a C++ "Hello World" program executable file is 1357 KB (1.32 MB) whereas the size of the executable file in C is only 122 KB. 我还先在C中然后在C ++中测试了一个简单的“ Hello World”程序,令人惊讶的是,C ++“ Hello World”程序可执行文件的大小为1357 KB(1.32 MB),而C中的可执行文件的大小仅为122 KB Why there is such a huge difference in the size between the C "Hello World" Program and the C++ "Hello World" Program? 为什么C“ Hello World”程序和C ++“ Hello World”程序在大小上有如此大的差异?

http://stroustrup.com/bs_faq.html#Hello-world http://stroustrup.com/bs_faq.html#Hello-world

I am using Orwell Dev-C++ compiler. 我正在使用Orwell Dev-C ++编译器。 I used option "Strip executable" & recompile it. 我使用选项“ Strip可执行文件”并重新编译它。 After doing this size reduces to 12 KB in C and 536 KB in C++. 完成此操作后,大小在C中减小为12 KB,在C ++中减小为536 KB。

Here is a comparison of binary filesize of c and c++ hello world programs when compiled with gcc: http://coliru.stacked-crooked.com/a/46126d97d45fba1a 这是使用gcc编译时c和c ++ hello world程序的二进制文件大小的比较: http : //coliru.stacked-crooked.com/a/46126d97d45fba1a

The size difference between c and c++ on that platform is 240 bytes for both stripped and non-stripped executable which is quite insignificant in my opinion. 在该平台上,对于带剥离的可执行文件和非带剥离的可执行文件,c和c ++之间的大小差为240字节,这在我看来是微不足道的。

As Stroustrup writes on the page that you've linked: 正如Stroustrup在您链接的页面上写道:

It is all an issue on how an implementor organizes the standard libraries (eg static linking vs. dynamic linking, locale support by default vs. locale support enabled through and option, etc.). 关于实现者如何组织标准库的所有问题(例如,静态链接与动态链接,默认情况下的语言环境支持与通过和选项启用的语言环境支持等)。 If one version is significantly larger than the other, report the problem to the implementor of the larger. 如果一个版本比另一个版本大得多,请向更大版本的实施者报告问题。

Stroustrup doesn't specify how much should be considered "significant", but if your compiler produces 10x bigger binary for an identical hello world program, a bug report may be appropriate. Stroustrup没有指定应视为“重要”的数量,但是如果您的编译器为相同的hello world程序生成的二进制文件大10倍,则可能需要报告错误。

It depends on your compiler, and what environment you use. 这取决于您的编译器以及所使用的环境。 If you link statically then you will be including the standard library in your executable, while typically this is done dynamically since the standard library can vary on different platforms (though I suppose you'd compile separately for those anyway). 如果您进行静态链接,那么您将在可执行文件中包含标准库,而通常这是动态完成的,因为标准库在不同平台上可能会有所不同(尽管我认为您还是会针对这些平台分别进行编译)。 Also, it does bloat your executable's file size, as you have seen here. 而且,它确实会膨胀可执行文件的文件大小,如您在此处所看到的。

Try linking dynamically to the standard library, you may have to copy the library binaries into the same directory with your executable. 尝试动态链接到标准库,您可能必须将库二进制文件与可执行文件复制到同一目录中。 The file size will probably be very similar after this. 此后,文件大小可能非常相似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM