blob: 512833143b33de9a5acf340127e6ffd66b2f3163 [file] [log] [blame]
Eric Christopher2dc30a52008-02-08 06:45:49 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
Benjamin Kramereaa262b2012-01-15 15:26:07 +00005 <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
Eric Christopher2dc30a52008-02-08 06:45:49 +00006 <title>Clang - Getting Started</title>
Benjamin Kramereaa262b2012-01-15 15:26:07 +00007 <link type="text/css" rel="stylesheet" href="menu.css">
8 <link type="text/css" rel="stylesheet" href="content.css">
Eric Christopher2dc30a52008-02-08 06:45:49 +00009</head>
10<body>
11
12<!--#include virtual="menu.html.incl"-->
13
14<div id="content">
15
16<h1>Getting Started: Building and Running Clang</h1>
17
Ted Kremenekb3e8f652009-03-27 16:32:57 +000018<p>This page gives you the shortest path to checking out Clang and demos a few
Eric Christopher2dc30a52008-02-08 06:45:49 +000019options. This should get you up and running with the minimum of muss and fuss.
20If you like what you see, please consider <a href="get_involved.html">getting
Chris Lattnerb8ca9542009-11-09 03:18:18 +000021involved</a> with the Clang community. If you run into problems, please file
Douglas Gregora8810102010-10-07 20:20:57 +000022bugs in <a href="http://llvm.org/bugs/">LLVM Bugzilla</a>.</p>
Eric Christopher2dc30a52008-02-08 06:45:49 +000023
Daniel Dunbar6b835382010-11-02 22:34:04 +000024<h2 id="download">Release Clang Versions</h2>
25
26<p>Clang has been released as part of regular LLVM releases since LLVM 2.6. You
27can download the release versions
28from <a href="http://llvm.org/releases/">http://llvm.org/releases/</a>.</p>
29
Ted Kremenekb3e8f652009-03-27 16:32:57 +000030<h2 id="build">Building Clang and Working with the Code</h2>
Eric Christopher2dc30a52008-02-08 06:45:49 +000031
Eli Friedman05194922009-08-03 19:42:28 +000032<h3 id="buildNix">On Unix-like Systems</h3>
33
Ted Kremenekb3e8f652009-03-27 16:32:57 +000034<p>If you would like to check out and build Clang, the current procedure is as
35follows:</p>
Eric Christopher2dc30a52008-02-08 06:45:49 +000036
37<ol>
Anton Korobeynikov3ebc08b2009-08-06 13:00:08 +000038 <li>Get the required tools.
39 <ul>
40 <li>See
41 <a href="http://llvm.org/docs/GettingStarted.html#requirements">
42 Getting Started with the LLVM System - Requirements</a>.</li>
43 <li>Note also that Python is needed for running the test suite.
44 Get it at: <a href="http://www.python.org/download">
45 http://www.python.org/download</a></li>
46 </ul>
47
Benjamin Kramereaa262b2012-01-15 15:26:07 +000048 <li>Checkout LLVM:
Eric Christopher2dc30a52008-02-08 06:45:49 +000049 <ul>
Chris Lattnerf6bad862009-08-20 06:17:11 +000050 <li>Change directory to where you want the llvm directory placed.</li>
Eric Christopher2dc30a52008-02-08 06:45:49 +000051 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
Eric Christopher2dc30a52008-02-08 06:45:49 +000052 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000053 </li>
54 <li>Checkout Clang:
Eric Christopher2dc30a52008-02-08 06:45:49 +000055 <ul>
Chandler Carruth18d67452012-08-14 00:41:30 +000056 <li><tt>cd llvm/tools</tt></li>
Chris Lattnerf6bad862009-08-20 06:17:11 +000057 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
Chandler Carruth18d67452012-08-14 00:41:30 +000058 <li><tt>cd ../..</tt></li>
Chris Lattnerf6bad862009-08-20 06:17:11 +000059 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000060 </li>
Chandler Carruth72af8c32012-08-14 07:19:09 +000061 <li>Checkout extra Clang Tools: (optional)
62 <ul>
63 <li><tt>cd llvm/tools/clang/tools</tt></li>
64 <li><tt>svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk
65 extra</tt></li>
66 <li><tt>cd ../../../..</tt></li>
67 </ul>
68 </li>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000069 <li>Checkout Compiler-RT:
Daniel Dunbar2931cd92011-12-07 03:04:42 +000070 <ul>
Chandler Carruth18d67452012-08-14 00:41:30 +000071 <li><tt>cd llvm/projects</tt></li>
Daniel Dunbar2931cd92011-12-07 03:04:42 +000072 <li><tt>svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk
73 compiler-rt</tt></li>
Chandler Carruth18d67452012-08-14 00:41:30 +000074 <li><tt>cd ../..</tt></li>
Daniel Dunbar2931cd92011-12-07 03:04:42 +000075 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000076 </li>
77 <li>Build LLVM and Clang:
Chris Lattnerf6bad862009-08-20 06:17:11 +000078 <ul>
Rafael Espindola4f8857e2010-10-29 22:05:17 +000079 <li><tt>mkdir build</tt> (for building without polluting the source dir)
80 </li>
81 <li><tt>cd build</tt></li>
82 <li><tt>../llvm/configure</tt></li>
Chris Lattnerf6bad862009-08-20 06:17:11 +000083 <li><tt>make</tt></li>
84 <li>This builds both LLVM and Clang for debug mode.</li>
85 <li>Note: For subsequent Clang development, you can just do make at the
86 clang directory level.</li>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000087 <li>It is also possible to use CMake instead of the makefiles. With CMake
NAKAMURA Takumi5e745102012-11-27 23:34:28 +000088 it is possible to generate project files for several IDEs: Xcode, Eclipse
Benjamin Kramereaa262b2012-01-15 15:26:07 +000089 CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator), KDevelop3.</li>
Eric Christopher2dc30a52008-02-08 06:45:49 +000090 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000091 </li>
Chris Lattner6eeb2212010-05-06 21:57:57 +000092
Richard Smith9deb9d92013-03-12 19:56:09 +000093 <li>If you intend to use Clang's C++ support, you may need to tell it how
Chandler Carruthcc312312012-05-16 08:18:58 +000094 to find your C++ standard library headers. In general, Clang will detect
95 the best version of libstdc++ headers available and use them - it will
96 look both for system installations of libstdc++ as well as installations
97 adjacent to Clang itself. If your configuration fits neither of these
98 scenarios, you can use the <tt>--with-gcc-toolchain</tt> configure option
99 to tell Clang where the gcc containing the desired libstdc++ is installed.
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000100 </li>
101 <li>Try it out (assuming you add llvm/Debug+Asserts/bin to your path):
Eric Christopher2dc30a52008-02-08 06:45:49 +0000102 <ul>
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000103 <li><tt>clang --help</tt></li>
104 <li><tt>clang file.c -fsyntax-only</tt> (check for correctness)</li>
105 <li><tt>clang file.c -S -emit-llvm -o -</tt> (print out unoptimized llvm code)</li>
106 <li><tt>clang file.c -S -emit-llvm -o - -O3</tt></li>
107 <li><tt>clang file.c -S -O3 -o -</tt> (output native machine code)</li>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000108 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000109 </li>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000110</ol>
111
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000112<p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
113encounter problems with building Clang, make sure you have the latest SVN
114version of LLVM. LLVM contains support libraries for Clang that will be updated
115as well as development on Clang progresses.</p>
Ted Kremenek09d6ba12008-02-08 07:32:26 +0000116
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000117<h3>Simultaneously Building Clang and LLVM:</h3>
Eric Christopher6aa87cb2008-02-08 07:10:48 +0000118
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000119<p>Once you have checked out Clang into the llvm source tree it will build along
120with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
121once simply run <tt>make</tt> from the root LLVM directory.</p>
122
123<p><em>Note:</em> Observe that Clang is technically part of a separate
124Subversion repository. As mentioned above, the latest Clang sources are tied to
125the latest sources in the LLVM tree. You can update your toplevel LLVM project
126and all (possibly unrelated) projects inside it with <tt><b>make
127update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
128to subversion. </p>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000129
Eli Friedman05194922009-08-03 19:42:28 +0000130<h3 id="buildWindows">Using Visual Studio</h3>
131
132<p>The following details setting up for and building Clang on Windows using
133Visual Studio:</p>
134
135<ol>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000136 <li>Get the required tools:
Eli Friedman05194922009-08-03 19:42:28 +0000137 <ul>
138 <li><b>Subversion</b>. Source code control program. Get it from:
139 <a href="http://subversion.tigris.org/getting.html">
140 http://subversion.tigris.org/getting.html</a></li>
NAKAMURA Takumia9f3c0f2012-11-27 23:34:08 +0000141 <li><b>CMake</b>. This is used for generating Visual Studio solution and
Eli Friedman05194922009-08-03 19:42:28 +0000142 project files. Get it from:
143 <a href="http://www.cmake.org/cmake/resources/software.html">
144 http://www.cmake.org/cmake/resources/software.html</a></li>
Chandler Carruth74f0df42011-11-16 19:29:07 +0000145 <li><b>Visual Studio 2008 or 2010</b></li>
Chris Lattnerf6bad862009-08-20 06:17:11 +0000146 <li><b>Python</b>. This is needed only if you will be running the tests
Eli Friedman05194922009-08-03 19:42:28 +0000147 (which is essential, if you will be developing for clang).
148 Get it from:
NAKAMURA Takumi823f3eb2011-03-27 02:04:21 +0000149 <a href="http://www.python.org/download/">
150 http://www.python.org/download/</a></li>
Chris Lattnerf6bad862009-08-20 06:17:11 +0000151 <li><b>GnuWin32 tools</b>
152 These are also necessary for running the tests.
153 (Note that the grep from MSYS or Cygwin doesn't work with the tests
154 because of embedded double-quotes in the search strings. The GNU
155 grep does work in this case.)
NAKAMURA Takumi823f3eb2011-03-27 02:04:21 +0000156 Get them from <a href="http://getgnuwin32.sourceforge.net/">
157 http://getgnuwin32.sourceforge.net/</a>.</li>
Eli Friedman05194922009-08-03 19:42:28 +0000158 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000159 </li>
Eli Friedman05194922009-08-03 19:42:28 +0000160
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000161 <li>Checkout LLVM:
Eli Friedman05194922009-08-03 19:42:28 +0000162 <ul>
163 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
164 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000165 </li>
166 <li>Checkout Clang:
Eli Friedman05194922009-08-03 19:42:28 +0000167 <ul>
168 <li><tt>cd llvm\tools</tt>
169 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
170 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000171 </li>
NAKAMURA Takumia9f3c0f2012-11-27 23:34:08 +0000172 <li>Run CMake to generate the Visual Studio solution and project files:
Eli Friedman05194922009-08-03 19:42:28 +0000173 <ul>
Michael J. Spencer7ed7afc2010-12-16 22:01:14 +0000174 <li><tt>cd ..\..</tt> (back to where you started)</li>
175 <li><tt>mkdir build</tt> (for building without polluting the source dir)</li>
176 <li><tt>cd build</tt></li>
Chandler Carruth74f0df42011-11-16 19:29:07 +0000177 <li>If you are using Visual Studio 2008: <tt>cmake -G "Visual Studio 9 2008" ..\llvm</tt></li>
Michael J. Spencer7ed7afc2010-12-16 22:01:14 +0000178 <li>Or if you are using Visual Studio 2010: <tt>cmake -G "Visual Studio 10" ..\llvm</tt></li>
John Thompson9bfba162011-04-06 18:22:12 +0000179 <li>See the <a href="http://www.llvm.org/docs/CMake.html">LLVM CMake guide</a> for
NAKAMURA Takumia9f3c0f2012-11-27 23:34:08 +0000180 more information on other configuration options for CMake.</li>
Eli Friedman05194922009-08-03 19:42:28 +0000181 <li>The above, if successful, will have created an LLVM.sln file in the
NAKAMURA Takumib44cd822011-07-25 12:40:26 +0000182 <tt>build</tt> directory.
Eli Friedman05194922009-08-03 19:42:28 +0000183 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000184 </li>
185 <li>Build Clang:
Eli Friedman05194922009-08-03 19:42:28 +0000186 <ul>
187 <li>Open LLVM.sln in Visual Studio.</li>
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000188 <li>Build the "clang" project for just the compiler driver and front end, or
189 the "ALL_BUILD" project to build everything, including tools.</li>
Eli Friedman05194922009-08-03 19:42:28 +0000190 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000191 </li>
Eli Friedman05194922009-08-03 19:42:28 +0000192 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
193 running examples from above.)</li>
194 <li>See <a href="hacking.html#testingWindows">
195 Hacking on clang - Testing using Visual Studio on Windows</a> for information
196 on running regression tests on Windows.</li>
197</ol>
198
199<p>Note that once you have checked out both llvm and clang, to synchronize
200to the latest code base, use the <tt>svn update</tt> command in both the
201llvm and llvm\tools\clang directories, as they are separate repositories.</p>
202
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000203<h2 id="driver">Clang Compiler Driver (Drop-in Substitute for GCC)</h2>
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000204
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000205<p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
206designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are
207some examples of how to use the high-level driver:
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000208</p>
209
210<pre class="code">
211$ <b>cat t.c</b>
212#include &lt;stdio.h&gt;
213int main(int argc, char **argv) { printf("hello world\n"); }
214$ <b>clang t.c</b>
215$ <b>./a.out</b>
216hello world
217</pre>
218
Chris Lattnere5eb7262009-11-09 03:21:02 +0000219<p>The 'clang' driver is designed to work as closely to GCC as possible to
220 maximize portability. The only major difference between the two is that
221 Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see
Chris Lattnere6e64be2009-11-09 04:04:07 +0000222 weird link-time errors relating to inline functions, try passing -std=gnu89
Chris Lattnere5eb7262009-11-09 03:21:02 +0000223 to clang.</p>
224
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000225<h2>Examples of using Clang</h2>
226
Eric Christopher2dc30a52008-02-08 06:45:49 +0000227<!-- Thanks to
228 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
229Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
230tag. -->
231
232<pre class="code">
233$ <b>cat ~/t.c</b>
234typedef float V __attribute__((vector_size(16)));
235V foo(V a, V b) { return a+b*a; }
236</pre>
237
238
Ted Kremenek77251e92008-06-17 13:48:36 +0000239<h3>Preprocessing:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000240
241<pre class="code">
242$ <b>clang ~/t.c -E</b>
243# 1 "/Users/sabre/t.c" 1
244
245typedef float V __attribute__((vector_size(16)));
246
247V foo(V a, V b) { return a+b*a; }
248</pre>
249
250
Ted Kremenek77251e92008-06-17 13:48:36 +0000251<h3>Type checking:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000252
253<pre class="code">
254$ <b>clang -fsyntax-only ~/t.c</b>
255</pre>
256
257
Ted Kremenek77251e92008-06-17 13:48:36 +0000258<h3>GCC options:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000259
260<pre class="code">
261$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000262/Users/sabre/t.c:2:17: <span style="color:magenta">warning:</span> extension used
263<span style="color:darkgreen">typedef float V __attribute__((vector_size(16)));</span>
264<span style="color:blue"> ^</span>
Eric Christopher2dc30a52008-02-08 06:45:49 +00002651 diagnostic generated.
266</pre>
267
268
Ted Kremenek77251e92008-06-17 13:48:36 +0000269<h3>Pretty printing from the AST:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000270
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000271<p>Note, the <tt>-cc1</tt> argument indicates the compiler front-end, and
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000272not the driver, should be run. The compiler front-end has several additional
273Clang specific features which are not exposed through the GCC compatible driver
274interface.</p>
275
Eric Christopher2dc30a52008-02-08 06:45:49 +0000276<pre class="code">
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000277$ <b>clang -cc1 ~/t.c -ast-print</b>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000278typedef float V __attribute__(( vector_size(16) ));
279V foo(V a, V b) {
280 return a + b * a;
281}
282</pre>
283
284
Ted Kremenek77251e92008-06-17 13:48:36 +0000285<h3>Code generation with LLVM:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000286
287<pre class="code">
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000288$ <b>clang ~/t.c -S -emit-llvm -o -</b>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000289define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
290entry:
291 %mul = mul &lt;4 x float&gt; %b, %a
292 %add = add &lt;4 x float&gt; %mul, %a
293 ret &lt;4 x float&gt; %add
294}
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000295$ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
296...
Eric Christopher2dc30a52008-02-08 06:45:49 +0000297_foo:
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000298Leh_func_begin1:
299 mulps %xmm0, %xmm1
300 addps %xmm1, %xmm0
301 ret
302Leh_func_end1:
Eric Christopher2dc30a52008-02-08 06:45:49 +0000303</pre>
304
Eric Christopher2dc30a52008-02-08 06:45:49 +0000305</div>
306</body>
307</html>