Jump to content

Single compilation unit: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Drewnoakes (talk | contribs)
Improved confusing opening sentence in 'Purpose' section. Renamed 'Usage' as 'Example'.
Drewnoakes (talk | contribs)
Formatting of source code and code excerpts in text
Line 3: Line 3:


==Purpose==
==Purpose==
Usually a C/C++ [[Integrated development environment|development environment]] assumes that .c/.cpp source files are preprocessed into [[Translation unit (programming)|translation units]] which are then translated (compiled) separately by the compiler into multiple object (.o or .obj) files. These object files can then be [[Linker (computing)|linked]] together to create a single executable file or library. However, this leads to multiple passes being performed on common header files, and with C++, multiple template instantiations of the same [[Template (programming)|templates]] in different translation units.
Usually a C/C++ [[Integrated development environment|development environment]] assumes that <tt>.c</tt>/<tt>.cpp</tt> source files are preprocessed into [[Translation unit (programming)|translation units]] which are then translated (compiled) separately by the compiler into multiple object (<tt>.o</tt> or <tt>.obj</tt>) files. These object files can then be [[Linker (computing)|linked]] together to create a single executable file or library. However, this leads to multiple passes being performed on common header files, and with C++, multiple template instantiations of the same [[Template (programming)|templates]] in different translation units.


The ''Single Compilation Unit'' technique uses pre-processor directives to "glue" different translation units together at compile-time rather than at link-time. This reduces the overall build time, but increases the build time required after making minor changes to any source file that is included in the Single Compilation Unit. Therefore, this technique is appropriate for modules which consist of infrequently modified source files.
The ''Single Compilation Unit'' technique uses pre-processor directives to "glue" different translation units together at compile-time rather than at link-time. This reduces the overall build time, but increases the build time required after making minor changes to any source file that is included in the Single Compilation Unit. Therefore, this technique is appropriate for modules which consist of infrequently modified source files.
Line 10: Line 10:


==Example==
==Example==
For example, if you have the source files foo.cpp and bar.cpp, they can be placed in a Single Compilation Unit as follows:
For example, if you have the source files <tt>foo.cpp</tt> and <tt>bar.cpp</tt>, they can be placed in a Single Compilation Unit as follows:

<pre>
<syntaxhighlight lang="cpp">
#include "foo.cpp"
#include "foo.cpp"
#include "bar.cpp"
#include "bar.cpp"
</syntaxhighlight>
</pre>

Suppose foo.cpp and bar.cpp are:
Suppose <tt>foo.cpp</tt> and <tt>bar.cpp</tt> are:
<pre>

<syntaxhighlight lang="cpp">
//foo.cpp
//foo.cpp
#include <iostream> // A large, standard header
#include <iostream> // A large, standard header
Line 25: Line 28:
bar();
bar();
}
}
</syntaxhighlight>
</pre>

<pre>
<syntaxhighlight lang="cpp">
//bar.cpp
//bar.cpp
#include <iostream> // The same large, standard header
#include <iostream> // The same large, standard header
Line 34: Line 38:
...
...
}
}
</syntaxhighlight>
</pre>


Now the standard header file ('iostream') is compiled only once, and function 'bar' may be inlined into function 'main', despite being from another module.
Now the standard header file (<tt>iostream</tt>) is compiled only once, and function <tt>bar</tt> may be inlined into function <tt>main</tt>, despite being from another module.


==See also==
==See also==

Revision as of 12:37, 10 March 2013

Single Compilation Unit (SCU) is a technique of computer programming for the C/C++ languages, which reduces compilation time and aids the compiler to perform program optimization even when the 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 are preprocessed into translation units which are then translated (compiled) separately by the compiler into multiple object (.o or .obj) files. These object files can then be linked together to create a single executable file or library. However, 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 rather than at link-time. This reduces the overall build time, but increases the build time required after making minor changes to any source file that is included in the Single Compilation Unit. Therefore, this technique is appropriate for 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, and 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 [clarification needed].

Example

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.h"    // 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