CLANG(1)

๐Ÿ“ฆ
version 13.0.1 ยท 3 versions
, in ubuntu jammy

โœจ clang โ€” the Clang C, C++, and Objective-C compiler โœจ

okay bestie, sit down, hydrate, and let's talk about clang ๐Ÿ’– this page is a whole journey so buckle up.


๐Ÿ“› NAME

clang โ€” the Clang C, C++, and Objective-C compiler. that's her. that's the whole name. iconic. ๐Ÿ‘‘

๐Ÿ“‹ SYNOPSIS

clang [options] filename โ€ฆ

๐Ÿฆ‹ DESCRIPTION

clang is a C, C++, and Objective-C compiler, and she does EVERYTHING: preprocessing, parsing, optimization, code generation, assembly, and linking. depending on which high-level mode setting you pass, Clang will stop before doing a full link โ€” like she'll do her part and hand off, no drama ๐Ÿ’…

and yes, she's highly integrated, which is slay, but you still gotta understand the stages of compilation to understand how to invoke her. so here are the stages, in order, no skipping:

๐Ÿš— Driver

the clang executable is actually a small driver โ€” she's the manager, not the intern โ€” and she controls the overall execution of the other tools: the compiler, the assembler, and the linker. typically you don't need to interact with the driver directly, you just transparently use it to run all those other tools. she's giving main character energy while letting the side characters cook. ๐Ÿซถ

๐Ÿงš Preprocessing

this stage handles tokenization of the input source file, macro expansion, #include expansion, and handling of other preprocessor directives. the output of this stage is typically called a .i file (for C), a .ii file (for C++), a .mi file (for Objective-C), or a .mii file (for Objective-C++). cute little extensions, love that for them ๐ŸŒธ

๐ŸŒณ Parsing and Semantic Analysis

this stage parses the input file, translating preprocessor tokens into a parse tree. once it's in the form of a parse tree, it applies semantic analysis to compute types for expressions as well and determine whether the code is well formed. this stage is responsible for generating most of the compiler warnings as well as parse errors โ€” so yes, this is the stage that's gonna roast your code, but lovingly. the output of this stage is an "Abstract Syntax Tree" (AST). โœจ

โšก Code Generation and Optimization

this stage translates an AST into low-level intermediate code (known as "LLVM IR") and ultimately to machine code. this phase is responsible for optimizing the generated code and handling target-specific code generation. the output of this stage is typically called a .s file or "assembly" file.

Clang also supports the use of an integrated assembler, in which the code generator produces object files directly. this avoids the overhead of generating the .s file and of calling the target assembler. efficiency? we love her. ๐Ÿ”ฅ

๐Ÿ”ง Assembler

this stage runs the target assembler to translate the output of the compiler into a target object file. the output of this stage is typically called a .o file or "object" file. ๐Ÿ“

๐Ÿ”— Linker

this stage runs the target linker to merge multiple object files into an executable or dynamic library. the output of this stage is typically called an a.out, .dylib, or .so file. boom, you have a program, periodt. ๐Ÿ’œ

๐Ÿ” Clang Static Analyzer

the Clang Static Analyzer is a tool that scans source code to try to find bugs through code analysis. this tool uses many parts of Clang and is built into the same driver. please see <https://clang-analyzer.llvm.org> for more details on how to use the static analyzer. she's the friend who reads your code and goes "bestieโ€ฆ what is this" but in a helpful way ๐Ÿ™ˆ


โš™๏ธ OPTIONS

๐ŸŽฏ Stage Selection Options

  • -E โ€” run the preprocessor stage.
  • -fsyntax-only โ€” run the preprocessor, parser, and type checking stages.
  • -S โ€” run the previous stages as well as LLVM generation and optimization stages and target-specific code generation, producing an assembly file.
  • -c โ€” run all of the above, plus the assembler, generating a target .o object file.
  • no stage selection option โ€” if you don't specify any stage selection option, all stages above get run, and the linker is run to combine the results into an executable or shared library.

๐Ÿ—ฃ๏ธ Language Selection and Mode Options

  • -x <language> โ€” treat subsequent input files as having type language.
  • -std= <standard> โ€” specify the language standard to compile for.

supported values for the C language are:

  • c89, c90, iso9899:1990 โ€” ISO C 1990
  • iso9899:199409 โ€” ISO C 1990 with amendment 1
  • gnu89, gnu90 โ€” ISO C 1990 with GNU extensions
  • c99, iso9899:1999 โ€” ISO C 1999
  • gnu99 โ€” ISO C 1999 with GNU extensions
  • c11, iso9899:2011 โ€” ISO C 2011
  • gnu11 โ€” ISO C 2011 with GNU extensions
  • c17, iso9899:2017 โ€” ISO C 2017
  • gnu17 โ€” ISO C 2017 with GNU extensions

the default C language standard is gnu17, except on PS4, where it is gnu99. (yes, really. PS4 said "not today." ๐ŸŽฎ)

supported values for the C++ language are:

  • c++98, c++03 โ€” ISO C++ 1998 with amendments
  • gnu++98, gnu++03 โ€” ISO C++ 1998 with amendments and GNU extensions
  • c++11 โ€” ISO C++ 2011 with amendments
  • gnu++11 โ€” ISO C++ 2011 with amendments and GNU extensions
  • c++14 โ€” ISO C++ 2014 with amendments
  • gnu++14 โ€” ISO C++ 2014 with amendments and GNU extensions
  • c++17 โ€” ISO C++ 2017 with amendments
  • gnu++17 โ€” ISO C++ 2017 with amendments and GNU extensions
  • c++2a โ€” working draft for ISO C++ 2020
  • gnu++2a โ€” working draft for ISO C++ 2020 with GNU extensions

the default C++ language standard is gnu++14.

supported values for the OpenCL language are:

  • cl1.0 โ€” OpenCL 1.0
  • cl1.1 โ€” OpenCL 1.1
  • cl1.2 โ€” OpenCL 1.2
  • cl2.0 โ€” OpenCL 2.0

the default OpenCL language standard is cl1.0.

supported values for the CUDA language are:

  • cuda โ€” NVIDIA CUDA(tm)
  • -stdlib= <library> โ€” specify the C++ standard library to use; supported options are libstdc++ and libc++. if not specified, the platform default will be used.
  • -rtlib= <library> โ€” specify the compiler runtime library to use; supported options are libgcc and compiler-rt. if not specified, the platform default will be used.
  • -ansi โ€” same as -std=c89.
  • -ObjC, -ObjC++ โ€” treat source input files as Objective-C and Object-C++ inputs respectively.
  • -trigraphs โ€” enable trigraphs.
  • -ffreestanding โ€” indicate that the file should be compiled for a freestanding, not a hosted, environment. note that it is assumed that a freestanding environment will additionally provide memcpy, memmove, memset and memcmp implementations, as these are needed for efficient codegen for many programs. ๐Ÿง 
  • -fno-builtin โ€” disable special handling and optimizations of builtin functions like strlen() and malloc().
  • -fmath-errno โ€” indicate that math functions should be treated as updating errno.
  • -fpascal-strings โ€” enable support for Pascal-style strings with "\pfoo".
  • -fms-extensions โ€” enable support for Microsoft extensions.
  • -fmsc-version= โ€” set _MSC_VER. defaults to 1300 on Windows. not set otherwise.
  • -fborland-extensions โ€” enable support for Borland extensions.
  • -fwritable-strings โ€” make all string literals default to writable. this disables uniquing of strings and other optimizations. โš ๏ธ
  • -flax-vector-conversions, -flax-vector-conversions= <kind>, -fno-lax-vector-conversions โ€” allow loose type checking rules for implicit vector conversions. possible values of <kind>:
    • none: allow no implicit conversions between vectors
    • integer: allow implicit bitcasts between integer vectors of the same overall bit-width
    • all: allow implicit bitcasts between any vectors of the same overall bit-width

<kind> defaults to integer if unspecified.

  • -fblocks โ€” enable the "Blocks" language feature.
  • -fobjc-abi-version= version โ€” select the Objective-C ABI version to use. available versions are 1 (legacy "fragile" ABI), 2 (non-fragile ABI 1), and 3 (non-fragile ABI 2).
  • -fobjc-nonfragile-abi-version= <version> โ€” select the Objective-C non-fragile ABI version to use by default. this will only be used as the Objective-C ABI when the non-fragile ABI is enabled (either via -fobjc-nonfragile-abi, or because it is the platform default).
  • -fobjc-nonfragile-abi, -fno-objc-nonfragile-abi โ€” enable use of the Objective-C non-fragile ABI. on platforms for which this is the default ABI, it can be disabled with -fno-objc-nonfragile-abi.

๐ŸŽฏ Target Selection Options

Clang fully supports cross compilation as an inherent part of its design. depending on how your version of Clang is configured, it may have support for a number of cross compilers, or may only support a native target. either way, she's flexible ๐Ÿ’…

  • -target <architecture> โ€” specify the architecture to build for.
  • --print-supported-cpus โ€” print out a list of supported processors for the given target (specified through --target=<architecture> or -arch <architecture>). if no target is specified, the system default target will be used.
  • -mcpu=?, -mtune=? โ€” acts as an alias for --print-supported-cpus.
  • -march= <cpu> โ€” specify that Clang should generate code for a specific processor family member and later. for example, if you specify -march=i486, the compiler is allowed to generate instructions that are valid on i486 and later processors, but which may not exist on earlier ones. ๐Ÿ‘‘

๐Ÿ—๏ธ Code Generation Options

  • -O0, -O1, -O2, -O3, -Ofast, -Os, -Oz, -Og, -O, -O4 โ€” specify which optimization level to use:
    • -O0 โ€” means "no optimization": this level compiles the fastest and generates the most debuggable code. ๐Ÿข
    • -O1 โ€” somewhere between -O0 and -O2.
    • -O2 โ€” moderate level of optimization which enables most optimizations.
    • -O3 โ€” like -O2, except that it enables optimizations that take longer to perform or that may generate larger code (in an attempt to make the program run faster). ๐Ÿ”ฅ
    • -Ofast โ€” enables all the optimizations from -O3 along with other aggressive optimizations that may violate strict compliance with language standards. she's reckless and she knows it ๐Ÿ’€
    • -Os โ€” like -O2 with extra optimizations to reduce code size.
    • -Oz โ€” like -Os (and thus -O2), but reduces code size further.
  • -Og โ€” this one's basically -O1 coded ๐Ÿ’– real talk though: in future versions it might disable different optimizations specifically to make debugging easier, so don't be shook if the vibes shift later.
  • -O โ€” literally equivalent to -O1. that's it. that's the whole thing. periodt.
  • -O4 and higher โ€” right now these are currently equivalent to -O3. they're not doing anything extra, bestie, they're just standing there looking pretty ๐Ÿ‘‘

  • -g, -gline-tables-only, -gmodules โ€” these control the debug information output ๐Ÿ›โœจ important note: Clang debug info works best at -O0. and when you pass more than one option starting with -g, the last one wins (so order matters, no cap):
    • -g โ€” generate debug information. the classic.
    • -gline-tables-only โ€” generate only line table debug information. this gives you symbolicated backtraces with inlining information, but it does not include any info about variables, their locations, or their types. so cute for stack traces, useless for inspecting your vars ๐Ÿ™ˆ
    • -gmodules โ€” generate debug information that contains external references to types defined in Clang modules or precompiled headers, instead of emitting redundant debug type info into every object file. this option transparently switches the Clang module format to object file containers that hold the Clang module together with the debug information. when you compile a program that uses Clang modules or precompiled headers, this option produces complete debug information with faster compile times and much smaller object files ๐Ÿ”ฅ
โš ๏ธ do not use this one when you're building static libraries for distribution to other machines, because the debug info will contain references to the module cache on the machine where the library's object files were built. it's giving "works on my machine" energy and we do not want that ๐Ÿ’€
  • -fstandalone-debug, -fno-standalone-debug โ€” so Clang supports a bunch of optimizations to shrink the size of debug information in the binary. they work on the assumption that debug type information can be spread out over multiple compilation units. for instance, Clang won't emit type definitions for types that aren't needed by a module and could be replaced with a forward declaration. further, Clang will only emit type info for a dynamic C++ class in the module that contains the vtable for that class.

The -fstandalone-debug option turns off these optimizations โœจ super useful when you're working with 3rd-party libraries that don't come with debug information. this is the default on Darwin. note that Clang will never emit type information for types that aren't referenced at all by the program โ€” no notes taken, no type info given ๐Ÿ’…

  • -feliminate-unused-debug-types โ€” by default, Clang does not emit type information for types that are defined but not used in a program. if you want to retain the debug info for these unused types, you can use the negation -fno-eliminate-unused-debug-types ๐Ÿซถ
  • -fexceptions โ€” enable generation of unwind information. this lets exceptions be thrown through Clang-compiled stack frames. it's on by default in x86-64 โญ
  • -ftrapv โ€” generate code to catch integer overflow errors. signed integer overflow is undefined in C, so with this flag extra code gets generated to detect it and abort when it happens. dramatic but necessary ๐Ÿ˜ญ
  • -fvisibility โ€” this flag sets the default visibility level ๐Ÿ‘€
  • -fcommon, -fno-common โ€” this flag specifies that variables without initializers get common linkage. you can turn it off with -fno-common.
  • -ftls-model=<model> โ€” set the default thread-local storage (TLS) model to use for thread-local variables. valid values are: global-dynamic, local-dynamic, initial-exec and local-exec. the default is global-dynamic. you can override the default model with the tls_model attribute. the compiler will try to choose a more efficient model if it possibly can โœจ
  • -flto, -flto=full, -flto=thin, -emit-llvm โ€” generate output files in LLVM formats, suitable for link time optimization. when used with -S this generates LLVM intermediate language assembly files; otherwise it generates LLVM bitcode format object files (which may be passed to the linker depending on the stage selection options) ๐Ÿฆ‹

The default for -flto is full, where the LLVM bitcode is suitable for monolithic Link Time Optimization (LTO), and the linker merges all such modules into a single combined module for optimization. with thin, ThinLTO compilation gets invoked instead.

NOTE: on Darwin, when you use -flto together with -g and you compile and link in separate steps, you also need to pass -Wl,-object_path_lto,<lto-filename>.o at the linking step to tell the ld64 linker not to delete the temporary object file generated during Link Time Optimization. (this flag is automatically passed to the linker by Clang if compilation and linking are done in a single step.) this lets you debug the executable as well as generate the .dSYM bundle using dsymutil(1). bestie this is a whole saga but it's important ๐ŸŒธ

๐Ÿš— Driver Options ๐Ÿš—

  • -### โ€” print (but do not run) the commands that would run for this compilation. it's just vibes, no execution ๐Ÿ™ˆ
  • --help โ€” display available options. your lil cheat sheet โœจ
  • -Qunused-arguments โ€” do not emit any warnings for unused driver arguments. silence is golden ๐Ÿ’…
  • -Wa,<args> โ€” pass the comma separated arguments in args to the assembler.
  • -Wl,<args> โ€” pass the comma separated arguments in args to the linker.
  • -Wp,<args> โ€” pass the comma separated arguments in args to the preprocessor.
  • -Xanalyzer <arg> โ€” pass arg to the static analyzer ๐Ÿ”
  • -Xassembler <arg> โ€” pass arg to the assembler.
  • -Xlinker <arg> โ€” pass arg to the linker.
  • -Xpreprocessor <arg> โ€” pass arg to the preprocessor.
  • -o <file> โ€” write output to file.
  • -print-file-name=<file> โ€” print the full library path of file.
  • -print-libgcc-file-name โ€” print the library path for the currently used compiler runtime library (libgcc.a or libclang_rt.builtins.*.a).
  • -print-prog-name=<name> โ€” print the full program path of name.
  • -print-search-dirs โ€” print the paths used for finding libraries and programs ๐Ÿ—บ๏ธ
  • -save-temps โ€” save intermediate compilation results.
  • -save-stats, -save-stats=cwd, -save-stats=obj โ€” save internal code generation (LLVM) statistics to a file in the current directory (-save-stats/-save-stats=cwd) or the directory of the output file (-save-state=obj). ๐Ÿ“Š
  • -integrated-as, -no-integrated-as โ€” used to enable and disable, respectively, the use of the integrated assembler. whether the integrated assembler is on by default is target dependent ๐Ÿ‘‘
  • -time โ€” time individual commands. clock it, bestie โฑ๏ธ
  • -ftime-report โ€” print a timing summary of each stage of compilation.
  • -v โ€” show commands to run and use verbose output. the yapper mode ๐Ÿ—ฃ๏ธ

๐Ÿฉบ Diagnostics Options ๐Ÿฉบ

  • -fshow-column, -fshow-source-location, -fcaret-diagnostics, -fdiagnostics-fixit-info, -fdiagnostics-parseable-fixits, -fdiagnostics-print-source-range-info, -fprint-source-range-info, -fdiagnostics-show-option, -fmessage-length โ€” these options control how Clang prints out information about diagnostics (errors and warnings). please see the Clang User's Manual for more information ๐Ÿ“–

๐Ÿงช Preprocessor Options ๐Ÿงช

  • -D<macroname>=<value> โ€” adds an implicit #define into the predefines buffer which is read before the source file is preprocessed.
  • -U<macroname> โ€” adds an implicit #undef into the predefines buffer which is read before the source file is preprocessed.
  • -include <filename> โ€” adds an implicit #include into the predefines buffer which is read before the source file is preprocessed.
  • -I<directory> โ€” add the specified directory to the search path for include files.
  • -F<directory> โ€” add the specified directory to the search path for framework include files.
  • -nostdinc โ€” do not search the standard system directories or compiler builtin directories for include files.
  • -nostdlibinc โ€” do not search the standard system directories for include files, but do search compiler builtin include directories.
  • -nobuiltininc โ€” do not search clang's builtin directory for include files.

๐ŸŒ ENVIRONMENT ๐ŸŒ

  • TMPDIR, TEMP, TMP โ€” these environment variables are checked, in order, for the location to write temporary files used during the compilation process. first one that hits, wins ๐Ÿ’…
  • CPATH โ€” if this environment variable is present, it's treated as a delimited list of paths to be added to the default system include path list. the delimiter is the platform dependent delimiter, as used in the PATH environment variable.

Empty components in the environment variable are ignored ๐Ÿซถ

  • C_INCLUDE_PATH, OBJC_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH โ€” these environment variables specify additional paths, as for CPATH, which are only used when processing the appropriate language. language-specific besties only โœจ

๐Ÿ› BUGS ๐Ÿ›

To report bugs, please visit <https://bugs.llvm.org/>. most bug reports should include preprocessed source files (use the -E option) and the full output of the compiler, along with information to reproduce. give them the receipts, bestie ๐Ÿงพ


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

as(1), ld(1)


๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

Maintained by the Clang / LLVM Team (http://clang.llvm.org)


2007-2023, The Clang Team


and that's a wrap on CLANG(1) ๐ŸŽ€ page 13, dated Jul 14, 2023 โ€” go forth and compile, my icon, you absolutely ate this whole manual ๐ŸŒˆโœจ๐Ÿ’–

๐Ÿ“ฆ where this page comes from