Skip to content
Latchkey

clang Command: Compile C/C++ in CI

clang compiles C, C++, and Objective-C with LLVM and clear diagnostics.

clang is the LLVM C-family compiler, valued in CI for fast builds, precise error messages, and first-class sanitizers. It is largely flag-compatible with gcc, so most gcc CI scripts run unchanged with clang.

Common flags

  • -c - compile only, emit an object file
  • -o NAME - name the output file
  • -I DIR / -L DIR / -lNAME - header path / lib path / link library
  • -std=c17 / -std=c++20 - select the language standard
  • -fsanitize=address,undefined - enable ASan/UBSan instrumentation
  • -Wall -Wextra -Werror - warnings; -Werror makes them fatal
  • -Weverything - enable nearly every clang warning (very strict)

Example in CI

Build a sanitized debug binary to catch memory bugs in a CI test run:

shell
clang -std=c17 -g -O1 -fsanitize=address,undefined -Wall -Wextra src/main.c -o app

Common errors in CI

  • fatal error: 'foo.h' file not found - missing include path or -dev package
  • error: unknown argument: ... - a gcc-only flag clang does not accept
  • ld: symbol(s) not found / undefined reference - missing library at link
  • runtime: AddressSanitizer: heap-use-after-free - a real bug surfaced by ASan

Key takeaways

  • clang is mostly gcc-flag-compatible, so CI scripts port with little change.
  • -fsanitize=address,undefined is the fast way to catch memory/UB bugs in CI.
  • Watch for "unknown argument" when a script passes gcc-only flags to clang.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →