clang.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. clang - the Clang C, C++, and Objective-C compiler
  2. ==================================================
  3. NOTE: this document applies to the original Clang project, not the DirectX
  4. Compiler. It's made available for informational purposes only. The primary
  5. replacement for the clang program is dxc.
  6. SYNOPSIS
  7. --------
  8. :program:`clang` [*options*] *filename ...*
  9. DESCRIPTION
  10. -----------
  11. :program:`clang` is a C, C++, and Objective-C compiler which encompasses
  12. preprocessing, parsing, optimization, code generation, assembly, and linking.
  13. Depending on which high-level mode setting is passed, Clang will stop before
  14. doing a full link. While Clang is highly integrated, it is important to
  15. understand the stages of compilation, to understand how to invoke it. These
  16. stages are:
  17. Driver
  18. The clang executable is actually a small driver which controls the overall
  19. execution of other tools such as the compiler, assembler and linker.
  20. Typically you do not need to interact with the driver, but you
  21. transparently use it to run the other tools.
  22. Preprocessing
  23. This stage handles tokenization of the input source file, macro expansion,
  24. #include expansion and handling of other preprocessor directives. The
  25. output of this stage is typically called a ".i" (for C), ".ii" (for C++),
  26. ".mi" (for Objective-C), or ".mii" (for Objective-C++) file.
  27. Parsing and Semantic Analysis
  28. This stage parses the input file, translating preprocessor tokens into a
  29. parse tree. Once in the form of a parse tree, it applies semantic
  30. analysis to compute types for expressions as well and determine whether
  31. the code is well formed. This stage is responsible for generating most of
  32. the compiler warnings as well as parse errors. The output of this stage is
  33. an "Abstract Syntax Tree" (AST).
  34. Code Generation and Optimization
  35. This stage translates an AST into low-level intermediate code (known as
  36. "LLVM IR") and ultimately to machine code. This phase is responsible for
  37. optimizing the generated code and handling target-specific code generation.
  38. The output of this stage is typically called a ".s" file or "assembly" file.
  39. Clang also supports the use of an integrated assembler, in which the code
  40. generator produces object files directly. This avoids the overhead of
  41. generating the ".s" file and of calling the target assembler.
  42. Assembler
  43. This stage runs the target assembler to translate the output of the
  44. compiler into a target object file. The output of this stage is typically
  45. called a ".o" file or "object" file.
  46. Linker
  47. This stage runs the target linker to merge multiple object files into an
  48. executable or dynamic library. The output of this stage is typically called
  49. an "a.out", ".dylib" or ".so" file.
  50. :program:`Clang Static Analyzer`
  51. The Clang Static Analyzer is a tool that scans source code to try to find bugs
  52. through code analysis. This tool uses many parts of Clang and is built into
  53. the same driver. Please see <http://clang-analyzer.llvm.org> for more details
  54. on how to use the static analyzer.
  55. OPTIONS
  56. -------
  57. Stage Selection Options
  58. ~~~~~~~~~~~~~~~~~~~~~~~
  59. .. option:: -E
  60. Run the preprocessor stage.
  61. .. option:: -fsyntax-only
  62. Run the preprocessor, parser and type checking stages.
  63. .. option:: -S
  64. Run the previous stages as well as LLVM generation and optimization stages
  65. and target-specific code generation, producing an assembly file.
  66. .. option:: -c
  67. Run all of the above, plus the assembler, generating a target ".o" object file.
  68. .. option:: no stage selection option
  69. If no stage selection option is specified, all stages above are run, and the
  70. linker is run to combine the results into an executable or shared library.
  71. Language Selection and Mode Options
  72. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  73. .. option:: -x <language>
  74. Treat subsequent input files as having type language.
  75. .. option:: -std=<language>
  76. Specify the language standard to compile for.
  77. .. option:: -stdlib=<library>
  78. Specify the C++ standard library to use; supported options are libstdc++ and
  79. libc++.
  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-gc-only
  110. Indicate that Objective-C code should be compiled in GC-only mode, which only
  111. works when Objective-C Garbage Collection is enabled.
  112. .. option:: -fobjc-gc
  113. Indicate that Objective-C code should be compiled in hybrid-GC mode, which
  114. works with both GC and non-GC mode.
  115. .. option:: -fobjc-abi-version=version
  116. Select the Objective-C ABI version to use. Available versions are 1 (legacy
  117. "fragile" ABI), 2 (non-fragile ABI 1), and 3 (non-fragile ABI 2).
  118. .. option:: -fobjc-nonfragile-abi-version=<version>
  119. Select the Objective-C non-fragile ABI version to use by default. This will
  120. only be used as the Objective-C ABI when the non-fragile ABI is enabled
  121. (either via :option:`-fobjc-nonfragile-abi`, or because it is the platform
  122. default).
  123. .. option:: -fobjc-nonfragile-abi
  124. Enable use of the Objective-C non-fragile ABI. On platforms for which this is
  125. the default ABI, it can be disabled with :option:`-fno-objc-nonfragile-abi`.
  126. Target Selection Options
  127. ~~~~~~~~~~~~~~~~~~~~~~~~
  128. Clang fully supports cross compilation as an inherent part of its design.
  129. Depending on how your version of Clang is configured, it may have support for a
  130. number of cross compilers, or may only support a native target.
  131. .. option:: -arch <architecture>
  132. Specify the architecture to build for.
  133. .. option:: -mmacosx-version-min=<version>
  134. When building for Mac OS X, specify the minimum version supported by your
  135. application.
  136. .. option:: -miphoneos-version-min
  137. When building for iPhone OS, specify the minimum version supported by your
  138. application.
  139. .. option:: -march=<cpu>
  140. Specify that Clang should generate code for a specific processor family
  141. member and later. For example, if you specify -march=i486, the compiler is
  142. allowed to generate instructions that are valid on i486 and later processors,
  143. but which may not exist on earlier ones.
  144. Code Generation Options
  145. ~~~~~~~~~~~~~~~~~~~~~~~
  146. .. option:: -O0, -O1, -O2, -O3, -Ofast, -Os, -Oz, -O, -O4
  147. Specify which optimization level to use:
  148. :option:`-O0` Means "no optimization": this level compiles the fastest and
  149. generates the most debuggable code.
  150. :option:`-O1` Somewhere between :option:`-O0` and :option:`-O2`.
  151. :option:`-O2` Moderate level of optimization which enables most
  152. optimizations.
  153. :option:`-O3` Like :option:`-O2`, except that it enables optimizations that
  154. take longer to perform or that may generate larger code (in an attempt to
  155. make the program run faster).
  156. :option:`-Ofast` Enables all the optimizations from :option:`-O3` along
  157. with other aggressive optimizations that may violate strict compliance with
  158. language standards.
  159. :option:`-Os` Like :option:`-O2` with extra optimizations to reduce code
  160. size.
  161. :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
  162. size further.
  163. :option:`-O` Equivalent to :option:`-O2`.
  164. :option:`-O4` and higher
  165. Currently equivalent to :option:`-O3`
  166. .. option:: -g
  167. Generate debug information. Note that Clang debug information works best at -O0.
  168. .. option:: -fstandalone-debug -fno-standalone-debug
  169. Clang supports a number of optimizations to reduce the size of debug
  170. information in the binary. They work based on the assumption that the
  171. debug type information can be spread out over multiple compilation units.
  172. For instance, Clang will not emit type definitions for types that are not
  173. needed by a module and could be replaced with a forward declaration.
  174. Further, Clang will only emit type info for a dynamic C++ class in the
  175. module that contains the vtable for the class.
  176. The :option:`-fstandalone-debug` option turns off these optimizations.
  177. This is useful when working with 3rd-party libraries that don't come with
  178. debug information. This is the default on Darwin. Note that Clang will
  179. never emit type information for types that are not referenced at all by the
  180. program.
  181. .. option:: -fexceptions
  182. Enable generation of unwind information. This allows exceptions to be thrown
  183. through Clang compiled stack frames. This is on by default in x86-64.
  184. .. option:: -ftrapv
  185. Generate code to catch integer overflow errors. Signed integer overflow is
  186. undefined in C. With this flag, extra code is generated to detect this and
  187. abort when it happens.
  188. .. option:: -fvisibility
  189. This flag sets the default visibility level.
  190. .. option:: -fcommon
  191. This flag specifies that variables without initializers get common linkage.
  192. It can be disabled with :option:`-fno-common`.
  193. .. option:: -ftls-model=<model>
  194. Set the default thread-local storage (TLS) model to use for thread-local
  195. variables. Valid values are: "global-dynamic", "local-dynamic",
  196. "initial-exec" and "local-exec". The default is "global-dynamic". The default
  197. model can be overridden with the tls_model attribute. The compiler will try
  198. to choose a more efficient model if possible.
  199. .. option:: -flto, -emit-llvm
  200. Generate output files in LLVM formats, suitable for link time optimization.
  201. When used with :option:`-S` this generates LLVM intermediate language
  202. assembly files, otherwise this generates LLVM bitcode format object files
  203. (which may be passed to the linker depending on the stage selection options).
  204. Driver Options
  205. ~~~~~~~~~~~~~~
  206. .. option:: -###
  207. Print (but do not run) the commands to run for this compilation.
  208. .. option:: --help
  209. Display available options.
  210. .. option:: -Qunused-arguments
  211. Do not emit any warnings for unused driver arguments.
  212. .. option:: -Wa,<args>
  213. Pass the comma separated arguments in args to the assembler.
  214. .. option:: -Wl,<args>
  215. Pass the comma separated arguments in args to the linker.
  216. .. option:: -Wp,<args>
  217. Pass the comma separated arguments in args to the preprocessor.
  218. .. option:: -Xanalyzer <arg>
  219. Pass arg to the static analyzer.
  220. .. option:: -Xassembler <arg>
  221. Pass arg to the assembler.
  222. .. option:: -Xlinker <arg>
  223. Pass arg to the linker.
  224. .. option:: -Xpreprocessor <arg>
  225. Pass arg to the preprocessor.
  226. .. option:: -o <file>
  227. Write output to file.
  228. .. option:: -print-file-name=<file>
  229. Print the full library path of file.
  230. .. option:: -print-libgcc-file-name
  231. Print the library path for "libgcc.a".
  232. .. option:: -print-prog-name=<name>
  233. Print the full program path of name.
  234. .. option:: -print-search-dirs
  235. Print the paths used for finding libraries and programs.
  236. .. option:: -save-temps
  237. Save intermediate compilation results.
  238. .. option:: -integrated-as, -no-integrated-as
  239. Used to enable and disable, respectively, the use of the integrated
  240. assembler. Whether the integrated assembler is on by default is target
  241. dependent.
  242. .. option:: -time
  243. Time individual commands.
  244. .. option:: -ftime-report
  245. Print timing summary of each stage of compilation.
  246. .. option:: -v
  247. Show commands to run and use verbose output.
  248. Diagnostics Options
  249. ~~~~~~~~~~~~~~~~~~~
  250. .. 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
  251. These options control how Clang prints out information about diagnostics
  252. (errors and warnings). Please see the Clang User's Manual for more information.
  253. Preprocessor Options
  254. ~~~~~~~~~~~~~~~~~~~~
  255. .. option:: -D<macroname>=<value>
  256. Adds an implicit #define into the predefines buffer which is read before the
  257. source file is preprocessed.
  258. .. option:: -U<macroname>
  259. Adds an implicit #undef into the predefines buffer which is read before the
  260. source file is preprocessed.
  261. .. option:: -include <filename>
  262. Adds an implicit #include into the predefines buffer which is read before the
  263. source file is preprocessed.
  264. .. option:: -I<directory>
  265. Add the specified directory to the search path for include files.
  266. .. option:: -F<directory>
  267. Add the specified directory to the search path for framework include files.
  268. .. option:: -nostdinc
  269. Do not search the standard system directories or compiler builtin directories
  270. for include files.
  271. .. option:: -nostdlibinc
  272. Do not search the standard system directories for include files, but do
  273. search compiler builtin include directories.
  274. .. option:: -nobuiltininc
  275. Do not search clang's builtin directory for include files.
  276. ENVIRONMENT
  277. -----------
  278. .. envvar:: TMPDIR, TEMP, TMP
  279. These environment variables are checked, in order, for the location to write
  280. temporary files used during the compilation process.
  281. .. envvar:: CPATH
  282. If this environment variable is present, it is treated as a delimited list of
  283. paths to be added to the default system include path list. The delimiter is
  284. the platform dependent delimiter, as used in the PATH environment variable.
  285. Empty components in the environment variable are ignored.
  286. .. envvar:: C_INCLUDE_PATH, OBJC_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH
  287. These environment variables specify additional paths, as for :envvar:`CPATH`, which are
  288. only used when processing the appropriate language.
  289. .. envvar:: MACOSX_DEPLOYMENT_TARGET
  290. If :option:`-mmacosx-version-min` is unspecified, the default deployment
  291. target is read from this environment variable. This option only affects
  292. Darwin targets.
  293. BUGS
  294. ----
  295. To report bugs, please visit <http://llvm.org/bugs/>. Most bug reports should
  296. include preprocessed source files (use the :option:`-E` option) and the full
  297. output of the compiler, along with information to reproduce.
  298. SEE ALSO
  299. --------
  300. :manpage:`as(1)`, :manpage:`ld(1)`