Clang Compiler User's Manual
- Introduction
- Command Line Options
- Language and Target-Independent Features
- C Language Features
- Objective-C Language Features
- C++ Language Features
- ...
- Objective C++ Language Features
- ...
- Target-Specific Features and Limitations
Introduction
The Clang Compiler is an open-source compiler for the C family of programming languages, aiming to be the best in class implementation of these languages. Clang builds on the LLVM optimizer and code generator, allowing it to provide high-quality optimization and code generation support for many targets. For more general information, please see the Clang Web Site or the LLVM Web Site.
This document describes important notes about using Clang as a compiler for an end-user, documenting the supported features, command line options, etc. If you are interested in using Clang to build a tool that processes code, please see the Clang Internals Manual. If you are interested in the Clang Static Analyzer, please see its web page.
Clang is designed to support the C family of programming languages, which includes C, Objective-C, C++, and Objective-C++ as well as many dialects of those. For language-specific information, please see the corresponding language specific section:
- C Language: K&R C, ANSI C89, ISO C90, ISO C94 (C89+AMD1), ISO C99 (+TC1, TC2, TC3).
- Objective-C Language: ObjC 1, ObjC 2, ObjC 2.1, plus variants depending on base language.
- C++ Language Features
- Objective C++ Language
In addition to these base languages and their dialects, Clang supports a broad variety of language extensions, which are documented in the corresponding language section. These extensions are provided to be compatible with the GCC, Microsoft, and other popular compilers as well as to improve functionality through Clang-specific features. The Clang driver and language features are intentionally designed to be as compatible with the GNU GCC compiler as reasonably possible, easing migration from GCC to Clang. In most cases, code "just works".
In addition to language specific features, Clang has a variety of features that depend on what CPU architecture or operating system is being compiled for. Please see the Target-Specific Features and Limitations section for more details.
The rest of the introduction introduces some basic compiler terminology that is used throughout this manual and contains a basic introduction to using Clang as a command line compiler.
Terminology
Front end, parser, backend, preprocessor, undefined behavior, diagnostic, optimizer
Basic Usage
Intro to how to use a C compiler for newbies.
compile + link compile then link debug info enabling optimizations picking a language to use, defaults to C99 by default. Autosenses based on extension. using a makefile
Command Line Options
This section is generally an index into other sections. It does not go into depth on most. However, the first part introduces the language selection and other high level options like -c, -g, etc.
Language and Target-Independent Features
Controlling Errors and Warnings
Precompiled Headers
Precompiled headers are a general approach employed by many compilers to reduce compilation time. The underlying motivation of the approach is that it is common for the same (and often large) header files to be included by multiple source files. Consequently, compile times can often be greatly improved by caching some of the (redundant) work done by a compiler to process headers. Precompiled header files, which represent one of many ways to implement this optimization, are literally files that represent an on-disk cache that contains the vital information necessary to reduce some of the work needed to process a corresponding header file. While details of precompiled headers vary between compilers, precompiled headers have been shown to be a highly effective at speeding up program compilation on systems with very large system headers (e.g., Mac OS/X).
Clang supports an implementation of precompiled headers known as pre-tokenized headers (PTH). Clang's pre-tokenized headers support most of same interfaces as GCC's pre-compiled headers (as well as others) but are completely different in their implementation. If you are interested in how PTH is implemented, please see the PTH Internals document.
Generating a PTH File
To generate a PTH file using Clang, one invokes Clang with the -x <language>-header option. This mirrors the interface in GCC for generating PCH files:
$ gcc -x c-header test.h -o test.h.gch $ clang -x c-header test.h -o test.h.pth
Using a PTH File
A PTH file can then be used as a prefix header when a -include option is passed to clang:
$ clang -include test.h test.c -o test
The clang driver will first check if a PTH file for test.h is available; if so, the contents of test.h (and the files it includes) will be processed from the PTH file. Otherwise, Clang falls back to directly processing the content of test.h. This mirrors the behavior of GCC.
NOTE: Clang does not automatically used PTH files for headers that are directly included within a source file. For example:
$ clang -x c-header test.h -o test.h.pth $ cat test.c #include "test.h" $ clang test.c -o test
In this example, clang will not automatically use the PTH file for test.h since test.h was included directly in the source file and not specified on the command line using -include.
C Language Features
Intentional Incompatibilities with GCC
No VLAs in structs.
Objective-C Language Features
Intentional Incompatibilities with GCC
No cast of super, no lvalue casts.
C++ Language Features
At this point, Clang C++ is not generally useful. However, Clang C++ support is under active development and is progressing rapidly. Please see the C++ Status page for details or ask on the mailing list about how you can help.
Note that the clang driver will refuse to even try to use clang to compile C++ code unless you pass the -ccc-clang-cxx option to the driver. If you really want to play with Clang's C++ support, please pass that flag.
Objective C++ Language Features
At this point, Clang C++ support is not generally useful (and therefore, neither is Objective-C++). Please see the C++ section for more information.
Target-Specific Features and Limitations
CPU Architectures Features and Limitations
X86
Operating System Features and Limitations
Darwin (Mac OS/X)
No __thread support, 64-bit ObjC support requires SL tools.