clang++(1)
Clang C++ compiler that translates C++ source code into machine-executable binaries.
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
| Flag | What it does |
|---|---|
-o FILE | Write output to FILE (executable or object file) |
-c | Compile only; produce object files (.o) without linking |
-O0, -O1, -O2, -O3, -Os | Optimization level: none, minimal, standard, aggressive, or size-optimized |
-Wall | Enable all common warning messages |
-Wextra | Enable extra warning messages beyond -Wall |
-g | Include debugging symbols for gdb and other debuggers |
-I DIR | Add directory to header search path |
-L DIR | Add directory to library search path |
-l LIB | Link against library libLIB.a or libLIB.so |
-std=STANDARD | Use C++ standard: c++98, c++11, c++14, c++17, c++20, etc. |
-fPIC | Generate position-independent code for shared libraries |
-E | Preprocess only; output preprocessed source to stdout |
Examples
Compile and link main.cpp to create executable myapp
clang++ -o myapp main.cppCompile myapp.cpp with optimization level 2, warnings enabled, and debug symbols, creating myapp.o
clang++ -c -O2 -Wall -g myapp.cppLink 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++ -lmCompile 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 appPreprocess preprocess.cpp and display the first 50 lines of expanded source
clang++ -E preprocess.cpp | head -50Create a shared library (position-independent code required) from mylib.cpp
clang++ -fPIC -shared -o libmylib.so mylib.cppCompile with full debug symbols and no optimization for easier debugging
clang++ -g -O0 debug.cpp -o debug_appCompile all .cpp files in current directory using C++20 standard with warnings enabled
clang++ *.cpp -o program -std=c++20 -Wall