$linuxjunkies
>

clang++(1)

Clang C++ compiler that translates C++ source code into machine-executable binaries.

UbuntuDebianFedoraArch

Synopsis

clang++ [OPTIONS] FILE...

Description

clang++ is the C++ frontend for the Clang compiler, part of the LLVM project. It processes C++ source files (.cpp, .cc, .cxx, .C) and produces object files or executables. clang++ handles preprocessing, parsing, semantic analysis, optimization, and code generation in a single invocation.

Unlike g++, clang++ emphasizes fast compilation times, clear error messages, and modular architecture. It's compatible with GCC command-line options for easy migration between compilers.

Common options

FlagWhat it does
-o FILEWrite output to FILE (executable or object file)
-cCompile only; produce object files (.o) without linking
-O0, -O1, -O2, -O3, -OsOptimization level: none, minimal, standard, aggressive, or size-optimized
-WallEnable all common warning messages
-WextraEnable extra warning messages beyond -Wall
-gInclude debugging symbols for gdb and other debuggers
-I DIRAdd directory to header search path
-L DIRAdd directory to library search path
-l LIBLink against library libLIB.a or libLIB.so
-std=STANDARDUse C++ standard: c++98, c++11, c++14, c++17, c++20, etc.
-fPICGenerate position-independent code for shared libraries
-EPreprocess only; output preprocessed source to stdout

Examples

Compile and link main.cpp to create executable myapp

clang++ -o myapp main.cpp

Compile myapp.cpp with optimization level 2, warnings enabled, and debug symbols, creating myapp.o

clang++ -c -O2 -Wall -g myapp.cpp

Link object files main.o and utils.o with the C++ standard library and math library to create program

clang++ -o program main.o utils.o -lstdc++ -lm

Compile with C++17 standard, all warnings, optimization level 3, and custom include path

clang++ -std=c++17 -Wall -Wextra -O3 -I/usr/local/include main.cpp -o app

Preprocess preprocess.cpp and display the first 50 lines of expanded source

clang++ -E preprocess.cpp | head -50

Create a shared library (position-independent code required) from mylib.cpp

clang++ -fPIC -shared -o libmylib.so mylib.cpp

Compile with full debug symbols and no optimization for easier debugging

clang++ -g -O0 debug.cpp -o debug_app

Compile all .cpp files in current directory using C++20 standard with warnings enabled

clang++ *.cpp -o program -std=c++20 -Wall

Related commands