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 very early work and is under heavy development. In particular, there is no real C++ support yet (this is obviously a big project), and C/ObjC support is still missing some features. Some of the more notable missing pieces of C support are:

  1. The semantic analyzer does not produce all of the warnings and errors it should.
  2. The LLVM code generator is still missing important features. clang is not ready to be used as a general purpose C code generator yet, but if you hit problems and report them to cfe-dev, we'll fix them :).
  3. We don't consider the API to be stable yet, and reserve the right to change fundamental things.

Our plan is to continue chipping away at these issues until C works really well, but we'd love help from other interested contributors. We expect C to be in good shape by mid to late 2008.

Building clang / working with the code

If you would like to check out and build the project, the current scheme is:

  1. Checkout and build LLVM from SVN head:
  2. Checkout clang:
  3. Non-mac users: Paths to system header files are currently hard coded into clang; as a result, if clang can't find your system headers, please follow these instructions:
  4. Build clang:
  5. Try it out (assuming you add llvm/Debug/bin to your path):

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.

Examples of using clang

The clang driver takes a lot of GCC compatible options, which you can see with 'clang --help'. Here are a few examples:

$ 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 ~/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 ~/t.c -emit-llvm | 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 ~/t.c -emit-llvm | llvm-as | opt -std-compile-opts | llc -march=ppc32 -mcpu=g5
..
_foo:
         vmaddfp v2, v3, v2, v2
         blr
$ clang ~/t.c -emit-llvm | llvm-as | opt -std-compile-opts | llc -march=x86 -mcpu=yonah
..
_foo:
         mulps %xmm0, %xmm1
         addps %xmm0, %xmm1
         movaps %xmm1, %xmm0
         ret

GCC "Emulation" Driver

While the clang executable is a compiler driver that can perform code generation, program analysis, and other actions, it is not designed to be a drop-in replacement for GCC's cc. There is interest in developing such a driver for clang, but in the interim the clang source tree includes a Python script ccc in the utils subdirectory that provides some of this functionality (the script is intended to be used where GCC's cc could be used). It is currently a work in progress, and eventually will likely be replaced by a more complete driver.

Example use:

$ ccc t.c
clang -emit-llvm-bc -o t.o -U__GNUC__ t.c
llvm-ld -native -o a.out t.o
$ ls
a.out a.out.bc t.c t.o