clang.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. clang - the Clang C, C++, and Objective-C compiler
  2. ==================================================
  3. SYNOPSIS
  4. --------
  5. :program:`clang` [*options*] *filename ...*
  6. DESCRIPTION
  7. -----------
  8. :program:`clang` is a C, C++, and Objective-C compiler which encompasses
  9. preprocessing, parsing, optimization, code generation, assembly, and linking.
  10. Depending on which high-level mode setting is passed, Clang will stop before
  11. doing a full link. While Clang is highly integrated, it is important to
  12. understand the stages of compilation, to understand how to invoke it. These
  13. stages are:
  14. Driver
  15. The clang executable is actually a small driver which controls the overall
  16. execution of other tools such as the compiler, assembler and linker.
  17. Typically you do not need to interact with the driver, but you
  18. transparently use it to run the other tools.
  19. Preprocessing
  20. This stage handles tokenization of the input source file, macro expansion,
  21. #include expansion and handling of other preprocessor directives. The
  22. output of this stage is typically called a ".i" (for C), ".ii" (for C++),
  23. ".mi" (for Objective-C), or ".mii" (for Objective-C++) file.
  24. Parsing and Semantic Analysis
  25. This stage parses the input file, translating preprocessor tokens into a
  26. parse tree. Once in the form of a parse tree, it applies semantic
  27. analysis to compute types for expressions as well and determine whether
  28. the code is well formed. This stage is responsible for generating most of
  29. the compiler warnings as well as parse errors. The output of this stage is
  30. an "Abstract Syntax Tree" (AST).
  31. Code Generation and Optimization
  32. This stage translates an AST into low-level intermediate code (known as
  33. "LLVM IR") and ultimately to machine code. This phase is responsible for
  34. optimizing the generated code and handling target-specific code generation.
  35. The output of this stage is typically called a ".s" file or "assembly" file.
  36. Clang also supports the use of an integrated assembler, in which the code
  37. generator produces object files directly. This avoids the overhead of
  38. generating the ".s" file and of calling the target assembler.
  39. Assembler
  40. This stage runs the target assembler to translate the output of the
  41. compiler into a target object file. The output of this stage is typically
  42. called a ".o" file or "object" file.
  43. Linker
  44. This stage runs the target linker to merge multiple object files into an
  45. executable or dynamic library. The output of this stage is typically called
  46. an "a.out", ".dylib" or ".so" file.
  47. :program:`Clang Static Analyzer`
  48. The Clang Static Analyzer is a tool that scans source code to try to find bugs
  49. through code analysis. This tool uses many parts of Clang and is built into
  50. the same driver. Please see <https://clang-analyzer.llvm.org> for more details
  51. on how to use the static analyzer.
  52. OPTIONS
  53. -------
  54. Stage Selection Options
  55. ~~~~~~~~~~~~~~~~~~~~~~~
  56. .. option:: -E
  57. Run the preprocessor stage.
  58. .. option:: -fsyntax-only
  59. Run the preprocessor, parser and type checking stages.
  60. .. option:: -S
  61. Run the previous stages as well as LLVM generation and optimization stages
  62. and target-specific code generation, producing an assembly file.
  63. .. option:: -c
  64. Run all of the above, plus the assembler, generating a target ".o" object file.
  65. .. option:: no stage selection option
  66. If no stage selection option is specified, all stages above are run, and the
  67. linker is run to combine the results into an executable or shared library.
  68. Language Selection and Mode Options
  69. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  70. .. option:: -x <language>
  71. Treat subsequent input files as having type language.
  72. .. option:: -std=<standard>
  73. Specify the language standard to compile for.
  74. Supported values for the C language are:
  75. | ``c89``
  76. | ``c90``
  77. | ``iso9899:1990``
  78. ISO C 1990
  79. | ``iso9899:199409``
  80. ISO C 1990 with amendment 1
  81. | ``gnu89``
  82. | ``gnu90``
  83. ISO C 1990 with GNU extensions
  84. | ``c99``
  85. | ``iso9899:1999``
  86. ISO C 1999
  87. | ``gnu99``
  88. ISO C 1999 with GNU extensions
  89. | ``c11``
  90. | ``iso9899:2011``
  91. ISO C 2011
  92. | ``gnu11``
  93. ISO C 2011 with GNU extensions
  94. | ``c17``
  95. | ``iso9899:2017``
  96. ISO C 2017
  97. | ``gnu17``
  98. ISO C 2017 with GNU extensions
  99. The default C language standard is ``gnu11``, except on PS4, where it is
  100. ``gnu99``.
  101. Supported values for the C++ language are:
  102. | ``c++98``
  103. | ``c++03``
  104. ISO C++ 1998 with amendments
  105. | ``gnu++98``
  106. | ``gnu++03``
  107. ISO C++ 1998 with amendments and GNU extensions
  108. | ``c++11``
  109. ISO C++ 2011 with amendments
  110. | ``gnu++11``
  111. ISO C++ 2011 with amendments and GNU extensions
  112. | ``c++14``
  113. ISO C++ 2014 with amendments
  114. | ``gnu++14``
  115. ISO C++ 2014 with amendments and GNU extensions
  116. | ``c++17``
  117. ISO C++ 2017 with amendments
  118. | ``gnu++17``
  119. ISO C++ 2017 with amendments and GNU extensions
  120. | ``c++2a``
  121. Working draft for ISO C++ 2020
  122. | ``gnu++2a``
  123. Working draft for ISO C++ 2020 with GNU extensions
  124. The default C++ language standard is ``gnu++14``.
  125. Supported values for the OpenCL language are:
  126. | ``cl1.0``
  127. OpenCL 1.0
  128. | ``cl1.1``
  129. OpenCL 1.1
  130. | ``cl1.2``
  131. OpenCL 1.2
  132. | ``cl2.0``
  133. OpenCL 2.0
  134. The default OpenCL language standard is ``cl1.0``.
  135. Supported values for the CUDA language are:
  136. | ``cuda``
  137. NVIDIA CUDA(tm)
  138. .. option:: -stdlib=<library>
  139. Specify the C++ standard library to use; supported options are libstdc++ and
  140. libc++. If not specified, platform default will be used.
  141. .. option:: -rtlib=<library>
  142. Specify the compiler runtime library to use; supported options are libgcc and
  143. compiler-rt. If not specified, platform default will be used.
  144. .. option:: -ansi
  145. Same as -std=c89.
  146. .. option:: -ObjC, -ObjC++
  147. Treat source input files as Objective-C and Object-C++ inputs respectively.
  148. .. option:: -trigraphs
  149. Enable trigraphs.
  150. .. option:: -ffreestanding
  151. Indicate that the file should be compiled for a freestanding, not a hosted,
  152. environment.
  153. .. option:: -fno-builtin
  154. Disable special handling and optimizations of builtin functions like
  155. :c:func:`strlen` and :c:func:`malloc`.
  156. .. option:: -fmath-errno
  157. Indicate that math functions should be treated as updating :c:data:`errno`.
  158. .. option:: -fpascal-strings
  159. Enable support for Pascal-style strings with "\\pfoo".
  160. .. option:: -fms-extensions
  161. Enable support for Microsoft extensions.
  162. .. option:: -fmsc-version=
  163. Set _MSC_VER. Defaults to 1300 on Windows. Not set otherwise.
  164. .. option:: -fborland-extensions
  165. Enable support for Borland extensions.
  166. .. option:: -fwritable-strings
  167. Make all string literals default to writable. This disables uniquing of
  168. strings and other optimizations.
  169. .. option:: -flax-vector-conversions
  170. Allow loose type checking rules for implicit vector conversions.
  171. .. option:: -fblocks
  172. Enable the "Blocks" language feature.
  173. .. option:: -fobjc-abi-version=version
  174. Select the Objective-C ABI version to use. Available versions are 1 (legacy
  175. "fragile" ABI), 2 (non-fragile ABI 1), and 3 (non-fragile ABI 2).
  176. .. 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
  180. default).
  181. .. option:: -fobjc-nonfragile-abi, -fno-objc-nonfragile-abi
  182. Enable use of the Objective-C non-fragile ABI. On platforms for which this is
  183. the default ABI, it can be disabled with :option:`-fno-objc-nonfragile-abi`.
  184. Target Selection Options
  185. ~~~~~~~~~~~~~~~~~~~~~~~~
  186. Clang fully supports cross compilation as an inherent part of its design.
  187. Depending on how your version of Clang is configured, it may have support for a
  188. number of cross compilers, or may only support a native target.
  189. .. option:: -arch <architecture>
  190. Specify the architecture to build for.
  191. .. option:: -mmacosx-version-min=<version>
  192. When building for macOS, specify the minimum version supported by your
  193. application.
  194. .. option:: -miphoneos-version-min
  195. When building for iPhone OS, specify the minimum version supported by your
  196. application.
  197. .. option:: --print-supported-cpus
  198. Print out a list of supported processors for the given target (specified
  199. through --target=<architecture> or -arch <architecture>). If no target is
  200. specified, the system default target will be used.
  201. .. option:: -mcpu=?, -mtune=?
  202. Aliases of --print-supported-cpus
  203. .. option:: -march=<cpu>
  204. Specify that Clang should generate code for a specific processor family
  205. member and later. For example, if you specify -march=i486, the compiler is
  206. allowed to generate instructions that are valid on i486 and later processors,
  207. but which may not exist on earlier ones.
  208. Code Generation Options
  209. ~~~~~~~~~~~~~~~~~~~~~~~
  210. .. option:: -O0, -O1, -O2, -O3, -Ofast, -Os, -Oz, -Og, -O, -O4
  211. Specify which optimization level to use:
  212. :option:`-O0` Means "no optimization": this level compiles the fastest and
  213. generates the most debuggable code.
  214. :option:`-O1` Somewhere between :option:`-O0` and :option:`-O2`.
  215. :option:`-O2` Moderate level of optimization which enables most
  216. optimizations.
  217. :option:`-O3` Like :option:`-O2`, except that it enables optimizations that
  218. take longer to perform or that may generate larger code (in an attempt to
  219. make the program run faster).
  220. :option:`-Ofast` Enables all the optimizations from :option:`-O3` along
  221. with other aggressive optimizations that may violate strict compliance with
  222. language standards.
  223. :option:`-Os` Like :option:`-O2` with extra optimizations to reduce code
  224. size.
  225. :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
  226. size further.
  227. :option:`-Og` Like :option:`-O1`. In future versions, this option might
  228. disable different optimizations in order to improve debuggability.
  229. :option:`-O` Equivalent to :option:`-O2`.
  230. :option:`-O4` and higher
  231. Currently equivalent to :option:`-O3`
  232. .. option:: -g, -gline-tables-only, -gmodules
  233. Control debug information output. Note that Clang debug information works
  234. best at :option:`-O0`. When more than one option starting with `-g` is
  235. specified, the last one wins:
  236. :option:`-g` Generate debug information.
  237. :option:`-gline-tables-only` Generate only line table debug information. This
  238. allows for symbolicated backtraces with inlining information, but does not
  239. include any information about variables, their locations or types.
  240. :option:`-gmodules` Generate debug information that contains external
  241. references to types defined in Clang modules or precompiled headers instead
  242. of emitting redundant debug type information into every object file. This
  243. option transparently switches the Clang module format to object file
  244. containers that hold the Clang module together with the debug information.
  245. When compiling a program that uses Clang modules or precompiled headers,
  246. this option produces complete debug information with faster compile
  247. times and much smaller object files.
  248. This option should not be used when building static libraries for
  249. distribution to other machines because the debug info will contain
  250. references to the module cache on the machine the object files in the
  251. library were built on.
  252. .. option:: -fstandalone-debug -fno-standalone-debug
  253. Clang supports a number of optimizations to reduce the size of debug
  254. information in the binary. They work based on the assumption that the
  255. debug type information can be spread out over multiple compilation units.
  256. For instance, Clang will not emit type definitions for types that are not
  257. needed by a module and could be replaced with a forward declaration.
  258. Further, Clang will only emit type info for a dynamic C++ class in the
  259. module that contains the vtable for the class.
  260. The :option:`-fstandalone-debug` option turns off these optimizations.
  261. This is useful when working with 3rd-party libraries that don't come with
  262. debug information. This is the default on Darwin. Note that Clang will
  263. never emit type information for types that are not referenced at all by the
  264. program.
  265. .. option:: -fexceptions
  266. Enable generation of unwind information. This allows exceptions to be thrown
  267. through Clang compiled stack frames. This is on by default in x86-64.
  268. .. option:: -ftrapv
  269. Generate code to catch integer overflow errors. Signed integer overflow is
  270. undefined in C. With this flag, extra code is generated to detect this and
  271. abort when it happens.
  272. .. option:: -fvisibility
  273. This flag sets the default visibility level.
  274. .. option:: -fcommon, -fno-common
  275. This flag specifies that variables without initializers get common linkage.
  276. It can be disabled with :option:`-fno-common`.
  277. .. option:: -ftls-model=<model>
  278. Set the default thread-local storage (TLS) model to use for thread-local
  279. variables. Valid values are: "global-dynamic", "local-dynamic",
  280. "initial-exec" and "local-exec". The default is "global-dynamic". The default
  281. model can be overridden with the tls_model attribute. The compiler will try
  282. to choose a more efficient model if possible.
  283. .. option:: -flto, -flto=full, -flto=thin, -emit-llvm
  284. Generate output files in LLVM formats, suitable for link time optimization.
  285. When used with :option:`-S` this generates LLVM intermediate language
  286. assembly files, otherwise this generates LLVM bitcode format object files
  287. (which may be passed to the linker depending on the stage selection options).
  288. The default for :option:`-flto` is "full", in which the
  289. LLVM bitcode is suitable for monolithic Link Time Optimization (LTO), where
  290. the linker merges all such modules into a single combined module for
  291. optimization. With "thin", :doc:`ThinLTO <../ThinLTO>`
  292. compilation is invoked instead.
  293. Driver Options
  294. ~~~~~~~~~~~~~~
  295. .. option:: -###
  296. Print (but do not run) the commands to run for this compilation.
  297. .. option:: --help
  298. Display available options.
  299. .. option:: -Qunused-arguments
  300. Do not emit any warnings for unused driver arguments.
  301. .. option:: -Wa,<args>
  302. Pass the comma separated arguments in args to the assembler.
  303. .. option:: -Wl,<args>
  304. Pass the comma separated arguments in args to the linker.
  305. .. option:: -Wp,<args>
  306. Pass the comma separated arguments in args to the preprocessor.
  307. .. option:: -Xanalyzer <arg>
  308. Pass arg to the static analyzer.
  309. .. option:: -Xassembler <arg>
  310. Pass arg to the assembler.
  311. .. option:: -Xlinker <arg>
  312. Pass arg to the linker.
  313. .. option:: -Xpreprocessor <arg>
  314. Pass arg to the preprocessor.
  315. .. option:: -o <file>
  316. Write output to file.
  317. .. option:: -print-file-name=<file>
  318. Print the full library path of file.
  319. .. option:: -print-libgcc-file-name
  320. Print the library path for the currently used compiler runtime library
  321. ("libgcc.a" or "libclang_rt.builtins.*.a").
  322. .. option:: -print-prog-name=<name>
  323. Print the full program path of name.
  324. .. option:: -print-search-dirs
  325. Print the paths used for finding libraries and programs.
  326. .. option:: -save-temps
  327. Save intermediate compilation results.
  328. .. option:: -save-stats, -save-stats=cwd, -save-stats=obj
  329. Save internal code generation (LLVM) statistics to a file in the current
  330. directory (:option:`-save-stats`/"-save-stats=cwd") or the directory
  331. of the output file ("-save-state=obj").
  332. .. option:: -integrated-as, -no-integrated-as
  333. Used to enable and disable, respectively, the use of the integrated
  334. assembler. Whether the integrated assembler is on by default is target
  335. dependent.
  336. .. option:: -time
  337. Time individual commands.
  338. .. option:: -ftime-report
  339. Print timing summary of each stage of compilation.
  340. .. option:: -v
  341. Show commands to run and use verbose output.
  342. Diagnostics Options
  343. ~~~~~~~~~~~~~~~~~~~
  344. .. 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
  345. These options control how Clang prints out information about diagnostics
  346. (errors and warnings). Please see the Clang User's Manual for more information.
  347. Preprocessor Options
  348. ~~~~~~~~~~~~~~~~~~~~
  349. .. option:: -D<macroname>=<value>
  350. Adds an implicit #define into the predefines buffer which is read before the
  351. source file is preprocessed.
  352. .. option:: -U<macroname>
  353. Adds an implicit #undef into the predefines buffer which is read before the
  354. source file is preprocessed.
  355. .. option:: -include <filename>
  356. Adds an implicit #include into the predefines buffer which is read before the
  357. source file is preprocessed.
  358. .. option:: -I<directory>
  359. Add the specified directory to the search path for include files.
  360. .. option:: -F<directory>
  361. Add the specified directory to the search path for framework include files.
  362. .. option:: -nostdinc
  363. Do not search the standard system directories or compiler builtin directories
  364. for include files.
  365. .. option:: -nostdlibinc
  366. Do not search the standard system directories for include files, but do
  367. search compiler builtin include directories.
  368. .. option:: -nobuiltininc
  369. Do not search clang's builtin directory for include files.
  370. ENVIRONMENT
  371. -----------
  372. .. envvar:: TMPDIR, TEMP, TMP
  373. These environment variables are checked, in order, for the location to write
  374. temporary files used during the compilation process.
  375. .. envvar:: CPATH
  376. If this environment variable is present, it is treated as a delimited list of
  377. paths to be added to the default system include path list. The delimiter is
  378. the platform dependent delimiter, as used in the PATH environment variable.
  379. Empty components in the environment variable are ignored.
  380. .. envvar:: C_INCLUDE_PATH, OBJC_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH
  381. These environment variables specify additional paths, as for :envvar:`CPATH`, which are
  382. only used when processing the appropriate language.
  383. .. envvar:: MACOSX_DEPLOYMENT_TARGET
  384. If :option:`-mmacosx-version-min` is unspecified, the default deployment
  385. target is read from this environment variable. This option only affects
  386. Darwin targets.
  387. BUGS
  388. ----
  389. To report bugs, please visit <https://bugs.llvm.org/>. Most bug reports should
  390. include preprocessed source files (use the :option:`-E` option) and the full
  391. output of the compiler, along with information to reproduce.
  392. SEE ALSO
  393. --------
  394. :manpage:`as(1)`, :manpage:`ld(1)`