Note: This article is auto translated by Lingotek.
For large-scale software systems, the dependency control between individual functions is not a simple problem. In order to reduce the complexity of the program code in the management, the concept of importing module, the entire system into separate and close cooperation between the units, has become a very common design techniques.
In fact, for the time being, C + + does not have a real sense of "module system", although in the C++20 standard has already joined the module-related mechanism of the draft, but it is clear that UE4 used in the module mechanism is not the standard library of the implementation of the draft.
As an engine on the front end of the technology, many of the features will of course not wait until the standard library is stable to import. The module system implemented by UE4 does not depend on the implementation of the compiler, and it designs its own Cross-platform architecture based on the existing C + + standard library, unreal build Tool (UBT). This set of construction systems not only controls the timing of each module's loading, but also has many compiler optimization mechanisms.
In order to correctly understand the mode of operation of this Module system in UE4, first of all, we must first understand what is "module".
In the abstract concept, the so-called module is actually a collection of functions. In C + +, we can think of it as a bunch of translation unit (or compliation unit) generated by the collection of obj files; the collection of these. obj files is the module defined in UE4.
Next, UBT has the following choices based on the different compilation environments:
- Compiles the. obj file contained in the module into the final. exe file.
- Make the. obj file contained in the module a static Library (. lib).
- The. obj file included in the module is made into dynamic Library (. dll) and then dynamically loaded in the runtime period.
UE4 by default, if the compiled TargetType is editor, then Targetlinktype will be set to modular, meaning that all the module will be made (. dll), and if TargetType is a different type, The engine will take different actions depending on whether the module being compiled is Pluginmodule or gamemodule. The former will be made into (. Lib), which will directly compile the link into the final EXE file.
Why should editor be specially set to modular form? This is a specially designed design for UE4 to achieve "hotreload".The purpose of this feature is to allow us to see the results immediately after we modify the C + + program code in editor. Therefore, in order to implement dynamically loaded modified program logic, the module can only be compiled into dynamic library in editor mode.
Of course, we can also set linktype manually in target. cs to Targetlinktype. Modular, so that regardless of TargetType is editor, all the module will be compiled into dynamic library. Just, if you want to change this setting, you have to get the engine's source code to compile itself, or it will produce a compilation error.
All compilation behavior about individual module can be set in the build. cs file, such as the dependencies of this module with other module, relative path of its dependent project include file, various other compilation settings, the compiler to be passed in Definitions, or the module must be loaded with an externally compiled external library is also set up here.
Target. CS and build CS separately control the compilation behavior at different levels, target. CS Comparison of the high-level, the set of the compilation options will affect all the module required to compile, and determine the final form of the finished product (TARGETTYPE); and build. CS is the lower level, used to determine the compilation of individual module, with some compilation options will overwrite target. CS set content.
 See Wikipedia c++20, this chapter is written when UE4 has fully supported c++14
 The translation unit is a header file that is expanded by a CPP with all include, and finally produces an object file