CLANG(1)
โจ 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.oobject 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 1990iso9899:199409โ ISO C 1990 with amendment 1gnu89,gnu90โ ISO C 1990 with GNU extensionsc99,iso9899:1999โ ISO C 1999gnu99โ ISO C 1999 with GNU extensionsc11,iso9899:2011โ ISO C 2011gnu11โ ISO C 2011 with GNU extensionsc17,iso9899:2017โ ISO C 2017gnu17โ 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 amendmentsgnu++98,gnu++03โ ISO C++ 1998 with amendments and GNU extensionsc++11โ ISO C++ 2011 with amendmentsgnu++11โ ISO C++ 2011 with amendments and GNU extensionsc++14โ ISO C++ 2014 with amendmentsgnu++14โ ISO C++ 2014 with amendments and GNU extensionsc++17โ ISO C++ 2017 with amendmentsgnu++17โ ISO C++ 2017 with amendments and GNU extensionsc++2aโ working draft for ISO C++ 2020gnu++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.0cl1.1โ OpenCL 1.1cl1.2โ OpenCL 1.2cl2.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 arelibstdc++andlibc++. if not specified, the platform default will be used.-rtlib=<library>โ specify the compiler runtime library to use; supported options arelibgccandcompiler-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 providememcpy,memmove,memsetandmemcmpimplementations, as these are needed for efficient codegen for many programs. ๐ง-fno-builtinโ disable special handling and optimizations of builtin functions likestrlen()andmalloc().-fmath-errnoโ indicate that math functions should be treated as updatingerrno.-fpascal-stringsโ enable support for Pascal-style strings with"\pfoo".-fms-extensionsโ enable support for Microsoft extensions.-fmsc-version=โ set_MSC_VER. defaults to1300on 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 vectorsinteger: allow implicit bitcasts between integer vectors of the same overall bit-widthall: 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 are1(legacy "fragile" ABI),2(non-fragile ABI 1), and3(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-O0and-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-O3along with other aggressive optimizations that may violate strict compliance with language standards. she's reckless and she knows it ๐-Osโ like-O2with extra optimizations to reduce code size.-Ozโ like-Os(and thus-O2), but reduces code size further.
-Ogโ this one's basically-O1coded ๐ 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.-O4and 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-execandlocal-exec. the default isglobal-dynamic. you can override the default model with thetls_modelattribute. 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-Sthis 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-fltotogether with-gand you compile and link in separate steps, you also need to pass-Wl,-object_path_lto,<lto-filename>.oat the linking step to tell theld64linker 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.dSYMbundle usingdsymutil(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.aorlibclang_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#defineinto the predefines buffer which is read before the source file is preprocessed.-U<macroname>โ adds an implicit#undefinto the predefines buffer which is read before the source file is preprocessed.-include <filename>โ adds an implicit#includeinto 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 thePATHenvironment 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 forCPATH, 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 ๐
๐ AUTHOR ๐
Maintained by the Clang / LLVM Team (http://clang.llvm.org)
๐ COPYRIGHT ๐
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
- ubuntu jammy: clang-13 1:13.0.1-2ubuntu2.2 ยท APACHE-2-LLVM-EXCEPTIONS
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms