LinuxCommandLibrary

clang

TLDR

Compile C program

$ clang [program.c] -o [program]
copy
Compile with optimization
$ clang -O3 [program.c] -o [program]
copy
Compile with debug info
$ clang -g [program.c] -o [program]
copy
Enable all warnings
$ clang -Wall -Wextra [program.c] -o [program]
copy
Compile C++ program
$ clang++ [program.cpp] -o [program]
copy

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

$ # Simple compile
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
copy

SANITIZERS

Address Sanitizer:

Detect memory errors
Thread Sanitizer:
Detect data races
Undefined Behavior Sanitizer:
Detect undefined behavior
Memory 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.

SEE ALSO

gcc(1), clang++(1), llvm(1)

Copied to clipboard