clang.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 <http://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=<language>
  73. Specify the language standard to compile for.
  74. .. option:: -stdlib=<library>
  75. Specify the C++ standard library to use; supported options are libstdc++ and
  76. libc++. If not specified, platform default will be used.
  77. .. option:: -rtlib=<library>
  78. Specify the compiler runtime library to use; supported options are libgcc and
  79. compiler-rt. If not specified, platform default will be used.
  80. .. option:: -ansi
  81. Same as -std=c89.
  82. .. option:: -ObjC, -ObjC++
  83. Treat source input files as Objective-C and Object-C++ inputs respectively.
  84. .. option:: -trigraphs
  85. Enable trigraphs.
  86. .. option:: -ffreestanding
  87. Indicate that the file should be compiled for a freestanding, not a hosted,
  88. environment.
  89. .. option:: -fno-builtin
  90. Disable special handling and optimizations of builtin functions like
  91. :c:func:`strlen` and :c:func:`malloc`.
  92. .. option:: -fmath-errno
  93. Indicate that math functions should be treated as updating :c:data:`errno`.
  94. .. option:: -fpascal-strings
  95. Enable support for Pascal-style strings with "\\pfoo".
  96. .. option:: -fms-extensions
  97. Enable support for Microsoft extensions.
  98. .. option:: -fmsc-version=
  99. Set _MSC_VER. Defaults to 1300 on Windows. Not set otherwise.
  100. .. option:: -fborland-extensions
  101. Enable support for Borland extensions.
  102. .. option:: -fwritable-strings
  103. Make all string literals default to writable. This disables uniquing of
  104. strings and other optimizations.
  105. .. option:: -flax-vector-conversions
  106. Allow loose type checking rules for implicit vector conversions.
  107. .. option:: -fblocks
  108. Enable the "Blocks" language feature.
  109. .. option:: -fobjc-abi-version=version
  110. Select the Objective-C ABI version to use. Available versions are 1 (legacy
  111. "fragile" ABI), 2 (non-fragile ABI 1), and 3 (non-fragile ABI 2).
  112. .. option:: -fobjc-nonfragile-abi-version=<version>
  113. Select the Objective-C non-fragile ABI version to use by default. This will
  114. only be used as the Objective-C ABI when the non-fragile ABI is enabled
  115. (either via :option:`-fobjc-nonfragile-abi`, or because it is the platform
  116. default).
  117. .. option:: -fobjc-nonfragile-abi, -fno-objc-nonfragile-abi
  118. Enable use of the Objective-C non-fragile ABI. On platforms for which this is
  119. the default ABI, it can be disabled with :option:`-fno-objc-nonfragile-abi`.
  120. Target Selection Options
  121. ~~~~~~~~~~~~~~~~~~~~~~~~
  122. Clang fully supports cross compilation as an inherent part of its design.
  123. Depending on how your version of Clang is configured, it may have support for a
  124. number of cross compilers, or may only support a native target.
  125. .. option:: -arch <architecture>
  126. Specify the architecture to build for.
  127. .. option:: -mmacosx-version-min=<version>
  128. When building for Mac OS X, specify the minimum version supported by your
  129. application.
  130. .. option:: -miphoneos-version-min
  131. When building for iPhone OS, specify the minimum version supported by your
  132. application.
  133. .. option:: -march=<cpu>
  134. Specify that Clang should generate code for a specific processor family
  135. member and later. For example, if you specify -march=i486, the compiler is
  136. allowed to generate instructions that are valid on i486 and later processors,
  137. but which may not exist on earlier ones.
  138. Code Generation Options
  139. ~~~~~~~~~~~~~~~~~~~~~~~
  140. .. option:: -O0, -O1, -O2, -O3, -Ofast, -Os, -Oz, -Og, -O, -O4
  141. Specify which optimization level to use:
  142. :option:`-O0` Means "no optimization": this level compiles the fastest and
  143. generates the most debuggable code.
  144. :option:`-O1` Somewhere between :option:`-O0` and :option:`-O2`.
  145. :option:`-O2` Moderate level of optimization which enables most
  146. optimizations.
  147. :option:`-O3` Like :option:`-O2`, except that it enables optimizations that
  148. take longer to perform or that may generate larger code (in an attempt to
  149. make the program run faster).
  150. :option:`-Ofast` Enables all the optimizations from :option:`-O3` along
  151. with other aggressive optimizations that may violate strict compliance with
  152. language standards.
  153. :option:`-Os` Like :option:`-O2` with extra optimizations to reduce code
  154. size.
  155. :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
  156. size further.
  157. :option:`-Og` Like :option:`-O1`. In future versions, this option might
  158. disable different optimizations in order to improve debuggability.
  159. :option:`-O` Equivalent to :option:`-O2`.
  160. :option:`-O4` and higher
  161. Currently equivalent to :option:`-O3`
  162. .. option:: -g, -gline-tables-only, -gmodules
  163. Control debug information output. Note that Clang debug information works
  164. best at :option:`-O0`. When more than one option starting with `-g` is
  165. specified, the last one wins:
  166. :option:`-g` Generate debug information.
  167. :option:`-gline-tables-only` Generate only line table debug information. This
  168. allows for symbolicated backtraces with inlining information, but does not
  169. include any information about variables, their locations or types.
  170. :option:`-gmodules` Generate debug information that contains external
  171. references to types defined in Clang modules or precompiled headers instead
  172. of emitting redundant debug type information into every object file. This
  173. option transparently switches the Clang module format to object file
  174. containers that hold the Clang module together with the debug information.
  175. When compiling a program that uses Clang modules or precompiled headers,
  176. this option produces complete debug information with faster compile
  177. times and much smaller object files.
  178. This option should not be used when building static libraries for
  179. distribution to other machines because the debug info will contain
  180. references to the module cache on the machine the object files in the
  181. library were built on.
  182. .. option:: -fstandalone-debug -fno-standalone-debug
  183. Clang supports a number of optimizations to reduce the size of debug
  184. information in the binary. They work based on the assumption that the
  185. debug type information can be spread out over multiple compilation units.
  186. For instance, Clang will not emit type definitions for types that are not
  187. needed by a module and could be replaced with a forward declaration.
  188. Further, Clang will only emit type info for a dynamic C++ class in the
  189. module that contains the vtable for the class.
  190. The :option:`-fstandalone-debug` option turns off these optimizations.
  191. This is useful when working with 3rd-party libraries that don't come with
  192. debug information. This is the default on Darwin. Note that Clang will
  193. never emit type information for types that are not referenced at all by the
  194. program.
  195. .. option:: -fexceptions
  196. Enable generation of unwind information. This allows exceptions to be thrown
  197. through Clang compiled stack frames. This is on by default in x86-64.
  198. .. option:: -ftrapv
  199. Generate code to catch integer overflow errors. Signed integer overflow is
  200. undefined in C. With this flag, extra code is generated to detect this and
  201. abort when it happens.
  202. .. option:: -fvisibility
  203. This flag sets the default visibility level.
  204. .. option:: -fcommon, -fno-common
  205. This flag specifies that variables without initializers get common linkage.
  206. It can be disabled with :option:`-fno-common`.
  207. .. option:: -ftls-model=<model>
  208. Set the default thread-local storage (TLS) model to use for thread-local
  209. variables. Valid values are: "global-dynamic", "local-dynamic",
  210. "initial-exec" and "local-exec". The default is "global-dynamic". The default
  211. model can be overridden with the tls_model attribute. The compiler will try
  212. to choose a more efficient model if possible.
  213. .. option:: -flto, -flto=full, -flto=thin, -emit-llvm
  214. Generate output files in LLVM formats, suitable for link time optimization.
  215. When used with :option:`-S` this generates LLVM intermediate language
  216. assembly files, otherwise this generates LLVM bitcode format object files
  217. (which may be passed to the linker depending on the stage selection options).
  218. The default for :option:`-flto` is "full", in which the
  219. LLVM bitcode is suitable for monolithic Link Time Optimization (LTO), where
  220. the linker merges all such modules into a single combined module for
  221. optimization. With "thin", :doc:`ThinLTO <../ThinLTO>`
  222. compilation is invoked instead.
  223. Driver Options
  224. ~~~~~~~~~~~~~~
  225. .. option:: -###
  226. Print (but do not run) the commands to run for this compilation.
  227. .. option:: --help
  228. Display available options.
  229. .. option:: -Qunused-arguments
  230. Do not emit any warnings for unused driver arguments.
  231. .. option:: -Wa,<args>
  232. Pass the comma separated arguments in args to the assembler.
  233. .. option:: -Wl,<args>
  234. Pass the comma separated arguments in args to the linker.
  235. .. option:: -Wp,<args>
  236. Pass the comma separated arguments in args to the preprocessor.
  237. .. option:: -Xanalyzer <arg>
  238. Pass arg to the static analyzer.
  239. .. option:: -Xassembler <arg>
  240. Pass arg to the assembler.
  241. .. option:: -Xlinker <arg>
  242. Pass arg to the linker.
  243. .. option:: -Xpreprocessor <arg>
  244. Pass arg to the preprocessor.
  245. .. option:: -o <file>
  246. Write output to file.
  247. .. option:: -print-file-name=<file>
  248. Print the full library path of file.
  249. .. option:: -print-libgcc-file-name
  250. Print the library path for the currently used compiler runtime library
  251. ("libgcc.a" or "libclang_rt.builtins.*.a").
  252. .. option:: -print-prog-name=<name>
  253. Print the full program path of name.
  254. .. option:: -print-search-dirs
  255. Print the paths used for finding libraries and programs.
  256. .. option:: -save-temps
  257. Save intermediate compilation results.
  258. .. option:: -save-stats, -save-stats=cwd, -save-stats=obj
  259. Save internal code generation (LLVM) statistics to a file in the current
  260. directory (:option:`-save-stats`/"-save-stats=cwd") or the directory
  261. of the output file ("-save-state=obj").
  262. .. option:: -integrated-as, -no-integrated-as
  263. Used to enable and disable, respectively, the use of the integrated
  264. assembler. Whether the integrated assembler is on by default is target
  265. dependent.
  266. .. option:: -time
  267. Time individual commands.
  268. .. option:: -ftime-report
  269. Print timing summary of each stage of compilation.
  270. .. option:: -v
  271. Show commands to run and use verbose output.
  272. Diagnostics Options
  273. ~~~~~~~~~~~~~~~~~~~
  274. .. 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
  275. These options control how Clang prints out information about diagnostics
  276. (errors and warnings). Please see the Clang User's Manual for more information.
  277. Preprocessor Options
  278. ~~~~~~~~~~~~~~~~~~~~
  279. .. option:: -D<macroname>=<value>
  280. Adds an implicit #define into the predefines buffer which is read before the
  281. source file is preprocessed.
  282. .. option:: -U<macroname>
  283. Adds an implicit #undef into the predefines buffer which is read before the
  284. source file is preprocessed.
  285. .. option:: -include <filename>
  286. Adds an implicit #include into the predefines buffer which is read before the
  287. source file is preprocessed.
  288. .. option:: -I<directory>
  289. Add the specified directory to the search path for include files.
  290. .. option:: -F<directory>
  291. Add the specified directory to the search path for framework include files.
  292. .. option:: -nostdinc
  293. Do not search the standard system directories or compiler builtin directories
  294. for include files.
  295. .. option:: -nostdlibinc
  296. Do not search the standard system directories for include files, but do
  297. search compiler builtin include directories.
  298. .. option:: -nobuiltininc
  299. Do not search clang's builtin directory for include files.
  300. ENVIRONMENT
  301. -----------
  302. .. envvar:: TMPDIR, TEMP, TMP
  303. These environment variables are checked, in order, for the location to write
  304. temporary files used during the compilation process.
  305. .. envvar:: CPATH
  306. If this environment variable is present, it is treated as a delimited list of
  307. paths to be added to the default system include path list. The delimiter is
  308. the platform dependent delimiter, as used in the PATH environment variable.
  309. Empty components in the environment variable are ignored.
  310. .. envvar:: C_INCLUDE_PATH, OBJC_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH
  311. These environment variables specify additional paths, as for :envvar:`CPATH`, which are
  312. only used when processing the appropriate language.
  313. .. envvar:: MACOSX_DEPLOYMENT_TARGET
  314. If :option:`-mmacosx-version-min` is unspecified, the default deployment
  315. target is read from this environment variable. This option only affects
  316. Darwin targets.
  317. BUGS
  318. ----
  319. To report bugs, please visit <http://llvm.org/bugs/>. Most bug reports should
  320. include preprocessed source files (use the :option:`-E` option) and the full
  321. output of the compiler, along with information to reproduce.
  322. SEE ALSO
  323. --------
  324. :manpage:`as(1)`, :manpage:`ld(1)`