LinuxCommandLibrary

clang

Compile C, C++, Objective-C, and Objective-C++ code

TLDR

Compile multiple source files into an executable

$ clang [path/to/source1.c path/to/source2.c ...] [[-o|--output]] [path/to/output_executable]
copy

Activate output of all errors and warnings
$ clang [path/to/source.c] -Wall [[-o|--output]] [output_executable]
copy

Show common warnings, debug symbols in output, and optimize without affecting debugging
$ clang [path/to/source.c] -Wall [[-g|--debug]] -Og [[-o|--output]] [path/to/output_executable]
copy

Include libraries from a different path
$ clang [path/to/source.c] [[-o|--output]] [path/to/output_executable] -I[path/to/header] -L[path/to/library] -l[library_name]
copy

Compile source code into LLVM Intermediate Representation (IR)
$ clang [[-S|--assemble]] -emit-llvm [path/to/source.c] [[-o|--output]] [path/to/output.ll]
copy

Compile source code into an object file without linking
$ clang [[-c|--compile]] [path/to/source.c]
copy

Optimize the compiled program for performance
$ clang [path/to/source.c] -O[1|2|3|fast] [[-o|--output]] [path/to/output_executable]
copy

Display version
$ clang --version
copy

SYNOPSIS

clang [options] <input files>

PARAMETERS

-o <file>
    Write output to <file>.

-c
    Compile or assemble the source files, but do not link.

-S
    Compile only; do not assemble or link.

-E
    Preprocess only; do not compile, assemble or link.

-I <dir>
    Add directory to include search path.

-L <dir>
    Add directory to library search path.

-l<lib>
    Link with library <lib>.

-D <macro>
    Define a macro.

-U <macro>
    Undefine a macro.

-Wall
    Enable all standard warnings.

-Wextra
    Enable some extra warning flags not enabled by -Wall.

-Werror
    Treat all warnings as errors.

-O<level>
    Specify optimization level. Common levels are O0 (no opt), O1, O2, O3 (aggressive), Os (size), Oz (smaller size).

-std=<standard>
    Specify language standard (e.g., c99, c++11, gnu17).

-g
    Generate debug information.

-v
    Show commands Clang runs.

DESCRIPTION

Clang is a compiler front end for the C, C++, Objective-C, and Objective-C++ programming languages, as well as a variety of other language standards like OpenMP and OpenCL. It is part of the LLVM project and uses LLVM as its back end.

Clang is known for its fast compilation times, excellent and human-readable diagnostic messages, and its modular architecture. It aims to be a complete drop-in replacement for GCC for many common use cases, offering superior error reporting and often faster compilation, especially during development. It's widely used in various software development environments, including macOS and iOS development, and increasingly in Linux and other Unix-like systems.

CAVEATS

While Clang aims for GCC compatibility, there might be minor differences in behavior or warnings for certain non-standard or edge cases. Some older build systems might explicitly expect `gcc` or `g++` and require symlinking or environment variable adjustments (e.g., CC, CXX). Specific compiler extensions or intrinsic functions present in GCC might not have direct equivalents in Clang, or vice versa, though compatibility is generally high for standard C/C++.

STATIC ANALYZER

Clang provides a powerful static analyzer (accessed via scan-build) which can detect logical errors, memory leaks, and other bugs in source code without executing the program.

SANITIZERS

Clang includes various runtime sanitizers (enabled with -fsanitize=) such as AddressSanitizer (ASan), UndefinedBehaviorSanitizer (UBSan), and ThreadSanitizer (TSan), which help detect common runtime errors like memory corruption, undefined behavior, and data races.

MODULE MAPS

Clang introduced "Module Maps" as a way to improve build times and dependency management for C-family languages, similar to modules in other modern languages.

HISTORY

Clang's development began in 2007 at Apple Inc. with the aim of replacing GCC as the compiler for their macOS and iOS development environments. The primary motivations were to provide a more modular and robust compiler infrastructure (LLVM), improve compilation speed, and generate superior diagnostics. It quickly gained traction due to its high quality, liberal licensing (Apache 2.0), and active open-source development. It has since become a major player in the compiler landscape, competing directly with GCC and being adopted by numerous projects and distributions.

SEE ALSO

gcc(1), g++(1), make(1), ld(1), ar(1), llvm-ar(1), llvm-objdump(1)

Copied to clipboard