clang(1)
Clang is a C/C++/Objective-C compiler frontend that uses LLVM as its backend, providing fast compilation and detailed diagnostics.
Synopsis
clang [OPTIONS] FILE...Description
Clang is a compiler front end for C, C++, Objective-C and Objective-C++ that uses LLVM as its backend. It offers fast compilation speeds, excellent error messages with source location highlighting, and a modular architecture. Clang aims to be compatible with GCC while providing superior diagnostics and performance in many cases.
The compiler processes source files through preprocessing, parsing, semantic analysis, code generation, and linking. It supports most GCC flags for compatibility while adding its own extensions and improvements.
Common options
| Flag | What it does |
|---|---|
-c | Compile only; do not link (produce object files) |
-o FILE | Write output to FILE instead of default location |
-O0, -O1, -O2, -O3, -Os | Optimization levels (0=none, 1=minimal, 2=standard, 3=aggressive, s=size) |
-Wall | Enable most common compiler warnings |
-Wextra | Enable additional warnings beyond -Wall |
-I DIR | Add DIR to header search path |
-D NAME[=VALUE] | Define preprocessor macro NAME (optionally with VALUE) |
-g | Generate debug symbols for use with debuggers |
-fPIC | Generate position-independent code for shared libraries |
-std=STANDARD | Specify C/C++ standard (e.g., c99, c11, c++11, c++17, c++20) |
-pedantic | Issue warnings about non-standard language features |
-E | Run preprocessor only, output to stdout |
Examples
Compile hello.c to executable named hello
clang -o hello hello.cCompile main.c to object file with warnings enabled and optimization level 2
clang -c -Wall -O2 main.cCompile with C11 standard and strict standard compliance checking
clang -std=c11 -pedantic program.c -o programCompile with debug symbols, no optimization, and warnings for debugging
clang -g -O0 -Wall debug_app.c -o debug_appCompile with custom include and library paths, linking against libmylib
clang -I/usr/local/include -L/usr/local/lib app.c -lmylib -o appRun preprocessor with DEBUG macro defined, show first 20 lines of output
clang -E -DDEBUG source.c | head -20Compile position-independent code for building shared libraries
clang -fPIC -c library.cCompile all C files in current directory with all common warnings enabled
clang *.c -Wall -Wextra -o program