AI for C++ code: CMake builds, compiler errors and sanitizers

C++ forgives a model a lot at compile time and punishes it later. How to build a build, test and sanitizer loop for an agent and what it costs on different models.

5 min · · development · агенты

If you are choosing an AI for C++ code, the key feature of the language is that "it compiles" does not mean "it works". A model easily writes code that builds without warnings and still contains undefined behavior, a data race or a dangling reference. So the value of an AI agent in C++ depends less on the model choice than on the verification loop around it: build, tests, sanitizers. Below: how to organize that loop, how to feed compile errors to the agent and what it costs.

What AI does well in C++ and where checks are needed

Typical tasks where an agent saves time: scaffolding a class and its operators, converting a loop into STL algorithms, moving code to a newer standard, writing unit tests, decoding a long compiler message about templates, routine CMake edits. We do not claim any model writes C++ better: that has to be checked on your code.

Where mistakes cost the most and machine checking is therefore needed:

  • object lifetime (references to temporaries, iterators after a container changes);
  • multithreading and memory access ordering;
  • signed overflow, out-of-bounds access, use of uninitialized data;
  • incompatibility with your standard (C++17 versus C++20) and your compiler.

Give the agent the build commands

An agent without build commands guesses. Write them in the project's instruction file: CLAUDE.md for Claude Code or AGENTS.md for Codex (more: how to write CLAUDE.md, AGENTS.md for Codex). An example block for a CMake project:

bash
## Build and verify
- Standard: C++20, clang or gcc, set in CMakeLists.txt. Do not change the standard.
- Configure: `cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug`
- Build: `cmake --build build -j`
- Tests: `ctest --test-dir build --output-on-failure`
- Sanitizers: `cmake -S . -B build-asan -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS="-fsanitize=address,undefined -fno-omit-frame-pointer"`, then build and run ctest in `build-asan`
- Do not add dependencies or edit CMakeLists.txt without asking.
- After every change: build with no warnings, then run tests.

The -fsanitize=address,undefined flags are understood by gcc and clang; on Windows with MSVC the options differ, so check your compiler's documentation. The thread sanitizer (-fsanitize=thread) cannot be combined with the address sanitizer and needs a separate build.

Turn on warnings (-Wall -Wextra, and -Werror in CI if you like): they catch some mistakes before any test and give the agent exact text to work from. clang-tidy is also useful if you already have it configured.

How to feed compile errors to an agent

A common mistake is pasting a thousand lines of build output into the chat. For C++ a different rule works: the first error matters more than the rest, because the rest are often its consequence. Give the model:

  1. the first 30-50 lines of output, starting at the first error:;
  2. the file and line the compiler points to, with about ten lines around it;
  3. your standard version and compiler.

With a console agent, it runs the build and reads the output itself. If you need your own script, here is a minimal example in Python with the OpenAI SDK:

bash
import subprocess
from openai import OpenAI

client = OpenAI(base_url="https://api.zukko.pro/v1", api_key="fetch_your_key")

def build() -> str:
    p = subprocess.run(
        ["cmake", "--build", "build", "-j"],
        capture_output=True, text=True,
    )
    return "" if p.returncode == 0 else (p.stdout + p.stderr)

log = build()
if log:

This script only explains and does not edit files: in C++ applying fixes automatically without a test check is risky. If you turn it into a loop, limit the number of iterations (for example to five) and do not let it keep "fixing" the same thing forever.

Sanitizers as the judge

Tests check the scenarios you thought of. Sanitizers check what actually happens in memory. A working order for a change task:

  1. The agent makes an edit and builds the project.
  2. Tests run in the regular build.
  3. The same tests run in a build with address,undefined.
  4. If a sanitizer finds something, its output (stack and error type) goes back to the agent.

A sanitizer report contains a call stack and the type of violation, so it is a very informative input for a model. Ask it to explain the cause first and only then propose a fix: that makes it easier to notice when it treats a symptom. And do read the fix yourself: a model may silence a warning instead of removing the cause.

Which model to try

We do not rank models by code quality, only by price, context and strength index (Intelligence Index, Artificial Analysis). Prices as of October 18, 2026, $ per 1M tokens:

ModelInput / outputContextIndex
claude-opus-5-50.8 / 41M58
gpt-6.1-sol0.27 / 1.321M52
claude-sonnet-5-50.4 / 21M48
deepseek-v4.1-flash0.013 / 0.0381M39

A reasonable scheme: keep a cheap model for routine (error triage, tests) and bring in a strong one for hard spots such as templates and multithreading. Try both on the same set of 5-10 of your tasks and compare not only the result but the number of iterations. A 1M-token context helps with C++ too: headers inflate the input.

Cost example: fixing build errors

Assumption: 15 "build, fix" iterations, each with 20,000 input tokens (files and log) and 1,000 output tokens. Total input 15 × 20,000 = 300,000, output 15 × 1,000 = 15,000.

  • claude-sonnet-5-5: 0.3 × $0.4 + 0.015 × $2 = $0.12 + $0.03 = $0.15.
  • gpt-6.1-sol: 0.3 × $0.27 + 0.015 × $1.32 = $0.081 + $0.0198 = $0.1008.
  • deepseek-v4.1-flash: 0.3 × $0.013 + 0.015 × $0.038 = $0.0039 + $0.00057 = $0.00447.

This ignores caching. In agents on the Anthropic format (Claude Code) a repeated prefix is read from cache more cheaply, and for GPT models cache reads are visible in both formats; details and a session calculation are in how much an agent costs with cache. See your own numbers in the "Usage" section of the dashboard. Prices change; the current ones come from GET https://api.zukko.pro/v1/models.

How to connect

For Claude Code set ANTHROPIC_BASE_URL=https://api.zukko.pro (no /v1) and ANTHROPIC_AUTH_TOKEN with a fetch_... key following the guide. For scripts and OpenAI clients use the address https://api.zukko.pro/v1. Create the key in the dashboard; set a daily dollar limit on it so a "fix the error" loop cannot run you into the red.

FAQ

Can AI replace a debugger in C++?

No. It helps you read debugger or sanitizer output and suggest a hypothesis, but the hypothesis has to be verified by running and testing.

What about huge template errors?

Give the first error and 30-50 lines around it, not the entire output. Ask for the instantiation chain in plain words and for the line of your own code where it started.

Is a cheap model enough for C++?

For routine work (documentation, tests, simple errors) it may be, but check it on your code. For subtle lifetime and threading questions, compare it with a stronger model on real tasks.

Does the agent need access to the whole repository?

It needs access to the task files and the build commands. Set rules in CLAUDE.md or AGENTS.md: do not change dependencies or CMake without asking.

zukko

Pay 25×
less

for Claude Code & ChatGPT

One fetch_* key and base URL api.zukko.pro — for Cursor, Claude Code, Codex, and any SDK.

Get API keyDocumentation

api.zukko.pro · fetch_*

AI for C++ code: agent, CMake and sanitizers · Zukko