79831228

Date: 2025-11-26 23:32:59
Score: 0.5
Natty:
Report link

The Main Correctness Issue: Name Collisions

​The most significant risk in a unity build using standard include guards (#ifndef HEADER_H) is Macro Name Collision.

​In a standard build, source files are compiled individually. If LibraryA/utils.h and LibraryB/utils.h both happen to use the include guard UTILS_H, it rarely causes issues because they are usually compiled in separate translation units.

​However, in a Unity Build, dozens or hundreds of files are combined into a single translation unit.

  1. ​The preprocessor reads LibraryA/utils.h, defines UTILS_H, and includes the content.

  2. ​Later in the same file, the preprocessor reaches LibraryB/utils.h.

  3. ​It sees that UTILS_H is already defined.

  4. It silently skips the content of LibraryB/utils.h.

​This results in confusing "incomplete type" or "undefined symbol" errors because the compiler literally ignored the second header file.

​**#pragma once prevents this.** It relies on the file's unique identity (inode, file path) rather than a user-defined name. Even if two files are named utils.h, the compiler knows they are different files and will include both.

​The Tradeoffs

Reasons:
  • Long answer (-1):
  • No code block (0.5):
  • Low reputation (1):
Posted by: Plio Peinataka