blob: 7460cd1ff3ebc04ecbff9217eb48cc9cb7d20476 [file] [log] [blame]
Eric Christopher7dc0e572008-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>
5 <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
6 <title>Clang - Getting Started</title>
7 <link type="text/css" rel="stylesheet" href="menu.css" />
8 <link type="text/css" rel="stylesheet" href="content.css" />
9</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 Kremenek9b16da32009-03-27 16:32:57 +000018<p>This page gives you the shortest path to checking out Clang and demos a few
Eric Christopher7dc0e572008-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 Lattnerd2c1c602009-11-09 03:18:18 +000021involved</a> with the Clang community. If you run into problems, please file
Douglas Gregor9ff3e662010-10-07 20:20:57 +000022bugs in <a href="http://llvm.org/bugs/">LLVM Bugzilla</a>.</p>
Eric Christopher7dc0e572008-02-08 06:45:49 +000023
Daniel Dunbarfa8e11b2010-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 Kremenek9b16da32009-03-27 16:32:57 +000030<h2 id="build">Building Clang and Working with the Code</h2>
Eric Christopher7dc0e572008-02-08 06:45:49 +000031
Eli Friedmand1e1ef32009-08-03 19:42:28 +000032<h3 id="buildNix">On Unix-like Systems</h3>
33
Ted Kremenek9b16da32009-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 Christopher7dc0e572008-02-08 06:45:49 +000036
37<ol>
Anton Korobeynikov1816e482009-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
Chris Lattner357f7ce2009-08-20 06:17:11 +000048 <li>Checkout LLVM:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000049 <ul>
Chris Lattner357f7ce2009-08-20 06:17:11 +000050 <li>Change directory to where you want the llvm directory placed.</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000051 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000052 </ul>
Ted Kremenek9b16da32009-03-27 16:32:57 +000053 <li>Checkout Clang:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000054 <ul>
Chris Lattner357f7ce2009-08-20 06:17:11 +000055 <li><tt>cd llvm/tools</tt>
56 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
57 </ul>
58 <li>Build LLVM and Clang:</li>
59 <ul>
Rafael Espindolafa5fa972010-10-29 22:05:17 +000060 <li><tt>cd ../..</tt> (back to where you started)</li>
61 <li><tt>mkdir build</tt> (for building without polluting the source dir)
62 </li>
63 <li><tt>cd build</tt></li>
64 <li><tt>../llvm/configure</tt></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +000065 <li><tt>make</tt></li>
66 <li>This builds both LLVM and Clang for debug mode.</li>
67 <li>Note: For subsequent Clang development, you can just do make at the
68 clang directory level.</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000069 </ul>
Chris Lattner78781782010-05-06 21:57:57 +000070
71 <p>It is also possible to use CMake instead of the makefiles. With CMake it
72 is also possible to generate project files for several IDEs: Eclipse CDT4,
73 CodeBlocks, Qt-Creator (use the CodeBlocks generator), KDevelop3.</p>
74
Ted Kremenek9b16da32009-03-27 16:32:57 +000075 <li>If you intend to work on Clang C++ support, you may need to tell it how
76 to find your C++ standard library headers. If Clang cannot find your
Chris Lattnerca8c49f2009-03-10 16:01:44 +000077 system libstdc++ headers, please follow these instructions:</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000078 <ul>
Gabor Greif74350822010-03-29 21:36:06 +000079 <li>'<tt>gcc -v -x c++ /dev/null -fsyntax-only</tt>' to get the
Eric Christopher7dc0e572008-02-08 06:45:49 +000080 path.</li>
81 <li>Look for the comment "FIXME: temporary hack:
Chris Lattnerca8c49f2009-03-10 16:01:44 +000082 hard-coded paths" in <tt>clang/lib/Frontend/InitHeaderSearch.cpp</tt> and
Eric Christopher7dc0e572008-02-08 06:45:49 +000083 change the lines below to include that path.</li>
84 </ul>
Chris Lattnerbe9a5ca2010-07-26 22:51:00 +000085 <li>Try it out (assuming you add llvm/Debug+Asserts/bin to your path):</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000086 <ul>
Daniel Dunbardd63b282009-12-11 23:04:35 +000087 <li><tt>clang --help</tt></li>
88 <li><tt>clang file.c -fsyntax-only</tt> (check for correctness)</li>
89 <li><tt>clang file.c -S -emit-llvm -o -</tt> (print out unoptimized llvm code)</li>
90 <li><tt>clang file.c -S -emit-llvm -o - -O3</tt></li>
91 <li><tt>clang file.c -S -O3 -o -</tt> (output native machine code)</li>
Eric Christopher7dc0e572008-02-08 06:45:49 +000092 </ul>
93</ol>
94
Ted Kremenek9b16da32009-03-27 16:32:57 +000095<p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
96encounter problems with building Clang, make sure you have the latest SVN
97version of LLVM. LLVM contains support libraries for Clang that will be updated
98as well as development on Clang progresses.</p>
Ted Kremenek675a9ba2008-02-08 07:32:26 +000099
Ted Kremenek9b16da32009-03-27 16:32:57 +0000100<h3>Simultaneously Building Clang and LLVM:</h3>
Eric Christopher562b4f32008-02-08 07:10:48 +0000101
Ted Kremenek9b16da32009-03-27 16:32:57 +0000102<p>Once you have checked out Clang into the llvm source tree it will build along
103with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
104once simply run <tt>make</tt> from the root LLVM directory.</p>
105
106<p><em>Note:</em> Observe that Clang is technically part of a separate
107Subversion repository. As mentioned above, the latest Clang sources are tied to
108the latest sources in the LLVM tree. You can update your toplevel LLVM project
109and all (possibly unrelated) projects inside it with <tt><b>make
110update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
111to subversion. </p>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000112
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000113<h3 id="buildWindows">Using Visual Studio</h3>
114
115<p>The following details setting up for and building Clang on Windows using
116Visual Studio:</p>
117
118<ol>
119 <li>Get the required tools:</li>
120 <ul>
121 <li><b>Subversion</b>. Source code control program. Get it from:
122 <a href="http://subversion.tigris.org/getting.html">
123 http://subversion.tigris.org/getting.html</a></li>
124 <li><b>cmake</b>. This is used for generating Visual Studio solution and
125 project files. Get it from:
126 <a href="http://www.cmake.org/cmake/resources/software.html">
127 http://www.cmake.org/cmake/resources/software.html</a></li>
Cedric Venet6bfc8b62009-09-27 10:34:36 +0000128 <li><b>Visual Studio 2005 or 2008</b></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000129 <li><b>Python</b>. This is needed only if you will be running the tests
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000130 (which is essential, if you will be developing for clang).
131 Get it from:
132 <a href="http://www.python.org/download">
133 http://www.python.org/download</a></li>
Chris Lattner357f7ce2009-08-20 06:17:11 +0000134 <li><b>GnuWin32 tools</b>
135 These are also necessary for running the tests.
136 (Note that the grep from MSYS or Cygwin doesn't work with the tests
137 because of embedded double-quotes in the search strings. The GNU
138 grep does work in this case.)
139 Get them from <a href="http://getgnuwin32.sourceforge.net">
140 http://getgnuwin32.sourceforge.net</a>.</li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000141 </ul>
142
143 <li>Checkout LLVM:</li>
144 <ul>
145 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
146 </ul>
147 <li>Checkout Clang:</li>
148 <ul>
149 <li><tt>cd llvm\tools</tt>
150 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
151 </ul>
152 <li>Run cmake to generate the Visual Studio solution and project files:</li>
153 <ul>
154 <li><tt>cd ..</tt> (Change directory back to the llvm top.)</li>
John Thompson99ff8da2009-11-06 00:06:29 +0000155 <li>If you are using Visual Studio 2005: <tt>cmake .</tt></li>
156 <li>Or if you are using Visual Studio 2008: <tt>cmake -G "Visual Studio 9 2008" .</tt></li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000157 <li>The above, if successful, will have created an LLVM.sln file in the
158 llvm directory.
159 </ul>
160 <li>Build Clang:</li>
161 <ul>
162 <li>Open LLVM.sln in Visual Studio.</li>
Daniel Dunbardd63b282009-12-11 23:04:35 +0000163 <li>Build the "clang" project for just the compiler driver and front end, or
164 the "ALL_BUILD" project to build everything, including tools.</li>
Eli Friedmand1e1ef32009-08-03 19:42:28 +0000165 </ul>
166 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
167 running examples from above.)</li>
168 <li>See <a href="hacking.html#testingWindows">
169 Hacking on clang - Testing using Visual Studio on Windows</a> for information
170 on running regression tests on Windows.</li>
171</ol>
172
173<p>Note that once you have checked out both llvm and clang, to synchronize
174to the latest code base, use the <tt>svn update</tt> command in both the
175llvm and llvm\tools\clang directories, as they are separate repositories.</p>
176
Daniel Dunbardd63b282009-12-11 23:04:35 +0000177<a name="driver"><h2>Clang Compiler Driver (Drop-in Substitute for GCC)</h2></a>
Ted Kremenek9b16da32009-03-27 16:32:57 +0000178
Daniel Dunbardd63b282009-12-11 23:04:35 +0000179<p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
180designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are
181some examples of how to use the high-level driver:
Ted Kremenek9b16da32009-03-27 16:32:57 +0000182</p>
183
184<pre class="code">
185$ <b>cat t.c</b>
186#include &lt;stdio.h&gt;
187int main(int argc, char **argv) { printf("hello world\n"); }
188$ <b>clang t.c</b>
189$ <b>./a.out</b>
190hello world
191</pre>
192
Chris Lattner63d423d2009-11-09 03:21:02 +0000193<p>The 'clang' driver is designed to work as closely to GCC as possible to
194 maximize portability. The only major difference between the two is that
195 Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see
Chris Lattnerd73fef62009-11-09 04:04:07 +0000196 weird link-time errors relating to inline functions, try passing -std=gnu89
Chris Lattner63d423d2009-11-09 03:21:02 +0000197 to clang.</p>
198
Ted Kremenek9b16da32009-03-27 16:32:57 +0000199<h2>Examples of using Clang</h2>
200
Eric Christopher7dc0e572008-02-08 06:45:49 +0000201<!-- Thanks to
202 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
203Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
204tag. -->
205
206<pre class="code">
207$ <b>cat ~/t.c</b>
208typedef float V __attribute__((vector_size(16)));
209V foo(V a, V b) { return a+b*a; }
210</pre>
211
212
Ted Kremenek35f29c52008-06-17 13:48:36 +0000213<h3>Preprocessing:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000214
215<pre class="code">
216$ <b>clang ~/t.c -E</b>
217# 1 "/Users/sabre/t.c" 1
218
219typedef float V __attribute__((vector_size(16)));
220
221V foo(V a, V b) { return a+b*a; }
222</pre>
223
224
Ted Kremenek35f29c52008-06-17 13:48:36 +0000225<h3>Type checking:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000226
227<pre class="code">
228$ <b>clang -fsyntax-only ~/t.c</b>
229</pre>
230
231
Ted Kremenek35f29c52008-06-17 13:48:36 +0000232<h3>GCC options:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000233
234<pre class="code">
235$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
236/Users/sabre/t.c:2:17: warning: extension used
237typedef float V __attribute__((vector_size(16)));
238 ^
2391 diagnostic generated.
240</pre>
241
242
Ted Kremenek35f29c52008-06-17 13:48:36 +0000243<h3>Pretty printing from the AST:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000244
Daniel Dunbardd63b282009-12-11 23:04:35 +0000245<p>Note, the <tt>-cc1</tt> argument indicates the the compiler front-end, and
246not the driver, should be run. The compiler front-end has several additional
247Clang specific features which are not exposed through the GCC compatible driver
248interface.</p>
249
Eric Christopher7dc0e572008-02-08 06:45:49 +0000250<pre class="code">
Daniel Dunbardd63b282009-12-11 23:04:35 +0000251$ <b>clang -cc1 ~/t.c -ast-print</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000252typedef float V __attribute__(( vector_size(16) ));
253V foo(V a, V b) {
254 return a + b * a;
255}
256</pre>
257
258
Ted Kremenek35f29c52008-06-17 13:48:36 +0000259<h3>Code generation with LLVM:</h3>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000260
261<pre class="code">
Daniel Dunbardd63b282009-12-11 23:04:35 +0000262$ <b>clang ~/t.c -S -emit-llvm -o -</b>
Eric Christopher7dc0e572008-02-08 06:45:49 +0000263define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
264entry:
265 %mul = mul &lt;4 x float&gt; %b, %a
266 %add = add &lt;4 x float&gt; %mul, %a
267 ret &lt;4 x float&gt; %add
268}
Daniel Dunbardd63b282009-12-11 23:04:35 +0000269$ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
270...
Eric Christopher7dc0e572008-02-08 06:45:49 +0000271_foo:
Daniel Dunbardd63b282009-12-11 23:04:35 +0000272Leh_func_begin1:
273 mulps %xmm0, %xmm1
274 addps %xmm1, %xmm0
275 ret
276Leh_func_end1:
Eric Christopher7dc0e572008-02-08 06:45:49 +0000277</pre>
278
Eric Christopher7dc0e572008-02-08 06:45:49 +0000279</div>
280</body>
281</html>