简体   繁体   English

在什么情况下必须编译cpp代码并将其链接到exe中

[英]Under what conditions must cpp code be compiled and linked into the exe

I am using MSVC++ with visual studio 2012, to build basic unmanaged c++ x86/x64 Windows and Console subsystem programs. 我在Visual Studio 2012中使用MSVC ++,以构建基本的非托管c ++ x86 / x64 Windows和控制台子系统程序。 (exception handling turned off) (异常处理已关闭)

I have a rather large project and when curious about the final exe size I decided to see how big it would be if I changed my main() function to just do nothing (so not calling or referencing any of the other code. I noticed that the exe size was still a lot bigger than when I went and removed all the cpp files from the project. 我有一个相当大的项目,当对最终的exe大小感到好奇时,我决定看看如果将main()函数更改为不执行任何操作(这样就不会调用或引用其他任何代码),它将有多大。 exe的大小仍然比我从项目中删除所有cpp文件时大得多。

So I was wondering what types of C++ code would cause code to be linked in even if the main function does nothing. 所以我想知道哪种C ++代码类型将导致代码链接,即使main函数什么也不做。 One example would be having a global variable in one of your cpp files of type A, where A was a class/struct. 一个示例是在类型A的cpp文件之一中有一个全局变量,其中A是类/结构。 In this case I would assume that A's constructor and destructor would have to be included, as well as the constructors and destructor's of the types of all of A's members. 在这种情况下,我假定必须包括A的构造函数和析构函数,以及所有A成员类型的构造函数和析构函数。

In visual studio I have all optimizations turned on including the linker ones to eliminate functions/data that is never referenced, and comdat folding. 在Visual Studio中,我启用了所有优化功能,包括链接器优化功能,以消除从未引用的功能/数据以及comdat折叠。 and the cpp code generation to enable comdats/function level linking. 和cpp代码生成以启用comdats /功能级别链接。

Update: I am not using any C++11 or above functionality. 更新:我没有使用任何C ++ 11或更高版本的功能。 Though I'm not sure if my build environment is set up to build for c++11 or above, or if this would make any difference. 尽管我不确定我的构建环境是否设置为针对c ++ 11或更高版本进行构建,或者这是否有区别。

Technically, a global doesn't need to be included just because it has a constructor. 从技术上讲,不必仅因为具有构造函数而将其包括在内。 C++ follows a graph model, where main acts as the root Translation Unit. C ++遵循图模型,其中main充当根翻译单元。 Function calls from one TU to another form the edges of the graph. 从一个TU到另一个TU的函数调用形成了图形的边缘。 A standalone TU might be skipped. 独立的TU可能会被跳过。

MSVC++ has another option, to use not the TU but individual functions as the nodes in the graph (/COMDAT). MSVC ++还有另一个选择,不是使用TU,而是使用各个功能作为图中的节点(/ COMDAT)。 In combination with /OPT:ICF this reduces the executable size even further. 与/ OPT:ICF结合使用,可以进一步减小可执行文件的大小。 Leaving out functions is a win by itself, but also can remove additional dependencies. 放弃功能本身就是一个胜利,但是也可以消除其他依赖性。

The downside of this model is that certain self-registration schemes are unportable, as the class doing the self-registration is not a dependency of anything and thus subject to removal. 该模型的缺点是某些自注册方案是不可移植的,因为进行自注册的类不依赖于任何东西,因此会被删除。

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

相关问题 将代码链接到exe时C ++程序崩溃,但是将代码编译到exe时工作正常,为什么呢? - C++ program crashes when code is linked to exe but works fine when code is compiled into exe, how come? CPP文件运行,但编译为exe时不起作用 - CPP file runs, but doesn't work when compiled to exe 当抛出异常的代码链接到使用-fno-exceptions编译的库时会发生什么? - What happens when code that throws exceptions is linked against a library compiled with -fno-exceptions? 为什么要编译并链接此代码而不会出现错误? - Why is this code compiled and linked without an error? 在VS2013下构建项目仅在手动编译每个* .cpp时有效 - Building project under VS2013 works only when each *.cpp's are compiled manually 如何从代码块中的.cpp文件创建.exe? - How do I create a .exe from a .cpp file in Code Blocks? 在什么条件下C ++头文件不能传递? - Under what conditions are C++ headers not transitive? 用VC10编译的C ++库(sp1)可以用VC11编译的代码链接吗? - Can C++ libraries compiled with VC10 (sp1) be linked by code compiled with VC11? 由于dyld,无法使用犰狳运行已编译的cpp代码:库未加载 - Can't run compiled cpp code with armadillo, because of dyld: Library not loaded 如果我在库和应用程序中编译了相同的cpp文件,该怎么办? - what happens if I have the same cpp file compiled within a library and the application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM