---
title: "Building C, C++ and Objective-C: unity groups and precompiled headers"
source: https://github.com/obs-rust/obs-studio/blob/524486a44c81ef30b039f10ab955bc778f39e225/BUILD.md
commit: 524486a44c81ef30b039f10ab955bc778f39e225
tier: reference
tokens_est: 4794
---
# Building C, C++ and Objective-C: unity groups and precompiled headers

This is the build policy for every first-party C, C++ and Objective-C
source in this repository (`libobs/`, `libobs-*/`, `frontend/`, `plugins/`,
`shared/`). It exists so the native build is as fast as we can make it while
staying correct on every platform and every CMake generator. Rust has its
own rules in [docs/rust-port/testing-policy.md](docs/rust-port/testing-policy.md).

Out of scope: third-party code (`deps/`), the git submodules
(`plugins/obs-browser`, `plugins/obs-websocket`), the unchanged C tests
(`test/`) and the C oracle (`rust/obs-c-oracle/`). They build the way their
upstream ships them.

## Why

Compiling one translation unit per source re-parses the same headers
hundreds of times. Measured on a fresh Linux tree (`-j16`, no compiler
cache):

| Build | Wall | CPU |
|---|---|---|
| One TU per source | 315 s | 2416 s |
| Precompiled headers | 322 s | 1798 s |
| Precompiled headers + unity | 198 s | 1053 s |

Those are broader native-build measurements, not isolated libobs timings.
A separate libobs-only experiment (GCC 15.2, RelWithDebInfo, `-j8`, three
trials per condition) measured uncached medians of 9.75 → 9.41 s wall and
63.48 → 46.62 s CPU; the wall-time ranges overlap. Warm ccache rebuilds
measured 1.54 → 1.44 s, with all cacheable compilations hitting. See the
[scoped results, raw evidence and reproduction commands](docs/ci-cache-audit-2026-10-17.md#isolated-c-libobs-build)
before applying either result to CI runner or full-app timing claims.

CMake's own `UNITY_BUILD` got us those numbers but batches sources by count
in list order. Each OS has a different source list, so each OS gets different
batches, and a name clash between two files appears only on the platform
where they happen to share a batch: Linux built clean while Windows failed
in `libobs-d3d11`, `nv-filters`, `text-freetype2` and `win-dshow` (#1128,
#1139). A precompiled header force-included in front of files that also
include their own headers hides what each file really depends on: a header
that only compiled thanks to the PCH broke on macOS alone, and a lean
`<windows.h>` from the PCH broke files that needed the full one on Windows
arm64. Per-file skip lists in CMake piled up to work around both.

The fix is to make the grouping and the headers part of the source tree
instead of build-system heuristics: the same files share a translation unit,
behind the same single header, on every compiler, OS and generator.

## The rules

### Dependency warning boundary (decision of 2026-11-06, #2625)

The maintainer decided: "Only strict settings for OUR headers and source.
third_party code is assumed working correctly and allows relaxed compiler
settings to allow it to pass. Make this a decision and put this in ci.yml."

First-party source and headers retain the full warning policy, including every
conversion group a target has enabled. Do not suppress diagnostics for a
first-party file to accommodate a dependency, or edit dependency code to make it
meet our warning policy. Dependency headers use CMake SYSTEM includes; imported
dependency targets retain CMake's default system-header treatment.

The concrete third-party paths, relative to the repository, are `.deps/**`
(downloaded dependency/Qt SDKs, including AMF, FFmpeg and bundled SIMDe),
`deps/**` (vendored libraries, including a SIMDe checkout placed there), and
`plugins/decklink/{linux,mac,win}/decklink-sdk/**`. A dependency remains third
party when vendored inside this repository. The browser and WebSocket git
submodules keep their own build policy. Everything else in the source tree is
first party unless a later reviewed decision explicitly extends this list;
in particular `libobs/`, `frontend/`, `shared/`, generated OBS headers, and the
rest of `plugins/` must never be made SYSTEM roots. Externally installed SDKs
are consumed through imported dependency targets, not by marking an OBS root
SYSTEM.

`cmake/third-party-headers.cmake` classifies the completed native and cross
target graphs. It keeps the original include lists and warning flags, marks
only dependency include directories SYSTEM, and propagates that classification
from vendored libraries to their consumers. Unknown generator expressions stay
strict; build-interface and simple conditional paths are classified explicitly. Resolved symlinks into
first-party directories are not exempt. CMake searches SYSTEM directories after
ordinary directories, so verify header resolution and generated code when
changing this boundary. Include dependency headers through their dedicated
SYSTEM include roots with angle brackets, including vendored dependencies;
spelling them through the ordinary repository root would bypass the exemption.
This dependency rule is an exception to the repo-root spelling rule below.

This decision is enforced by `ci/test_cross_conversion_ratchet.py`, including
a real compile that accepts a conversion in a dependency header and rejects one
in a first-party header. It runs in the existing required `ci.yml` path:
`Format 🔍 / checks` → `ci/ Self-Tests 🧪` → `ci/local_gate.py --only self-test`.
`CI Gate ✅` requires the Format workflow. "Put this in ci.yml" means enforcement
through that existing job; it does not require another workflow step or a change
to the external `zackees/ci.yml` policy repository.

### 1. File names say the role

| Role | C | C++ | Objective-C | Objective-C++ |
|---|---|---|---|---|
| Implementation (the code) | `foo.c.h` | `foo.cpp.hpp` | `foo.m.h` | `foo.mm.hpp` |
| Group (what the build compiles) | `name+.h.c` | `name+.hpp.cpp` | `name+.h.m` | `name+.hpp.mm` |
| Precompiled header | `family.pch.h` | `family.pch.hpp` | `family.pch.h` | `family.pch.hpp` |
| Header (declarations) | `foo.h` | `foo.hpp` | `foo.h` | `foo.h` |

An implementation file is ordinary source; the build reaches it only through
a group. C-family files end in `.h` and C++-family files in `.hpp` because
tools pick the language from the last extension: clang-format formats a
`.hpp` as C++, where names C code uses (`private`, `new`) are keywords. Group files and precompiled headers live in a `build/` directory
next to the component's `CMakeLists.txt` (`libobs/build/`). A group contains
its precompiled header, then its implementation files in alphabetical order:

```c
/* libobs/build/libobs.util+.h.c */
/* Unity group (BUILD.md): util/ and callback/. */

#include "libobs/build/libobs.pch.h"

#include "libobs/callback/calldata.c.h"
#include "libobs/callback/decl.c.h"
#include "libobs/util/cf-lexer.c.h"
```

Every quoted include of a repo file in a group, a precompiled header, an
implementation file or a forwarding file is a repo-root path
(`plugins/obs-ffmpeg/obs-ffmpeg-formats.h`), never one relative to the file
(`../obs-ffmpeg-formats.h`): a line reads the same wherever it is, and moves
with no rewriting. `cmake/common/groups.cmake` makes the repo root an include
directory of every target. Headers found through a target's own include
directories (`<obs-module.h>`, `"util/base.h"`) keep their usual form.

### 2. One header per group, and it is the only include

Every implementation file includes exactly one header: its group's
precompiled header, as its first line.

```c
/* libobs/util/cf-lexer.c.h */
#include "libobs/build/libobs.pch.h"

static inline void cf_convert_from_escape_literal(...)
```

Everything a file needs (system headers, the component's own headers, the
feature macros that must precede them such as `_FILE_OFFSET_BITS` or
`WIN32_LEAN_AND_MEAN`, and the platform conditionals around them) lives in
that header, in the order the files need it. A file's own macros (`do_log`, a
`FALLBACK_FRAMERATE`) stay in the file, right after its include: only the
macros that set up headers move. A header that defines macros of its own
(obs-outputs' `rtmp-stream.h` defines `do_log`) puts its includers in a
family of their own, and a header that defines data (`mp4-mux-internal.h`)
stays out of headers the C tests compile more than once. So the precompiled header is
exactly what its files depend on, every member of a group sees the same
thing whether it is compiled in the group or alone, and nothing is
force-included in front of anything.

Groups share a header when their files need the same things (all of
libobs's core groups use `libobs.pch.h`), which is what makes precompiling it
pay. A group gets a header of its own where its dependencies differ:

- third-party headers some files must not pull in (FFmpeg for
  `libobs-media.pch.h`; the Rust-ported files, which the C oracle and the C
  tests compile on their own, have `libobs-ported-*.pch.h` without them),
- a platform (`libobs-windows.pch.h`, `libobs-nix.pch.h`), named so
  `ci/platform_ifdef_ratchet.py` sees its host,
- an optional feature (`libobs-pulse.pch.h`, `libobs-wayland.pch.h`),
- a feature macro that must come first (`libobs-platform.pch.h` for
  `_FILE_OFFSET_BITS`, `libobs-ported-posix-threading.pch.h` for
  `_GNU_SOURCE`),
- a different `<windows.h>` (lean or full).

One language per group: C, C++, Objective-C and Objective-C++ files never
share one. Platform-only groups are listed in the component's
`cmake/os-<os>.cmake` like any platform source, with no `#ifdef` choosing
between groups.

Two exceptions, both after the code starts:

- **No-op host capabilities** (`libobs/obs-host/noop/<capability>.hpp`,
  #1051) are headers, not group members: each host file
  (`obs-nix.c.h`, `obs-windows.c.h`, `obs-cocoa.m.h`) includes the
  ones for the capabilities it lacks, at its end.
- **X-macro tables** (a header without `#pragma once` or an include guard,
  such as `obs-hotkeys.h`) included as data.

A C test that `#include`s a source file by its old name (`#include "media.c"`)
keeps working through a one-line forwarding file left at that name
(`media.c`: `#include "shared/media-playback/media-playback/media.c.h"`). The build never compiles it, and the
unchanged C test needs no edit.

### 3. Unity and precompiled headers are always on

Converted components build one way: each group is one translation unit, and
the header most of a target's groups share is precompiled (per language; on
Xcode, which allows one prefix header per target, for the language with the
most groups). A group of another family includes its header textually.
`ENABLE_UNITY_BUILD` and `ENABLE_PCH` only steer components not yet
converted.

A target that compiles one implementation file directly (a C test pinning a
single libobs file) compiles a generated one-line wrapper with a real
extension that includes it (generators, Xcode among them, take a source's
type from its extension), with the owning target's include directories,
since its header comes from there.

### 4. Every implementation file is in exactly one group

A file in no group silently drops out of the build; a file in two defines
its symbols twice. It may be in several groups only when each is for a
different host. `ci/test_build_groups.py` (part of `./lint`) checks this,
the shape of every group, and rule 2. New code joins a group in the same
commit that adds the file.

### 5. Names that are file-local must be unique within a group

Inside a group, every implementation file shares one translation unit, so:

- `static` functions and variables, file-scope `enum` enumerators and
  anonymous-namespace names get a prefix from their file
  (`od_` for `obs-output-delay`, `compressor_` for the compressor filter).
  An anonymous namespace does not help: the files share it.
- A file-scope `#define` is `#undef`ed at the end of the file
  (`ci/test_build_groups.py` checks it; a macro a header provides may be
  restored there instead). A macro that
  shadows a system name (`near`, `far`) is saved and restored with
  `push_macro`/`pop_macro`.
- Default arguments are declared once, in the header.
- Headers have `#pragma once` and are never duplicated under two paths (a
  copy forwards to the original).
- No `using namespace std;` in a group whose header brings in global names
  `std` also declares (`byte` from the Windows SDK): qualify instead.

When a clash is cheaper to isolate than to rename (two backends that
deliberately define the same functions, a vendored fork), the file gets its
own group. That is the only exemption, and it lives in the source tree, not
in CMake.

### 6. Renames are mechanical and in place

Turning `foo.c` into `foo.c.h` keeps the directory and the stem, so
`git log --follow`, `git blame` and merges from `upstream/master` follow
the file. Each component converts in its own PR with this order of commits:

1. Only `git mv` of the implementation files (no content change), so rename
   detection is exact.
2. The groups, their precompiled headers (each file's includes moved into
   its group's header), the CMake change, and path updates in `ci/`, the
   platform-ifdef baseline, the C tests' CMake and `rust/` (oracle includes).
3. Any clash fixes.

When an upstream merge adds a new `foo.c` to a converted component, rename
it, move its includes into its group's header and add it to a group in the
merge PR.

The libobs source sets are one JSON list per set,
`libobs/build/source-sets.d/<SET>.json` (`LIBOBS_SOURCES.json`,
`LIBOBS_WINDOWS_PORTED_SOURCES.json`, ...; #3235). A change edits only the
lists it touches, and a host set's file name carries its OS token
(`WINDOWS`, `MACOS`, `LINUX`, `NIX`, `PULSE`, `WAYLAND`), so
`ci/platform_paths.py` selects that OS's CI lanes for it while a shared set
selects what any libobs file does. Adding a set means adding its file (CMake
re-runs configure through the `CONFIGURE_DEPENDS` glob) and the code that
consumes it.

A newly ported libobs unity group can register itself in
`libobs/build/ported-source-sets/<unit>.json` with the same
`LIBOBS_*_PORTED_SOURCES` keys as the set files. The CMake,
Cargo-native and Tier 2 inventory readers discover these fragments, so two
ports can add their own files without editing the same ported-source list.
Keep each member in exactly one list or fragment.

A port edits only its unit file, never `libobs/CMakeLists.txt` or
`libobs/cmake/os-<os>.cmake` (#3236; `ci/test_port_unit_metadata.py`
checks it). Besides its set keys, a unit file takes two optional fields,
which `libobs/cmake/sources.cmake` applies:

```json
{
  "LIBOBS_PORTED_SOURCES": ["build/libobs.ported-audio-resampler-drift+.h.c"],
  "compile_options": {"gnu": ["-ffp-contract=off"], "msvc": ["/wd4996"]},
  "headers": ["media-io/audio-resampler-drift.h"]
}
```

- `compile_options`: flags appended to each of the unit's groups, per
  compiler family: `gnu` (GCC, Clang, AppleClang; C and Objective-C) or
  `msvc`.
- `headers`: headers (relative to `libobs/`) listed on `libobs-objects`
  wherever one of the unit's sets is built; the Linux and FreeBSD host files
  share one `libobs_add_ported_sources(<SET>)` call per optional set, so a
  port no longer edits both.

The Cargo-native build and the Tier 2 inventory read the set keys and ignore
these fields. Settings for a whole target (warning pins, definitions) stay in
the CMake files.

libobs's groups are partitioned into lib-modules, the unit the module
cache of #3256 compiles, caches and relinks:
`libobs/build/modules.d/<module>.json` gives a module's groups as
`fnmatch` patterns on the group name (`core`, `nix-*`, `ported-*`) and
the modules it `depends` on. Patterns keep a new group from editing a
central list: a new `ported-foo` group is already in `ported`. A new
non-matching group goes into the module whose code it is.
`ci/test_build_groups.py` checks that every libobs group is in exactly one
module, that every pattern still matches a group, and that the
dependencies exist and form no cycle (#3258). `libobs/cmake/modules.cmake`
compiles each module into a static archive, `libobs-mod-<module>`, linked
whole into libobs (#3259, #3261).

`ci/libobs_modules.py` caches those archives (#3262). A module's key covers
its compile commands (build dir and compiler launcher left out), every file
its groups read (the preprocessor's `-M -MG` list) and the compiler version.
On a configured tree:

```sh
uv run ci/libobs_modules.py restore --build <dir>   # copy hits, write hits.cmake
cmake -B <dir> -DOBS_LIBOBS_MODULE_CACHE=<dir>/libobs-modcache
cmake --build <dir> --target libobs
uv run ci/libobs_modules.py store --build <dir>     # cache the modules built
```

A hit is an IMPORTED archive, not compiled. An unbuilt `-keyprobe` target
keeps its compile commands visible, so the next `restore` sees an edit and
drops the hit. Run `restore` before each build. The cache dir is
`$OBS_LIBOBS_MODULE_CACHE_DIR`, else `modcache` beside the zccache root.
MSVC compile commands are not keyed yet.

The Rust staticlib (`libobs-rust`) is one more cached module (#3265). Its key
covers `libobs-modcache-rust.json` (the features, triple, profile and C
compiler that `libobs/cmake/rust.cmake` records), `Cargo.lock`,
`rust-toolchain.toml`, `rustc -vV`, `RUSTFLAGS`, gio's pkg-config version,
and every file of the path crates `libobs-rust` depends on, found by
walking their `Cargo.toml` files, except `tests/` and `benches/`. It also
covers the files outside those crates that their build scripts read. On a hit,
libobs links the cached archive with Corrosion's link libraries and cargo
does not run.

## Verifying a change

- Linux: `./unittest` and the local gate as usual; `./lint` runs
  `ci/test_build_groups.py`.
- Windows and macOS from Linux: `cmake/cross` (soldr's clang with the
  Windows SDK and the Apple SDK) builds libobs and the platform plugins
  through the same groups. Extend it when a component converts.
- A change to `cmake/common/`, `cmake/pch/` or a precompiled header itself
  must pass a full Windows and macOS build
  (`gh workflow run ci.yml --ref <branch>`, which runs every build) before
  it merges. PR CI never builds the OBS apps: `cmake/common/` starts only
  the macOS and Windows Rust tests and Tier 2, `cmake/pch/` starts nothing
  beyond the Fast Gate, and a `libobs/build/` file starts Linux Tier 2. A
  port that only adds its source's `#include` to a `ported-*` group does
  not need the manual run.
- `cmake/common/` stays host-neutral (#3234, `ci/test_cmake_common_os_neutral.py`):
  no `OS_<HOST>` branch, MSVC generator expression or `CMAKE_OSX_*` /
  `CMAKE_VS_*` read outside `osconfig.cmake` and `soldr.cmake`. Host-only
  logic goes in `cmake/windows/`, `cmake/macos/` or `cmake/linux/`, which start
  only their own OS lane; a common helper that needs a host variant defines
  a neutral default the OS module redefines after including it (for example
  `_obs_architecture_matches`, `obs_msvc_exempt_c4201`,
  `_obs_dependency_platform_hook`).

## Transition

Components not yet converted still build with CMake's `UNITY_BUILD`
batching, the shared headers in `cmake/pch/` and the skip lists in
`cmake/common/unity.cmake` and `cmake/common/pch.cmake`. Those lists only
shrink: a new entry means the file's component should be converted instead.
When the last component converts, the batching, `cmake/pch/` and the skip
lists are deleted. Order: `libobs/` (done), then `libobs-*/`, `shared/`,
`plugins/`, `frontend/`.
