blob: 50bbbed968b7049ebe06b9798a64af8a04ad890b [file] [log] [blame]
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Clang - Getting Started</title>
<link type="text/css" rel="stylesheet" href="menu.css">
<link type="text/css" rel="stylesheet" href="content.css">
<!--#include virtual="menu.html.incl"-->
<div id="content">
<h1>Getting Started: Building and Running Clang</h1>
<p>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 <a href="get_involved.html">getting
involved</a> with the Clang community. If you run into problems, please file
bugs in <a href="">LLVM Bugzilla</a>.</p>
<h2 id="download">Release Clang Versions</h2>
<p>Clang is released as part of regular LLVM releases. You can download the release versions from <a href=""></a>.</p>
<p>Clang is also provided in all major BSD or GNU/Linux distributions as part of their respective packaging systems. From Xcode 4.2, Clang is the default compiler for Mac OS X.</p>
<h2 id="build">Building Clang and Working with the Code</h2>
<h3 id="buildNix">On Unix-like Systems</h3>
<p>If you would like to check out and build Clang, the current procedure is as
<li>Get the required tools.
<a href="">
Getting Started with the LLVM System - Requirements</a>.</li>
<li>Note also that Python is needed for running the test suite.
Get it at: <a href=""></a></li>
<li>Standard build process uses CMake. Get it at:
<a href=""></a></li>
<li>Check out the LLVM project:
<li>Change directory to where you want the llvm directory placed.</li>
<li><tt>git clone</tt></li>
<li>Build LLVM and Clang:
<li><tt>cd llvm-project</tt></li>
<li><tt>mkdir build</tt> (in-tree build is not supported)</li>
<li><tt>cd build</tt></li>
<li><tt>cmake -DLLVM_ENABLE_PROJECTS=clang -G "Unix Makefiles" ../llvm</tt></li>
<li>This builds both LLVM and Clang for debug mode.</li>
<li>Note: For subsequent Clang development, you can just run
<tt>make clang</tt>.</li>
<li>CMake allows you to generate project files for several IDEs: Xcode,
Eclipse CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator),
KDevelop3. For more details see
<a href="">Building LLVM with CMake</a>
<li>If you intend to use Clang's C++ support, you may need to tell it how
to find your C++ standard library headers. In general, Clang will detect
the best version of libstdc++ headers available and use them - it will
look both for system installations of libstdc++ as well as installations
adjacent to Clang itself. If your configuration fits neither of these
scenarios, you can use the <tt>-DGCC_INSTALL_PREFIX</tt> cmake option
to tell Clang where the gcc containing the desired libstdc++ is installed.
<li>Try it out (assuming you add llvm/build/bin to your path):
<li><tt>clang --help</tt></li>
<li><tt>clang file.c -fsyntax-only</tt> (check for correctness)</li>
<li><tt>clang file.c -S -emit-llvm -o -</tt> (print out unoptimized llvm code)</li>
<li><tt>clang file.c -S -emit-llvm -o - -O3</tt></li>
<li><tt>clang file.c -S -O3 -o -</tt> (output native machine code)</li>
<li>Run the testsuite:
<li><tt>make check-clang</tt></li>
<h3 id="buildWindows">Using Visual Studio</h3>
<p>The following details setting up for and building Clang on Windows using
Visual Studio:</p>
<li>Get the required tools:
<li><b>Git</b>. Source code control program. Get it from:
<a href=""></a></li>
<li><b>CMake</b>. This is used for generating Visual Studio solution and
project files. Get it from:
<a href=""></a></li>
<li><b>Visual Studio 2017 or later</b></li>
<li><b>Python</b>. It is used to run the clang test suite. Get it from:
<a href=""></a></li>
<li><b>GnuWin32 tools</b>
The Clang and LLVM test suite use various GNU core utilities, such as
<tt>grep</tt>, <tt>sed</tt>, and <tt>find</tt>. The gnuwin32 packages
are the oldest and most well-tested way to get these tools. However, the
MSys utilities provided by git for Windows have been known to work.
Cygwin has worked in the past, but is not well tested.
If you don't already have the core utilies from some other source, get
gnuwin32 from <a href=""></a>.</li>
<li>Check out LLVM and Clang:
<li><tt>git clone</tt></li>
<p><em>Note</em>: Some Clang tests are sensitive to the line endings. Ensure
that checking out the files does not convert LF line endings to CR+LF. If
you're using git on Windows, make sure your <tt>core.autocrlf</tt> setting
is false.</p>
<li>Run CMake to generate the Visual Studio solution and project files:
<li><tt>cd ..\..</tt> (back to where you started)</li>
<li><tt>mkdir build</tt> (for building without polluting the source dir)</li>
<li><tt>cd build</tt></li>
If you are using Visual Studio 2017:
<tt>cmake -DLLVM_ENABLE_PROJECTS=clang -G "Visual Studio 15 2017" -A x64 -Thost=x64 ..\llvm</tt><br/>
<tt>-Thost=x64</tt> is required, since the 32-bit linker will run out of memory.
<li>To generate x86 binaries instead of x64, pass <tt>-A Win32</tt>.</li>
<li>See the <a href="">LLVM CMake guide</a> for
more information on other configuration options for CMake.</li>
<li>The above, if successful, will have created an LLVM.sln file in the
<tt>build</tt> directory.
<li>Build Clang:
<li>Open LLVM.sln in Visual Studio.</li>
<li>Build the "clang" project for just the compiler driver and front end, or
the "ALL_BUILD" project to build everything, including tools.</li>
<li>Try it out (assuming you added llvm/debug/bin to your path). (See the
running examples from above.)</li>
<li>See <a href="hacking.html#testingWindows">
Hacking on clang - Testing using Visual Studio on Windows</a> for information
on running regression tests on Windows.</li>
<h3 id="buildWindowsNinja">Using Ninja alongside Visual Studio</h3>
<p>We recommend that developers who want the fastest incremental builds use the
<a href="">Ninja build system</a>. You can use the
generated Visual Studio project files to edit Clang source code and generate a
second build directory next to it for running the tests with these steps:</p>
<li>Check out clang and LLVM as described above</li>
<li>Open a developer command prompt with the appropriate environment.
<li>If you open the start menu and search for "Command Prompt", you should
see shortcuts created by Visual Studio to do this. To use native x64
tools, choose the one titled "x64 Native Tools Command Prompt for VS
<li> Alternatively, launch a regular <tt>cmd</tt> prompt and run the
appropriate vcvarsall.bat incantation. To get the 2017 x64 tools, this
would be:<br/>
<tt>"C:\Program Files (x86)\Microsoft Visual
Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x64</tt>
<li><tt>mkdir build_ninja</tt> (or <tt>build</tt>, or use your own
<li><tt>cd build_ninja</tt></li>
<li><tt>set CC=cl</tt> (necessary to force CMake to choose MSVC over mingw GCC
if you have it installed)</li>
<li><tt>set CXX=cl</tt></li>
<li><tt>cmake -GNinja ..\llvm</tt></li>
<li><tt>ninja clang</tt> This will build just clang.</li>
<li><tt>ninja check-clang</tt> This will run the clang tests.</li>
<h2 id="driver">Clang Compiler Driver (Drop-in Substitute for GCC)</h2>
<p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are
some examples of how to use the high-level driver:
<pre class="code">
$ <b>cat t.c</b>
#include &lt;stdio.h&gt;
int main(int argc, char **argv) { printf("hello world\n"); }
$ <b>clang t.c</b>
$ <b>./a.out</b>
hello world
<p>The 'clang' driver is designed to work as closely to GCC as possible to
maximize portability. The only major difference between the two is that
Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see
weird link-time errors relating to inline functions, try passing -std=gnu89
to clang.</p>
<h2>Examples of using Clang</h2>
<!-- Thanks to
Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
tag. -->
<pre class="code">
$ <b>cat ~/t.c</b>
typedef float V __attribute__((vector_size(16)));
V foo(V a, V b) { return a+b*a; }
<pre class="code">
$ <b>clang ~/t.c -E</b>
# 1 "/Users/sabre/t.c" 1
typedef float V __attribute__((vector_size(16)));
V foo(V a, V b) { return a+b*a; }
<h3>Type checking:</h3>
<pre class="code">
$ <b>clang -fsyntax-only ~/t.c</b>
<h3>GCC options:</h3>
<pre class="code">
$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
/Users/sabre/t.c:2:17: <span style="color:magenta">warning:</span> extension used
<span style="color:darkgreen">typedef float V __attribute__((vector_size(16)));</span>
<span style="color:blue"> ^</span>
1 diagnostic generated.
<h3>Pretty printing from the AST:</h3>
<p>Note, the <tt>-cc1</tt> argument indicates the compiler front-end, and
not the driver, should be run. The compiler front-end has several additional
Clang specific features which are not exposed through the GCC compatible driver
<pre class="code">
$ <b>clang -cc1 ~/t.c -ast-print</b>
typedef float V __attribute__(( vector_size(16) ));
V foo(V a, V b) {
return a + b * a;
<h3>Code generation with LLVM:</h3>
<pre class="code">
$ <b>clang ~/t.c -S -emit-llvm -o -</b>
define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
%mul = mul &lt;4 x float&gt; %b, %a
%add = add &lt;4 x float&gt; %mul, %a
ret &lt;4 x float&gt; %add
$ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
mulps %xmm0, %xmm1
addps %xmm1, %xmm0