# Getting Started: Building and Running Clang

This page gives you the shortest path to checking out Clang and demos a few options. This should get you up and running with the minimum of muss and fuss. If you like what you see, please consider getting involved with the Clang community.

## A Word of Warning

While this work aims to provide a fully functional C/C++/ObjC front-end, it is still relatively new and under heavy development. Currently we believe clang to be very usable as a C and Objective-C compiler, however there is no real C++ support yet (this is obviously a big project). Additionally, for C and Objective-C:

1. The semantic analyzer does not produce all of the warnings it should.
2. We don't consider the API to be stable yet, and reserve the right to change fundamental things.
3. Only the X86-32 and X86-64 targets have been well tested.

If you run into problems, please file bugs in LLVM Bugzilla or bring up the issue on the Clang development mailing list.

## Building Clang and Working with the Code

### On Unix-like Systems

If you would like to check out and build Clang, the current procedure is as follows:

1. Get the required tools.
   - See Getting Started with the LLVM System - Requirements.
   - Note also that Python is needed for running the test suite. Get it at: http://www.python.org/download
2. Checkout and build LLVM from SVN head:

- `svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm`
- `cd llvm`
- `./configure; make`

3. Checkout Clang:

- From within the `llvm` directory (where you built llvm):
- `cd tools`
- `svn co http://llvm.org/svn/llvm-project/cfe/trunk clang`

4. If you intend to work on Clang C++ support, you may need to tell it how to find your C++ standard library headers. If Clang cannot find your system libstdc++ headers, please follow these instructions:

- '`touch empty.cpp; gcc -v empty.cpp -fsyntax-only`' to get the path.
- Look for the comment "FIXME: temporary hack: hard-coded paths" in `clang/lib/Frontend/InitHeaderSearch.cpp` and change the lines below to include that path.

5. Build Clang:

- `cd clang` (assuming that you are in `llvm/tools`)
- `make` (this will give you a debug build)

6. Try it out (assuming you add llvm/Debug/bin to your path):

- `clang-cc --help`
- `clang-cc file.c -fsyntax-only` (check for correctness)
- `clang-cc file.c -ast-dump` (internal debug dump of ast)
- `clang-cc file.c -ast-view` (set up graphviz and rebuild llvm first)
- `clang-cc file.c -emit-llvm` (print out unoptimized llvm code)
- `clang-cc file.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llvm-dis` (print out optimized llvm code)
- `clang-cc file.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llc > file.s` (output native machine code)

Note: Here `clang-cc` is the "low-level" frontend executable that is similar in purpose to `cc1`. Clang also has a high-level compiler driver that acts as a drop-in replacement for `gcc`.

Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you encounter problems with building Clang, make sure you have the latest SVN version of LLVM. LLVM contains support libraries for Clang that will be updated as well as development on Clang progresses.

### Simultaneously Building Clang and LLVM:

Once you have checked out Clang into the llvm source tree it will build along with the rest of `llvm`. To build all of LLVM and Clang together all at once simply run `make` from the root LLVM directory.

Note: Observe that Clang is technically part of a separate Subversion repository. As mentioned above, the latest Clang sources are tied to the latest sources in the LLVM tree. You can update your toplevel LLVM project and all (possibly unrelated) projects inside it with `make update`. This will run `svn update` on all subdirectories related to subversion.

### Using Visual Studio

The following details setting up for and building Clang on Windows using Visual Studio:

1. Get the required tools:

- Subversion. Source code control program. Get it from: http://subversion.tigris.org/getting.html
- cmake. This is used for generating Visual Studio solution and project files. Get it from: http://www.cmake.org/cmake/resources/software.html
- Visual Studio 2005 (VS 2008 may work also - cmake outputs VS2005 project files)
- Python. This is need only if you will be running the tests (which is essential, if you will be developing for clang). Get it from: http://www.python.org/download

2. Checkout LLVM:

- `svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm`

3. Checkout Clang:

- `cd llvm\tools`
- `svn co http://llvm.org/svn/llvm-project/cfe/trunk clang`

4. Run cmake to generate the Visual Studio solution and project files:

- `cd ..` (Change directory back to the llvm top.)
- `cmake .`
- The above, if successful, will have created an LLVM.sln file in the llvm directory.

5. Build Clang:

- Open LLVM.sln in Visual Studio.
- Build the "clang-cc" project for just the compiler front end. Alternatively, build the "clang" project for the compiler driver (note that the driver is currently broken on Windows), or the "ALL_BUILD" project to build everything, including tools.

6. Try it out (assuming you added llvm/debug/bin to your path). (See the running examples from above.)
7. See Hacking on clang - Testing using Visual Studio on Windows for information on running regression tests on Windows.

Note that once you have checked out both llvm and clang, to synchronize to the latest code base, use the `svn update` command in both the llvm and llvm\tools\clang directories, as they are separate repositories.

## High-Level Compiler Driver (Drop-in Substitute for GCC)

While the `clang-cc` executable is a low-level frontend executable that can perform code generation, program analysis, and other actions, it is not designed to be a drop-in replacement for GCC's `cc`. For this purpose, use the high-level driver, aptly named `clang`. Here are some examples of how to use the high-level driver:

    $ cat t.c
    #include <stdio.h>
    int main(int argc, char **argv) { printf("hello world\n"); }
    $ clang t.c
    $ ./a.out
    hello world

## Examples of using Clang

The high-level driver `clang` is designed to understand most of GCC's options, and the lower-level `clang-cc` executable also directly takes many of GCC's options. You can see which options `clang-cc` accepts with '`clang-cc --help`'. Here are a few examples of using `clang` and `clang-cc`:

    $ cat ~/t.c
    typedef float V __attribute__((vector_size(16)));
    V foo(V a, V b) { return a+b*a; }

### Preprocessing:

    $ clang ~/t.c -E
    # 1 "/Users/sabre/t.c" 1

    typedef float V __attribute__((vector_size(16)));

    V foo(V a, V b) { return a+b*a; }

### Type checking:

    $ clang -fsyntax-only ~/t.c

### GCC options:

    $ clang -fsyntax-only ~/t.c -pedantic
    /Users/sabre/t.c:2:17: warning: extension used
    typedef float V __attribute__((vector_size(16)));
                    ^
    1 diagnostic generated.

### Pretty printing from the AST:

    $ clang-cc ~/t.c -ast-print
    typedef float V __attribute__(( vector_size(16) ));
    V foo(V a, V b) {
       return a + b * a;
    }

### Code generation with LLVM:

    $ clang-cc ~/t.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llvm-dis
    define <4 x float> @foo(<4 x float> %a, <4 x float> %b) {
    entry:
             %mul = mul <4 x float> %b, %a
             %add = add <4 x float> %mul, %a
             ret <4 x float> %add
    }
    $ clang-cc ~/t.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llc -march=ppc32 -mcpu=g5
    ..
    _foo:
             vmaddfp v2, v3, v2, v2
             blr
    $ clang-cc ~/t.c -emit-llvm -o - | llvm-as | opt -std-compile-opts | llc -march=x86 -mcpu=yonah
    ..
    _foo:
             mulps %xmm0, %xmm1
             addps %xmm0, %xmm1
             movaps %xmm1, %xmm0
             ret