ArticleslgStudy

science

Single compilation unit

Single compilation unit is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Single compilation unit rather than just read about it. In short: Single compilation unit (SCU) is a computer programming technique for the C and C++ languages, which reduces compilation time for programs spanning multiple files. Specifically, it allows the compiler to keep data from shared header files, definitions and templates, so that it need not recreate them for each file.

Key takeaways

  • Single compilation unit belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Single compilation unit to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Single compilation unit from memory before moving on to harder problems.

Reference excerpt

Single compilation unit (SCU) is a computer programming technique for the C and C++ languages, which reduces compilation time for programs spanning multiple files. Specifically, it allows the compiler to keep data from shared header files, definitions and templates, so that it need not recreate them for each file. It is an instance of program optimization, and can be seen as a form of implementing modular programming. The technique can be applied to an entire program or to some subset of source files; when applied to an entire program, it is also known as a unity build.

Purpose In the C/C++ compilation model (formally "translation environment"), individual .c/.cpp source files are preprocessed into translation units, which are then 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, due to eliminating the duplication, but increases the incremental build time (the time required after making a change to any single source file that is included in the SCU), due to requiring a full rebuild of the entire unit if any single input file changes. Therefore, this technique is appropriate for a set of infrequently modified source files with significant overlap (many or expensive common headers or templates), or source files that frequently require recompilation together, such as due to all including a common header or template that changes frequently. It is similar to using precompiled headers, which are ideal for headers which change very infrequently if ever, or C++ modules (which were added in C++20). Another disadvantage of SCU is that it is serial, compiling all included source files in sequence in one process, and thus cannot be parallelized, as can be done in separate compilation (via distcc or similar programs). Thus SCU requires explicit partitioning (manual partitioning or "sharding" into multiple units) to parallelize compilation. SCU also allows an optimizing compiler to perform interprocedural optimization without requiring link-time optimization, therefore allowing optimizations such as inlining, and helps avoiding implicit code bloat due to exceptions, side effects, and register allocation. These optimizations are often not possible in many compilers, due to independent compilation, where optimization happens separately in each translation unit during compilation, but the "dumb linker" simply links object files, without performing any optimizations itself, and thus interprocedural optimization between translation units is not possible.

Example For example, suppose there exist the source files Foo.cpp and Bar.cpp, they can be placed in a Single Compilation Unit as follows:

Suppose Foo.cpp and Bar.cpp are:

Now the standard header file (<iostream>) is compiled only once, and class Bar and Bar::qux() may be inlined into function main(), despite being from another source file.

See also Precompiled header Modules (C++) C preprocessor Whole program optimization

References

Worked examples

Example 1 — a first encounter with Single compilation unit

Start with the simplest possible case. Write down what Single compilation unit claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Single compilation unit before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Single compilation unit ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Single compilation unit

In research
Single compilation unit appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Single compilation unit in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Single compilation unit is common in secondary-school and first-year university syllabi. It links to neighbouring topics C++, so understanding it makes those chapters shorter.
In everyday life
Look for Single compilation unit outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Single compilation unit” →

How to study Single compilation unit in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Single compilation unit means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Single compilation unit out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Single compilation unit in simple terms?

Single compilation unit (SCU) is a computer programming technique for the C and C++ languages, which reduces compilation time for programs spanning multiple files. Specifically, it allows the compiler to keep data from shared header files, definitions and templates, so that it need not recreate the…

Why does Single compilation unit matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Single compilation unit?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Single compilation unit.

Tags

  • C++

Keep exploring