c++ - 为什么 C++ 为 "Hello World"程序生成的代码比 C 代码大 10 倍?

标签 c++ c

这是我最近在 Dr. Bjarne Stroustrup's web site 上读到的常见问题解答。我还用 C 和 C++ 测试了一个简单的“Hello World”程序,令人惊讶的是,C++“Hello World”程序可执行文件的大小为 1357 KB (1.32 MB),而 C 中可执行文件的大小仅为 122知识库。为什么C“Hello World”程序和C++“Hello World”程序的大小有如此巨大的差异?

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

我正在使用 Orwell Dev-C++ 编译器。我使用了选项“剥离可执行文件”并重新编译它。执行此操作后,C 中的大小减少到 12 KB,C++ 中减少到 536 KB。

最佳答案

以下是使用 gcc 编译时 c 和 c++ hello world 程序的二进制文件大小的比较:http://coliru.stacked-crooked.com/a/46126d97d45fba1a

对于剥离和非剥离可执行文件,该平台上的 c 和 c++ 之间的大小差异都是 240 字节,在我看来,这非常微不足道。

正如 Stroustrup 在您链接的页面上所写:

It is all an issue on how an implementor organizes the standard libraries (e.g. 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 没有指定多少应该被视为“重要”,但如果您的编译器为相同的 hello world 程序生成 10 倍大的二进制文件,则错误报告可能是合适的。

关于c++ - 为什么 C++ 为 "Hello World"程序生成的代码比 C 代码大 10 倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26097574/

相关文章:

c++ - CMakeLists.txt 的 Apache Thrift 错误

c++ - 实现通用 map [来自 HW]

c++ - 如果应用程序以管理员身份运行,则奇怪的文件权限

c - fprintf(fp, "%c",10) 未按预期运行

C++11 线程与异步性能(VS2013)

c++ - Qt按钮没有出现在主窗口中

c - 将 Python/Matlab 移植到 C 和定点 DSP 处理器——C 也应该是定点的吗?

c++ - 理解函数指针

c - 在C中获取文件扩展名

c++ - "C-style array"是什么意思,它与 std::array (C++ 风格)有何不同?