clang
TLDR
Compile C program
SYNOPSIS
clang [options] file...
DESCRIPTION
clang is a C, C++, and Objective-C compiler based on LLVM. It provides fast compilation, excellent diagnostics with clear error messages, and is designed as a drop-in replacement for GCC.
The compiler is known for its helpful error messages and modern architecture.
PARAMETERS
-o file
Output file name-c
Compile without linking-g
Generate debug information-O[level]
Optimization (0, 1, 2, 3, fast, s, z)-std=standard
Language standard (c11, c17, c++17, c++20)-Wall
Enable common warnings-Wextra
Enable extra warnings-Werror
Treat warnings as errors-I dir
Add include directory-L dir
Add library directory-l lib
Link library-fsanitize=type
Enable sanitizer (address, thread, memory, undefined)
OPTIMIZATION LEVELS
- -O0 - No optimization (default)
- -O1 - Basic optimization
- -O2 - Moderate optimization (recommended)
- -O3 - Aggressive optimization
- -Os - Optimize for size
- -Ofast - Maximum speed (may break standards)
WORKFLOW
clang hello.c -o hello
# With warnings and optimization
clang -Wall -Wextra -O2 program.c -o program
# Debug build
clang -g -O0 program.c -o program
# C++ compilation
clang++ -std=c++20 program.cpp -o program
# With sanitizers
clang -fsanitize=address -g program.c -o program
# Multiple source files
clang main.c utils.c -o program
# Link with library
clang program.c -o program -lm -lpthread
SANITIZERS
Address Sanitizer:
Detect memory errorsThread Sanitizer:
Detect data racesUndefined Behavior Sanitizer:
Detect undefined behaviorMemory Sanitizer:
Detect uninitialized memory reads
CAVEATS
Some GCC-specific features not supported. Different optimization behavior than GCC. Sanitizers add overhead. Error messages more verbose than some prefer. Compilation may be slower than GCC in some cases.
HISTORY
clang was created by Apple and the LLVM community starting in 2007 as a modern, modular compiler to replace GCC, first released in 2009.


