Single Compilation Unit
This article does not cite any references or sources. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. (December 2006) |
The introduction to this article provides insufficient context for those unfamiliar with the subject. Please help improve the article with a good introductory style. (October 2009) |
Single Compilation Unit is a C/C++ technique which reduces compilation time and aids the compiler to perform program optimization even when compiler itself is lacking support for whole program optimization or precompiled headers.
Purpose
Usually a C/C++ development environment assumes that .c/.cpp source files (translation units) are preprocessed and translated separately by the compiler to object (.o or .obj) files, which can then be linked together to create an executable file or library. This leads to multiple passes being performed on common header files and, with C++, multiple template instantiations of the same templates in different translation units. The Single Compilation Unit technique uses pre-processor directives to "glue" different translation units together at compile-time, not link-time. This reduces the overall build time, but increases the build time required after making minor changes to any source file included in a Single Compilation Unit. So this technique is appropriate for some modules which consist of infrequently modified source files.
SCU also allows an optimizer to trace deeper relations between functions, therefore allowing optimizations such as inlining. It also helps avoiding implicit code bloat due to exceptions, side effects, and register allocation, which are generally overlooked by the classic scheme of using separate modules, and not always achieved by the use of precompiled headers.
Usage
For example, if you have the source files foo.cpp and bar.cpp, they can be placed in a Single Compilation Unit as follows:
#include "foo.cpp" #include "bar.cpp"
Suppose foo.cpp and bar.cpp are:
//foo.cpp #include <iostream> // A large, standard header #include "bar.hpp" // Declaration of function 'bar' int main() // Definition of function 'main' { bar(); }
//bar.cpp #include <iostream> // The same large, standard header void bar() // Definition of function 'bar' { ... }
Now the standard header file ('iostream') is compiled only once, and function 'bar' may be inlined into function 'main', despite being from another module.
See also
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...