> ## C/C++ Instrumentation

> Fetch the complete documentation index at: https://antithesis.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

---

## Prerequisites

Instrumentation for C and C++ is packaged with the [C++ SDK](/docs/reference/sdk/cpp/). Instrumentation is supported by the header file `antithesis_instrumentation.h`. This file must be included in one, and only one translation unit at link time. Coverage instrumentation requires Clang 13 or higher, and either C++ 11 or higher or C89 or higher.

We recommended that you include the instrumentation header in the file where you define the `main` function, as follows:

```cpp
#include "antithesis_instrumentation.h"
```

> **Note**
>
> If you are unable to meet the requirements of our C/C++ instrumentation, perhaps because you use an older version of Clang or are unable to include a header file, then you can try our [legacy instrumentation approach](/docs/reference/sdk/cpp/old_c_instrumentation/). This approach does not require a modern Clang or a header file, but it will instead require you to provide a shared object at link-time.

## Build Requirements

Your instrumented binary must be built with libraries and dependencies that it will use at runtime.

Add the following compile flag:

```
-fsanitize-coverage=trace-pc-guard,pc-table -g
```

And add the following link flag:

```
--build-id
```

> **Note**
>
> LLVM-instrumented binaries must have GNU build ids for symbolization to work. As long as you use the --build-id link flag above, you'll be fine.

If your build process includes a single compile/link step, then add both compile and link flags with the following:

```
-fsanitize-coverage=trace-pc-guard,pc-table -g -Wl,--build-id
```

> **Note**
>
> We recommend the use of `-g` but it is not necessary. It will provide more detailed information when bugs are found, which may make debugging easier.

## Link-time optimization

If you build with `-flto` or `-flto=thin`, instrument at link time rather than at compile time. The `-fsanitize-coverage` compile flag inserts the coverage callbacks before link-time optimization runs, and the optimizer then inlines, merges, and tail-calls the instrumented code in ways Antithesis cannot symbolize.

Instead, let the linker's optimization pipeline instrument the fully optimized program:

1. Compile every translation unit with `-flto` (or `-flto=thin`) and `-g`, and **without** `-fsanitize-coverage`.
2. Compile the one translation unit that includes `antithesis_instrumentation.h` **without** `-flto`. Keep that translation unit to just the include, so the rest of your program still gets link-time optimization. The header's runtime shim has to be a regular object file: it must stay uninstrumented, and it defines the coverage hooks that the link step's instrumentation will call.
3. Link with lld and these flags:

```
-fuse-ld=lld \
-Xlinker '--lto-newpm-passes=lto<O2>,sancov-module' \
-Wl,-mllvm,-sanitizer-coverage-level=3 \
-Wl,-mllvm,-sanitizer-coverage-trace-pc-guard \
-Wl,-mllvm,-sanitizer-coverage-pc-table \
-Wl,--build-id
```

Match the optimization level in `lto<O2>` to your `-O` flag (`lto<O3>` for `-O3`, and so on). With `-flto=thin`, use `thinlto<O2>` in place of `lto<O2>`.

For a program with two translation units, the whole build looks like this:

```
clang++ -O2 -flto -g -c main.cpp -o main.o
clang++ -O2 -flto -g -c worker.cpp -o worker.o
clang++ -O2 -c antithesis_instrumentation.cpp -o antithesis_instrumentation.o
clang++ -O2 -flto -fuse-ld=lld main.o worker.o antithesis_instrumentation.o \
    -Xlinker '--lto-newpm-passes=lto<O2>,sancov-module' \
    -Wl,-mllvm,-sanitizer-coverage-level=3 \
    -Wl,-mllvm,-sanitizer-coverage-trace-pc-guard \
    -Wl,-mllvm,-sanitizer-coverage-pc-table \
    -Wl,--build-id -o main
```

where `antithesis_instrumentation.cpp` contains only `#include "antithesis_instrumentation.h"`.

> **Note**
>
> - The pass list contains a comma, and `-Wl,` splits its argument at commas. Pass that flag through `-Xlinker` as shown, and if your build system wraps linker flags for you, make sure the comma survives.
> - The linker must be lld from the same LLVM version as your clang.
> - Do not also pass `-fsanitize-coverage` at compile time, or the program is instrumented twice.

To confirm that instrumentation ran after optimization, check that the coverage hook is only ever called, never jumped to:

```shell frame="none"
$ llvm-objdump -d main | grep -c 'jmp.*__sanitizer_cov_trace_pc_guard'
0
```

## Validation

To confirm that the instrumentation process was successful, you can run the command `nm` to list all the symbols in a binary, and then `grep` the string *"antithesis_load_libvoidstar"*.

```shell frame="none"
$ nm client_binary | grep "antithesis_load_libvoidstar"
T antithesis_load_libvoidstar
```

Notice the **"T"** character in the previous code block: it means the symbol is in the text (code) section.

If this does not work you probably failed to include the header file.

## Symbolization

The LLVM compiler infrastructure will output debug symbols in the [DWARF](https://en.wikipedia.org/wiki/DWARF) format. Antithesis needs either the original unstripped binary, or the binary together with its debug info (as produced by `objcopy --only-keep-debug` and `--add-gnu-debuglink`). Symlink (or move) these files into a directory named `/symbols` in the root of the [appropriate container image](/docs/product/writing_tests/instrumentation/coverage_instrumentation/#symbolization).

Antithesis labels the coverage it reports for each binary with the binary's file name.

## Help

[Contact us](/company/contact/) if you need help with this.
