blob: f511022dd77ef5aafbd20720f5cee5ab85dce831 [file] [log] [blame]
Andrew Wilkins6238c6f2015-06-30 02:52:38 +00001clang - the Clang C, C++, and Objective-C compiler
2==================================================
3
4SYNOPSIS
5--------
6
7:program:`clang` [*options*] *filename ...*
8
9DESCRIPTION
10-----------
11
12:program:`clang` is a C, C++, and Objective-C compiler which encompasses
13preprocessing, parsing, optimization, code generation, assembly, and linking.
14Depending on which high-level mode setting is passed, Clang will stop before
15doing a full link. While Clang is highly integrated, it is important to
16understand the stages of compilation, to understand how to invoke it. These
17stages are:
18
19Driver
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.
24
25Preprocessing
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.
30
31Parsing 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).
38
39Code 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.
44
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.
48
49Assembler
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.
53
54Linker
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.
58
59:program:`Clang Static Analyzer`
60
61The Clang Static Analyzer is a tool that scans source code to try to find bugs
62through code analysis. This tool uses many parts of Clang and is built into
Sylvestre Ledrubc5c3f52018-11-04 17:02:00 +000063the same driver. Please see <https://clang-analyzer.llvm.org> for more details
Andrew Wilkins6238c6f2015-06-30 02:52:38 +000064on how to use the static analyzer.
65
66OPTIONS
67-------
68
69Stage Selection Options
70~~~~~~~~~~~~~~~~~~~~~~~
71
72.. option:: -E
73
74 Run the preprocessor stage.
75
76.. option:: -fsyntax-only
77
78 Run the preprocessor, parser and type checking stages.
79
80.. option:: -S
81
82 Run the previous stages as well as LLVM generation and optimization stages
83 and target-specific code generation, producing an assembly file.
84
85.. option:: -c
86
87 Run all of the above, plus the assembler, generating a target ".o" object file.
88
89.. option:: no stage selection option
90
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.
93
94Language Selection and Mode Options
95~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96
97.. option:: -x <language>
98
99 Treat subsequent input files as having type language.
100
Dimitry Andricf6c76532018-04-11 17:21:52 +0000101.. option:: -std=<standard>
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000102
103 Specify the language standard to compile for.
104
Dimitry Andricf6c76532018-04-11 17:21:52 +0000105 Supported values for the C language are:
106
107 | ``c89``
108 | ``c90``
109 | ``iso9899:1990``
110
111 ISO C 1990
112
113 | ``iso9899:199409``
114
115 ISO C 1990 with amendment 1
116
117 | ``gnu89``
118 | ``gnu90``
119
120 ISO C 1990 with GNU extensions
121
122 | ``c99``
123 | ``iso9899:1999``
124
125 ISO C 1999
126
127 | ``gnu99``
128
129 ISO C 1999 with GNU extensions
130
131 | ``c11``
132 | ``iso9899:2011``
133
134 ISO C 2011
135
136 | ``gnu11``
137
138 ISO C 2011 with GNU extensions
139
140 | ``c17``
141 | ``iso9899:2017``
142
143 ISO C 2017
144
145 | ``gnu17``
146
147 ISO C 2017 with GNU extensions
148
149 The default C language standard is ``gnu11``, except on PS4, where it is
150 ``gnu99``.
151
152 Supported values for the C++ language are:
153
154 | ``c++98``
155 | ``c++03``
156
157 ISO C++ 1998 with amendments
158
159 | ``gnu++98``
160 | ``gnu++03``
161
162 ISO C++ 1998 with amendments and GNU extensions
163
164 | ``c++11``
165
166 ISO C++ 2011 with amendments
167
168 | ``gnu++11``
169
170 ISO C++ 2011 with amendments and GNU extensions
171
172 | ``c++14``
173
174 ISO C++ 2014 with amendments
175
176 | ``gnu++14``
177
178 ISO C++ 2014 with amendments and GNU extensions
179
180 | ``c++17``
181
182 ISO C++ 2017 with amendments
183
184 | ``gnu++17``
185
186 ISO C++ 2017 with amendments and GNU extensions
187
188 | ``c++2a``
189
190 Working draft for ISO C++ 2020
191
192 | ``gnu++2a``
193
194 Working draft for ISO C++ 2020 with GNU extensions
195
196 The default C++ language standard is ``gnu++14``.
197
198 Supported values for the OpenCL language are:
199
200 | ``cl1.0``
201
202 OpenCL 1.0
203
204 | ``cl1.1``
205
206 OpenCL 1.1
207
208 | ``cl1.2``
209
210 OpenCL 1.2
211
212 | ``cl2.0``
213
214 OpenCL 2.0
215
216 The default OpenCL language standard is ``cl1.0``.
217
218 Supported values for the CUDA language are:
219
220 | ``cuda``
221
222 NVIDIA CUDA(tm)
223
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000224.. option:: -stdlib=<library>
225
226 Specify the C++ standard library to use; supported options are libstdc++ and
Jonas Hahnfeld79e00932016-09-14 05:52:21 +0000227 libc++. If not specified, platform default will be used.
228
229.. option:: -rtlib=<library>
230
231 Specify the compiler runtime library to use; supported options are libgcc and
232 compiler-rt. If not specified, platform default will be used.
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000233
234.. option:: -ansi
235
236 Same as -std=c89.
237
238.. option:: -ObjC, -ObjC++
239
240 Treat source input files as Objective-C and Object-C++ inputs respectively.
241
242.. option:: -trigraphs
243
244 Enable trigraphs.
245
246.. option:: -ffreestanding
247
248 Indicate that the file should be compiled for a freestanding, not a hosted,
249 environment.
250
251.. option:: -fno-builtin
252
253 Disable special handling and optimizations of builtin functions like
254 :c:func:`strlen` and :c:func:`malloc`.
255
256.. option:: -fmath-errno
257
258 Indicate that math functions should be treated as updating :c:data:`errno`.
259
260.. option:: -fpascal-strings
261
262 Enable support for Pascal-style strings with "\\pfoo".
263
264.. option:: -fms-extensions
265
266 Enable support for Microsoft extensions.
267
268.. option:: -fmsc-version=
269
270 Set _MSC_VER. Defaults to 1300 on Windows. Not set otherwise.
271
272.. option:: -fborland-extensions
273
274 Enable support for Borland extensions.
275
276.. option:: -fwritable-strings
277
278 Make all string literals default to writable. This disables uniquing of
279 strings and other optimizations.
280
281.. option:: -flax-vector-conversions
282
283 Allow loose type checking rules for implicit vector conversions.
284
285.. option:: -fblocks
286
287 Enable the "Blocks" language feature.
288
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000289.. option:: -fobjc-abi-version=version
290
291 Select the Objective-C ABI version to use. Available versions are 1 (legacy
292 "fragile" ABI), 2 (non-fragile ABI 1), and 3 (non-fragile ABI 2).
293
294.. option:: -fobjc-nonfragile-abi-version=<version>
295
296 Select the Objective-C non-fragile ABI version to use by default. This will
297 only be used as the Objective-C ABI when the non-fragile ABI is enabled
298 (either via :option:`-fobjc-nonfragile-abi`, or because it is the platform
299 default).
300
Adrian Prantl6e7300b2016-07-11 17:03:13 +0000301.. option:: -fobjc-nonfragile-abi, -fno-objc-nonfragile-abi
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000302
303 Enable use of the Objective-C non-fragile ABI. On platforms for which this is
304 the default ABI, it can be disabled with :option:`-fno-objc-nonfragile-abi`.
305
306Target Selection Options
307~~~~~~~~~~~~~~~~~~~~~~~~
308
309Clang fully supports cross compilation as an inherent part of its design.
310Depending on how your version of Clang is configured, it may have support for a
311number of cross compilers, or may only support a native target.
312
313.. option:: -arch <architecture>
314
315 Specify the architecture to build for.
316
317.. option:: -mmacosx-version-min=<version>
318
J. Ryan Stinnettd45eaf92019-05-30 16:46:22 +0000319 When building for macOS, specify the minimum version supported by your
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000320 application.
321
322.. option:: -miphoneos-version-min
323
324 When building for iPhone OS, specify the minimum version supported by your
325 application.
326
Ziang Wanaf857b92019-06-14 21:42:21 +0000327.. option:: --print-supported-cpus
328
Ziang Wan9a2e7782019-06-14 23:34:40 +0000329 Print out a list of supported processors for the given target (specified
330 through --target=<architecture> or -arch <architecture>). If no target is
Ziang Wanaf857b92019-06-14 21:42:21 +0000331 specified, the system default target will be used.
332
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000333.. option:: -march=<cpu>
334
335 Specify that Clang should generate code for a specific processor family
336 member and later. For example, if you specify -march=i486, the compiler is
337 allowed to generate instructions that are valid on i486 and later processors,
338 but which may not exist on earlier ones.
339
340
341Code Generation Options
342~~~~~~~~~~~~~~~~~~~~~~~
343
Sylvestre Ledrud340ccc2016-11-11 17:29:56 +0000344.. option:: -O0, -O1, -O2, -O3, -Ofast, -Os, -Oz, -Og, -O, -O4
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000345
346 Specify which optimization level to use:
347
348 :option:`-O0` Means "no optimization": this level compiles the fastest and
349 generates the most debuggable code.
350
351 :option:`-O1` Somewhere between :option:`-O0` and :option:`-O2`.
352
353 :option:`-O2` Moderate level of optimization which enables most
354 optimizations.
355
356 :option:`-O3` Like :option:`-O2`, except that it enables optimizations that
357 take longer to perform or that may generate larger code (in an attempt to
358 make the program run faster).
359
360 :option:`-Ofast` Enables all the optimizations from :option:`-O3` along
361 with other aggressive optimizations that may violate strict compliance with
362 language standards.
363
364 :option:`-Os` Like :option:`-O2` with extra optimizations to reduce code
365 size.
366
367 :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
368 size further.
369
Sylvestre Ledrubc5c3f52018-11-04 17:02:00 +0000370 :option:`-Og` Like :option:`-O1`. In future versions, this option might
Sylvestre Ledrud340ccc2016-11-11 17:29:56 +0000371 disable different optimizations in order to improve debuggability.
372
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000373 :option:`-O` Equivalent to :option:`-O2`.
374
375 :option:`-O4` and higher
376
377 Currently equivalent to :option:`-O3`
378
Adrian Prantl59a6e242016-07-11 17:03:16 +0000379.. option:: -g, -gline-tables-only, -gmodules
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000380
Adrian Prantl59a6e242016-07-11 17:03:16 +0000381 Control debug information output. Note that Clang debug information works
382 best at :option:`-O0`. When more than one option starting with `-g` is
383 specified, the last one wins:
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000384
Adrian Prantl59a6e242016-07-11 17:03:16 +0000385 :option:`-g` Generate debug information.
Adrian Prantl6b21ab22015-08-27 19:46:20 +0000386
Adrian Prantl59a6e242016-07-11 17:03:16 +0000387 :option:`-gline-tables-only` Generate only line table debug information. This
388 allows for symbolicated backtraces with inlining information, but does not
389 include any information about variables, their locations or types.
Adrian Prantla7f56ab2015-12-22 22:37:22 +0000390
Adrian Prantl59a6e242016-07-11 17:03:16 +0000391 :option:`-gmodules` Generate debug information that contains external
392 references to types defined in Clang modules or precompiled headers instead
393 of emitting redundant debug type information into every object file. This
394 option transparently switches the Clang module format to object file
395 containers that hold the Clang module together with the debug information.
396 When compiling a program that uses Clang modules or precompiled headers,
397 this option produces complete debug information with faster compile
398 times and much smaller object files.
399
400 This option should not be used when building static libraries for
401 distribution to other machines because the debug info will contain
402 references to the module cache on the machine the object files in the
403 library were built on.
404
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000405.. option:: -fstandalone-debug -fno-standalone-debug
406
407 Clang supports a number of optimizations to reduce the size of debug
408 information in the binary. They work based on the assumption that the
409 debug type information can be spread out over multiple compilation units.
410 For instance, Clang will not emit type definitions for types that are not
411 needed by a module and could be replaced with a forward declaration.
412 Further, Clang will only emit type info for a dynamic C++ class in the
413 module that contains the vtable for the class.
414
415 The :option:`-fstandalone-debug` option turns off these optimizations.
416 This is useful when working with 3rd-party libraries that don't come with
417 debug information. This is the default on Darwin. Note that Clang will
418 never emit type information for types that are not referenced at all by the
419 program.
420
421.. option:: -fexceptions
422
423 Enable generation of unwind information. This allows exceptions to be thrown
424 through Clang compiled stack frames. This is on by default in x86-64.
425
426.. option:: -ftrapv
427
428 Generate code to catch integer overflow errors. Signed integer overflow is
429 undefined in C. With this flag, extra code is generated to detect this and
430 abort when it happens.
431
432.. option:: -fvisibility
433
434 This flag sets the default visibility level.
435
Adrian Prantl6e7300b2016-07-11 17:03:13 +0000436.. option:: -fcommon, -fno-common
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000437
438 This flag specifies that variables without initializers get common linkage.
439 It can be disabled with :option:`-fno-common`.
440
441.. option:: -ftls-model=<model>
442
443 Set the default thread-local storage (TLS) model to use for thread-local
444 variables. Valid values are: "global-dynamic", "local-dynamic",
445 "initial-exec" and "local-exec". The default is "global-dynamic". The default
446 model can be overridden with the tls_model attribute. The compiler will try
447 to choose a more efficient model if possible.
448
Aaron Ballman9bd12f4b2016-09-22 12:15:18 +0000449.. option:: -flto, -flto=full, -flto=thin, -emit-llvm
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000450
451 Generate output files in LLVM formats, suitable for link time optimization.
452 When used with :option:`-S` this generates LLVM intermediate language
453 assembly files, otherwise this generates LLVM bitcode format object files
454 (which may be passed to the linker depending on the stage selection options).
455
Teresa Johnson9e052662016-09-22 13:41:10 +0000456 The default for :option:`-flto` is "full", in which the
Teresa Johnson8de63462016-09-21 16:57:03 +0000457 LLVM bitcode is suitable for monolithic Link Time Optimization (LTO), where
458 the linker merges all such modules into a single combined module for
Teresa Johnson14142bf2016-09-22 13:58:33 +0000459 optimization. With "thin", :doc:`ThinLTO <../ThinLTO>`
Teresa Johnson8de63462016-09-21 16:57:03 +0000460 compilation is invoked instead.
461
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000462Driver Options
463~~~~~~~~~~~~~~
464
465.. option:: -###
466
467 Print (but do not run) the commands to run for this compilation.
468
469.. option:: --help
470
471 Display available options.
472
473.. option:: -Qunused-arguments
474
475 Do not emit any warnings for unused driver arguments.
476
477.. option:: -Wa,<args>
478
479 Pass the comma separated arguments in args to the assembler.
480
481.. option:: -Wl,<args>
482
483 Pass the comma separated arguments in args to the linker.
484
485.. option:: -Wp,<args>
486
487 Pass the comma separated arguments in args to the preprocessor.
488
489.. option:: -Xanalyzer <arg>
490
491 Pass arg to the static analyzer.
492
493.. option:: -Xassembler <arg>
494
495 Pass arg to the assembler.
496
497.. option:: -Xlinker <arg>
498
499 Pass arg to the linker.
500
501.. option:: -Xpreprocessor <arg>
502
503 Pass arg to the preprocessor.
504
505.. option:: -o <file>
506
507 Write output to file.
508
509.. option:: -print-file-name=<file>
510
511 Print the full library path of file.
512
513.. option:: -print-libgcc-file-name
514
Michal Gorny7cfe4802016-10-10 12:23:40 +0000515 Print the library path for the currently used compiler runtime library
516 ("libgcc.a" or "libclang_rt.builtins.*.a").
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000517
518.. option:: -print-prog-name=<name>
519
520 Print the full program path of name.
521
522.. option:: -print-search-dirs
523
524 Print the paths used for finding libraries and programs.
525
526.. option:: -save-temps
527
528 Save intermediate compilation results.
529
Matthias Braunabb6eea2016-09-26 18:53:34 +0000530.. option:: -save-stats, -save-stats=cwd, -save-stats=obj
531
532 Save internal code generation (LLVM) statistics to a file in the current
Aaron Ballman7482e592016-09-27 12:17:05 +0000533 directory (:option:`-save-stats`/"-save-stats=cwd") or the directory
534 of the output file ("-save-state=obj").
Matthias Braunabb6eea2016-09-26 18:53:34 +0000535
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000536.. option:: -integrated-as, -no-integrated-as
537
538 Used to enable and disable, respectively, the use of the integrated
539 assembler. Whether the integrated assembler is on by default is target
540 dependent.
541
542.. option:: -time
543
544 Time individual commands.
545
546.. option:: -ftime-report
547
548 Print timing summary of each stage of compilation.
549
550.. option:: -v
551
552 Show commands to run and use verbose output.
553
554
555Diagnostics Options
556~~~~~~~~~~~~~~~~~~~
557
558.. 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
559
560 These options control how Clang prints out information about diagnostics
561 (errors and warnings). Please see the Clang User's Manual for more information.
562
563Preprocessor Options
564~~~~~~~~~~~~~~~~~~~~
565
566.. option:: -D<macroname>=<value>
567
568 Adds an implicit #define into the predefines buffer which is read before the
569 source file is preprocessed.
570
571.. option:: -U<macroname>
572
573 Adds an implicit #undef into the predefines buffer which is read before the
574 source file is preprocessed.
575
576.. option:: -include <filename>
577
578 Adds an implicit #include into the predefines buffer which is read before the
579 source file is preprocessed.
580
581.. option:: -I<directory>
582
583 Add the specified directory to the search path for include files.
584
585.. option:: -F<directory>
586
587 Add the specified directory to the search path for framework include files.
588
589.. option:: -nostdinc
590
591 Do not search the standard system directories or compiler builtin directories
592 for include files.
593
594.. option:: -nostdlibinc
595
596 Do not search the standard system directories for include files, but do
597 search compiler builtin include directories.
598
599.. option:: -nobuiltininc
600
601 Do not search clang's builtin directory for include files.
602
603
604ENVIRONMENT
605-----------
606
607.. envvar:: TMPDIR, TEMP, TMP
608
609 These environment variables are checked, in order, for the location to write
610 temporary files used during the compilation process.
611
612.. envvar:: CPATH
613
614 If this environment variable is present, it is treated as a delimited list of
615 paths to be added to the default system include path list. The delimiter is
616 the platform dependent delimiter, as used in the PATH environment variable.
617
618 Empty components in the environment variable are ignored.
619
620.. envvar:: C_INCLUDE_PATH, OBJC_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH
621
622 These environment variables specify additional paths, as for :envvar:`CPATH`, which are
623 only used when processing the appropriate language.
624
625.. envvar:: MACOSX_DEPLOYMENT_TARGET
626
627 If :option:`-mmacosx-version-min` is unspecified, the default deployment
628 target is read from this environment variable. This option only affects
629 Darwin targets.
630
631BUGS
632----
633
Sylvestre Ledrubc5c3f52018-11-04 17:02:00 +0000634To report bugs, please visit <https://bugs.llvm.org/>. Most bug reports should
Andrew Wilkins6238c6f2015-06-30 02:52:38 +0000635include preprocessed source files (use the :option:`-E` option) and the full
636output of the compiler, along with information to reproduce.
637
638SEE ALSO
639--------
640
641:manpage:`as(1)`, :manpage:`ld(1)`