LinuxCommandLibrary

clang++

Compile C++ source code

TLDR

Compile a set of source code files into an executable binary

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

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

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

Choose a language standard to compile for
$ clang++ [path/to/source.cpp] -std=[c++20] [[-o|--output]] [path/to/output_executable]
copy

Include libraries located at a different path than the source file
$ clang++ [path/to/source.cpp] [[-o|--output]] [path/to/output_executable] -I[path/to/header_path] -L[path/to/library_path] -l[path/to/library_name]
copy

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

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

Display version
$ clang++ --version
copy

SYNOPSIS

clang++ [options...] filename...
clang++ filename -o output [options]

PARAMETERS

-o file
    Specify output filename

-c
    Compile to object file only, no linking

-Idir
    Add directory to include search path

-Ldir
    Add directory to library search path

-lname
    Link against library libname

-std=c++std
    Language standard (e.g., c++11, c++20, gnu++17)

-Olevel
    Optimization level (0-3, s, z, fast)

-g
    Generate debug information

-Wall
    Enable most common warnings

-Werror
    Treat warnings as errors

-Dmacro[=def]
    Define macro

-Umacro
    Undefine macro

-fPIC
    Generate position-independent code

-shared
    Create shared object file

-pthread
    Support POSIX threads

-fuse-ld=linker
    Use specified linker (lld, bfd)

-march=arch
    Target architecture (native, x86-64)

-mtune=tune
    Optimize for specific CPU

--version
    Print version information

-v
    Verbose output, show commands

-E
    Preprocess only

-M
    Generate dependency file

-MMD
    Like -M but ignore system headers

-fmodules
    Enable C++ modules

-stdlib=lib
    C++ standard library (libc++, libstdc++)

DESCRIPTION

clang++ is the C++ compiler driver from the LLVM/Clang project, designed as a drop-in replacement for g++. It compiles C++ source code into executables or object files, leveraging LLVM's modular infrastructure for optimization and code generation. Key advantages include faster compilation speeds, superior diagnostics with fix-it hints, and excellent GCC compatibility. It supports modern C++ standards like C++20 and C++23, modules, and OpenMP. clang++ automatically selects the appropriate language mode for C++ files (.cpp, .cxx, etc.) and invokes the clang frontend with -std=c++ defaults. It integrates seamlessly with build systems like CMake and supports cross-compilation for various architectures. Widely used in projects prioritizing performance and developer experience, such as Chrome, Firefox, and LLVM itself.

Unlike GCC, Clang produces human-readable IR and emphasizes modular design, enabling tools like static analyzers. It handles linking via lld or system linkers.

CAVEATS

Some GCC extensions not fully supported; linker defaults to system linker (use -fuse-ld=lld for LLVM linker). Requires LLVM/Clang installation. Modular headers experimental in some versions. Cross-compilation needs target triples.

EXAMPLES

clang++ main.cpp -o main -std=c++20 -O2 -Wall
clang++ -c file.cpp -I/usr/include/extra

RESOURCES

Man page: man clang++
Official docs: clang.llvm.org
Help: clang++ --help

HISTORY

Developed by Chris Lattner at Apple starting 2007 as part of LLVM; first public release 2010. Became production-ready for C++ around 2012. Apple adopted for Xcode; now default in FreeBSD. Reached GCC-8 parity by 2018. Actively maintained by LLVM Foundation with contributions from Google, Intel, etc.

SEE ALSO

clang(1), gcc(1), g++(1), lld(1), llvm-config(1)

Copied to clipboard