1 clang - the Clang C, C++, and Objective-C compiler
2 ==================================================
7 :program:`clang` [*options*] *filename ...*
12 :program:`clang` is a C, C++, and Objective-C compiler which encompasses
13 preprocessing, parsing, optimization, code generation, assembly, and linking.
14 Depending on which high-level mode setting is passed, Clang will stop before
15 doing a full link. While Clang is highly integrated, it is important to
16 understand the stages of compilation, to understand how to invoke it. These
20 The clang executable is actually a small driver which controls the overall
21 execution of other tools such as the compiler, assembler and linker.
22 Typically you do not need to interact with the driver, but you
23 transparently use it to run the other tools.
26 This stage handles tokenization of the input source file, macro expansion,
27 #include expansion and handling of other preprocessor directives. The
28 output of this stage is typically called a ".i" (for C), ".ii" (for C++),
29 ".mi" (for Objective-C), or ".mii" (for Objective-C++) file.
31 Parsing and Semantic Analysis
32 This stage parses the input file, translating preprocessor tokens into a
33 parse tree. Once in the form of a parse tree, it applies semantic
34 analysis to compute types for expressions as well and determine whether
35 the code is well formed. This stage is responsible for generating most of
36 the compiler warnings as well as parse errors. The output of this stage is
37 an "Abstract Syntax Tree" (AST).
39 Code Generation and Optimization
40 This stage translates an AST into low-level intermediate code (known as
41 "LLVM IR") and ultimately to machine code. This phase is responsible for
42 optimizing the generated code and handling target-specific code generation.
43 The output of this stage is typically called a ".s" file or "assembly" file.
45 Clang also supports the use of an integrated assembler, in which the code
46 generator produces object files directly. This avoids the overhead of
47 generating the ".s" file and of calling the target assembler.
50 This stage runs the target assembler to translate the output of the
51 compiler into a target object file. The output of this stage is typically
52 called a ".o" file or "object" file.
55 This stage runs the target linker to merge multiple object files into an
56 executable or dynamic library. The output of this stage is typically called
57 an "a.out", ".dylib" or ".so" file.
59 :program:`Clang Static Analyzer`
61 The Clang Static Analyzer is a tool that scans source code to try to find bugs
62 through code analysis. This tool uses many parts of Clang and is built into
63 the same driver. Please see <http://clang-analyzer.llvm.org> for more details
64 on how to use the static analyzer.
69 Stage Selection Options
70 ~~~~~~~~~~~~~~~~~~~~~~~
74 Run the preprocessor stage.
76 .. option:: -fsyntax-only
78 Run the preprocessor, parser and type checking stages.
82 Run the previous stages as well as LLVM generation and optimization stages
83 and target-specific code generation, producing an assembly file.
87 Run all of the above, plus the assembler, generating a target ".o" object file.
89 .. option:: no stage selection option
91 If no stage selection option is specified, all stages above are run, and the
92 linker is run to combine the results into an executable or shared library.
94 Language Selection and Mode Options
95 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97 .. option:: -x <language>
99 Treat subsequent input files as having type language.
101 .. option:: -std=<language>
103 Specify the language standard to compile for.
105 .. option:: -stdlib=<library>
107 Specify the C++ standard library to use; supported options are libstdc++ and
108 libc++. If not specified, platform default will be used.
110 .. option:: -rtlib=<library>
112 Specify the compiler runtime library to use; supported options are libgcc and
113 compiler-rt. If not specified, platform default will be used.
119 .. option:: -ObjC, -ObjC++
121 Treat source input files as Objective-C and Object-C++ inputs respectively.
123 .. option:: -trigraphs
127 .. option:: -ffreestanding
129 Indicate that the file should be compiled for a freestanding, not a hosted,
132 .. option:: -fno-builtin
134 Disable special handling and optimizations of builtin functions like
135 :c:func:`strlen` and :c:func:`malloc`.
137 .. option:: -fmath-errno
139 Indicate that math functions should be treated as updating :c:data:`errno`.
141 .. option:: -fpascal-strings
143 Enable support for Pascal-style strings with "\\pfoo".
145 .. option:: -fms-extensions
147 Enable support for Microsoft extensions.
149 .. option:: -fmsc-version=
151 Set _MSC_VER. Defaults to 1300 on Windows. Not set otherwise.
153 .. option:: -fborland-extensions
155 Enable support for Borland extensions.
157 .. option:: -fwritable-strings
159 Make all string literals default to writable. This disables uniquing of
160 strings and other optimizations.
162 .. option:: -flax-vector-conversions
164 Allow loose type checking rules for implicit vector conversions.
168 Enable the "Blocks" language feature.
170 .. option:: -fobjc-abi-version=version
172 Select the Objective-C ABI version to use. Available versions are 1 (legacy
173 "fragile" ABI), 2 (non-fragile ABI 1), and 3 (non-fragile ABI 2).
175 .. option:: -fobjc-nonfragile-abi-version=<version>
177 Select the Objective-C non-fragile ABI version to use by default. This will
178 only be used as the Objective-C ABI when the non-fragile ABI is enabled
179 (either via :option:`-fobjc-nonfragile-abi`, or because it is the platform
182 .. option:: -fobjc-nonfragile-abi, -fno-objc-nonfragile-abi
184 Enable use of the Objective-C non-fragile ABI. On platforms for which this is
185 the default ABI, it can be disabled with :option:`-fno-objc-nonfragile-abi`.
187 Target Selection Options
188 ~~~~~~~~~~~~~~~~~~~~~~~~
190 Clang fully supports cross compilation as an inherent part of its design.
191 Depending on how your version of Clang is configured, it may have support for a
192 number of cross compilers, or may only support a native target.
194 .. option:: -arch <architecture>
196 Specify the architecture to build for.
198 .. option:: -mmacosx-version-min=<version>
200 When building for Mac OS X, specify the minimum version supported by your
203 .. option:: -miphoneos-version-min
205 When building for iPhone OS, specify the minimum version supported by your
208 .. option:: -march=<cpu>
210 Specify that Clang should generate code for a specific processor family
211 member and later. For example, if you specify -march=i486, the compiler is
212 allowed to generate instructions that are valid on i486 and later processors,
213 but which may not exist on earlier ones.
216 Code Generation Options
217 ~~~~~~~~~~~~~~~~~~~~~~~
219 .. option:: -O0, -O1, -O2, -O3, -Ofast, -Os, -Oz, -Og, -O, -O4
221 Specify which optimization level to use:
223 :option:`-O0` Means "no optimization": this level compiles the fastest and
224 generates the most debuggable code.
226 :option:`-O1` Somewhere between :option:`-O0` and :option:`-O2`.
228 :option:`-O2` Moderate level of optimization which enables most
231 :option:`-O3` Like :option:`-O2`, except that it enables optimizations that
232 take longer to perform or that may generate larger code (in an attempt to
233 make the program run faster).
235 :option:`-Ofast` Enables all the optimizations from :option:`-O3` along
236 with other aggressive optimizations that may violate strict compliance with
239 :option:`-Os` Like :option:`-O2` with extra optimizations to reduce code
242 :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
245 :option:`-Og` Like :option:`-O1`. In future versions, this option might
246 disable different optimizations in order to improve debuggability.
248 :option:`-O` Equivalent to :option:`-O2`.
250 :option:`-O4` and higher
252 Currently equivalent to :option:`-O3`
254 .. option:: -g, -gline-tables-only, -gmodules
256 Control debug information output. Note that Clang debug information works
257 best at :option:`-O0`. When more than one option starting with `-g` is
258 specified, the last one wins:
260 :option:`-g` Generate debug information.
262 :option:`-gline-tables-only` Generate only line table debug information. This
263 allows for symbolicated backtraces with inlining information, but does not
264 include any information about variables, their locations or types.
266 :option:`-gmodules` Generate debug information that contains external
267 references to types defined in Clang modules or precompiled headers instead
268 of emitting redundant debug type information into every object file. This
269 option transparently switches the Clang module format to object file
270 containers that hold the Clang module together with the debug information.
271 When compiling a program that uses Clang modules or precompiled headers,
272 this option produces complete debug information with faster compile
273 times and much smaller object files.
275 This option should not be used when building static libraries for
276 distribution to other machines because the debug info will contain
277 references to the module cache on the machine the object files in the
278 library were built on.
280 .. option:: -fstandalone-debug -fno-standalone-debug
282 Clang supports a number of optimizations to reduce the size of debug
283 information in the binary. They work based on the assumption that the
284 debug type information can be spread out over multiple compilation units.
285 For instance, Clang will not emit type definitions for types that are not
286 needed by a module and could be replaced with a forward declaration.
287 Further, Clang will only emit type info for a dynamic C++ class in the
288 module that contains the vtable for the class.
290 The :option:`-fstandalone-debug` option turns off these optimizations.
291 This is useful when working with 3rd-party libraries that don't come with
292 debug information. This is the default on Darwin. Note that Clang will
293 never emit type information for types that are not referenced at all by the
296 .. option:: -fexceptions
298 Enable generation of unwind information. This allows exceptions to be thrown
299 through Clang compiled stack frames. This is on by default in x86-64.
303 Generate code to catch integer overflow errors. Signed integer overflow is
304 undefined in C. With this flag, extra code is generated to detect this and
305 abort when it happens.
307 .. option:: -fvisibility
309 This flag sets the default visibility level.
311 .. option:: -fcommon, -fno-common
313 This flag specifies that variables without initializers get common linkage.
314 It can be disabled with :option:`-fno-common`.
316 .. option:: -ftls-model=<model>
318 Set the default thread-local storage (TLS) model to use for thread-local
319 variables. Valid values are: "global-dynamic", "local-dynamic",
320 "initial-exec" and "local-exec". The default is "global-dynamic". The default
321 model can be overridden with the tls_model attribute. The compiler will try
322 to choose a more efficient model if possible.
324 .. option:: -flto, -flto=full, -flto=thin, -emit-llvm
326 Generate output files in LLVM formats, suitable for link time optimization.
327 When used with :option:`-S` this generates LLVM intermediate language
328 assembly files, otherwise this generates LLVM bitcode format object files
329 (which may be passed to the linker depending on the stage selection options).
331 The default for :option:`-flto` is "full", in which the
332 LLVM bitcode is suitable for monolithic Link Time Optimization (LTO), where
333 the linker merges all such modules into a single combined module for
334 optimization. With "thin", :doc:`ThinLTO <../ThinLTO>`
335 compilation is invoked instead.
342 Print (but do not run) the commands to run for this compilation.
346 Display available options.
348 .. option:: -Qunused-arguments
350 Do not emit any warnings for unused driver arguments.
352 .. option:: -Wa,<args>
354 Pass the comma separated arguments in args to the assembler.
356 .. option:: -Wl,<args>
358 Pass the comma separated arguments in args to the linker.
360 .. option:: -Wp,<args>
362 Pass the comma separated arguments in args to the preprocessor.
364 .. option:: -Xanalyzer <arg>
366 Pass arg to the static analyzer.
368 .. option:: -Xassembler <arg>
370 Pass arg to the assembler.
372 .. option:: -Xlinker <arg>
374 Pass arg to the linker.
376 .. option:: -Xpreprocessor <arg>
378 Pass arg to the preprocessor.
380 .. option:: -o <file>
382 Write output to file.
384 .. option:: -print-file-name=<file>
386 Print the full library path of file.
388 .. option:: -print-libgcc-file-name
390 Print the library path for the currently used compiler runtime library
391 ("libgcc.a" or "libclang_rt.builtins.*.a").
393 .. option:: -print-prog-name=<name>
395 Print the full program path of name.
397 .. option:: -print-search-dirs
399 Print the paths used for finding libraries and programs.
401 .. option:: -save-temps
403 Save intermediate compilation results.
405 .. option:: -save-stats, -save-stats=cwd, -save-stats=obj
407 Save internal code generation (LLVM) statistics to a file in the current
408 directory (:option:`-save-stats`/"-save-stats=cwd") or the directory
409 of the output file ("-save-state=obj").
411 .. option:: -integrated-as, -no-integrated-as
413 Used to enable and disable, respectively, the use of the integrated
414 assembler. Whether the integrated assembler is on by default is target
419 Time individual commands.
421 .. option:: -ftime-report
423 Print timing summary of each stage of compilation.
427 Show commands to run and use verbose output.
433 .. option:: -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
435 These options control how Clang prints out information about diagnostics
436 (errors and warnings). Please see the Clang User's Manual for more information.
441 .. option:: -D<macroname>=<value>
443 Adds an implicit #define into the predefines buffer which is read before the
444 source file is preprocessed.
446 .. option:: -U<macroname>
448 Adds an implicit #undef into the predefines buffer which is read before the
449 source file is preprocessed.
451 .. option:: -include <filename>
453 Adds an implicit #include into the predefines buffer which is read before the
454 source file is preprocessed.
456 .. option:: -I<directory>
458 Add the specified directory to the search path for include files.
460 .. option:: -F<directory>
462 Add the specified directory to the search path for framework include files.
464 .. option:: -nostdinc
466 Do not search the standard system directories or compiler builtin directories
469 .. option:: -nostdlibinc
471 Do not search the standard system directories for include files, but do
472 search compiler builtin include directories.
474 .. option:: -nobuiltininc
476 Do not search clang's builtin directory for include files.
482 .. envvar:: TMPDIR, TEMP, TMP
484 These environment variables are checked, in order, for the location to write
485 temporary files used during the compilation process.
489 If this environment variable is present, it is treated as a delimited list of
490 paths to be added to the default system include path list. The delimiter is
491 the platform dependent delimiter, as used in the PATH environment variable.
493 Empty components in the environment variable are ignored.
495 .. envvar:: C_INCLUDE_PATH, OBJC_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH
497 These environment variables specify additional paths, as for :envvar:`CPATH`, which are
498 only used when processing the appropriate language.
500 .. envvar:: MACOSX_DEPLOYMENT_TARGET
502 If :option:`-mmacosx-version-min` is unspecified, the default deployment
503 target is read from this environment variable. This option only affects
509 To report bugs, please visit <http://llvm.org/bugs/>. Most bug reports should
510 include preprocessed source files (use the :option:`-E` option) and the full
511 output of the compiler, along with information to reproduce.
516 :manpage:`as(1)`, :manpage:`ld(1)`