$linuxjunkies
>

clang(1)

Clang is a C/C++/Objective-C compiler frontend that uses LLVM as its backend, providing fast compilation and detailed diagnostics.

UbuntuDebianFedoraArch

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

FlagWhat it does
-cCompile only; do not link (produce object files)
-o FILEWrite output to FILE instead of default location
-O0, -O1, -O2, -O3, -OsOptimization levels (0=none, 1=minimal, 2=standard, 3=aggressive, s=size)
-WallEnable most common compiler warnings
-WextraEnable additional warnings beyond -Wall
-I DIRAdd DIR to header search path
-D NAME[=VALUE]Define preprocessor macro NAME (optionally with VALUE)
-gGenerate debug symbols for use with debuggers
-fPICGenerate position-independent code for shared libraries
-std=STANDARDSpecify C/C++ standard (e.g., c99, c11, c++11, c++17, c++20)
-pedanticIssue warnings about non-standard language features
-ERun preprocessor only, output to stdout

Examples

Compile hello.c to executable named hello

clang -o hello hello.c

Compile main.c to object file with warnings enabled and optimization level 2

clang -c -Wall -O2 main.c

Compile with C11 standard and strict standard compliance checking

clang -std=c11 -pedantic program.c -o program

Compile with debug symbols, no optimization, and warnings for debugging

clang -g -O0 -Wall debug_app.c -o debug_app

Compile with custom include and library paths, linking against libmylib

clang -I/usr/local/include -L/usr/local/lib app.c -lmylib -o app

Run preprocessor with DEBUG macro defined, show first 20 lines of output

clang -E -DDEBUG source.c | head -20

Compile position-independent code for building shared libraries

clang -fPIC -c library.c

Compile all C files in current directory with all common warnings enabled

clang *.c -Wall -Wextra -o program

Related commands