blob: 515cd57ce259e301db614d42b7487af85ffe7c1a [file] [log] [blame]
Eric Christopher2dc30a5a2008-02-08 06:45:491<!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 Kremenekb3e8f652009-03-27 16:32:5718<p>This page gives you the shortest path to checking out Clang and demos a few
Eric Christopher2dc30a5a2008-02-08 06:45:4919options. 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:1821involved</a> with the Clang community. If you run into problems, please file
Douglas Gregora8810102010-10-07 20:20:5722bugs in <a href="http://llvm.org/bugs/">LLVM Bugzilla</a>.</p>
Eric Christopher2dc30a5a2008-02-08 06:45:4923
Daniel Dunbar6b835382010-11-02 22:34:0424<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:5730<h2 id="build">Building Clang and Working with the Code</h2>
Eric Christopher2dc30a5a2008-02-08 06:45:4931
Eli Friedman05194922009-08-03 19:42:2832<h3 id="buildNix">On Unix-like Systems</h3>
33
Ted Kremenekb3e8f652009-03-27 16:32:5734<p>If you would like to check out and build Clang, the current procedure is as
35follows:</p>
Eric Christopher2dc30a5a2008-02-08 06:45:4936
37<ol>
Anton Korobeynikov3ebc08b2009-08-06 13:00:0838 <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 Lattnerf6bad862009-08-20 06:17:1148 <li>Checkout LLVM:</li>
Eric Christopher2dc30a5a2008-02-08 06:45:4949 <ul>
Chris Lattnerf6bad862009-08-20 06:17:1150 <li>Change directory to where you want the llvm directory placed.</li>
Eric Christopher2dc30a5a2008-02-08 06:45:4951 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
Eric Christopher2dc30a5a2008-02-08 06:45:4952 </ul>
Ted Kremenekb3e8f652009-03-27 16:32:5753 <li>Checkout Clang:</li>
Eric Christopher2dc30a5a2008-02-08 06:45:4954 <ul>
Chris Lattnerf6bad862009-08-20 06:17:1155 <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 Espindola4f8857e2010-10-29 22:05:1760 <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 Lattnerf6bad862009-08-20 06:17:1165 <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 Christopher2dc30a5a2008-02-08 06:45:4969 </ul>
Chris Lattner6eeb2212010-05-06 21:57:5770
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 Kremenekb3e8f652009-03-27 16:32:5775 <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 Lattner5013d3a2009-03-10 16:01:4477 system libstdc++ headers, please follow these instructions:</li>
Eric Christopher2dc30a5a2008-02-08 06:45:4978 <ul>
Gabor Greifbf82f94d2010-03-29 21:36:0679 <li>'<tt>gcc -v -x c++ /dev/null -fsyntax-only</tt>' to get the
Eric Christopher2dc30a5a2008-02-08 06:45:4980 path.</li>
81 <li>Look for the comment "FIXME: temporary hack:
Chris Lattner5013d3a2009-03-10 16:01:4482 hard-coded paths" in <tt>clang/lib/Frontend/InitHeaderSearch.cpp</tt> and
Eric Christopher2dc30a5a2008-02-08 06:45:4983 change the lines below to include that path.</li>
84 </ul>
Chris Lattner76f0cf42010-07-26 22:51:0085 <li>Try it out (assuming you add llvm/Debug+Asserts/bin to your path):</li>
Eric Christopher2dc30a5a2008-02-08 06:45:4986 <ul>
Daniel Dunbar520d1e62009-12-11 23:04:3587 <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 Christopher2dc30a5a2008-02-08 06:45:4992 </ul>
93</ol>
94
Ted Kremenekb3e8f652009-03-27 16:32:5795<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 Kremenek09d6ba12008-02-08 07:32:2699
Ted Kremenekb3e8f652009-03-27 16:32:57100<h3>Simultaneously Building Clang and LLVM:</h3>
Eric Christopher6aa87cb2008-02-08 07:10:48101
Ted Kremenekb3e8f652009-03-27 16:32:57102<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 Christopher2dc30a5a2008-02-08 06:45:49112
Eli Friedman05194922009-08-03 19:42:28113<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>
Michael J. Spencer7ed7afc2010-12-16 22:01:14128 <li><b>Visual Studio 2005, 2008, or 2010</b></li>
Chris Lattnerf6bad862009-08-20 06:17:11129 <li><b>Python</b>. This is needed only if you will be running the tests
Eli Friedman05194922009-08-03 19:42:28130 (which is essential, if you will be developing for clang).
131 Get it from:
NAKAMURA Takumi823f3eb2011-03-27 02:04:21132 <a href="http://www.python.org/download/">
133 http://www.python.org/download/</a></li>
Chris Lattnerf6bad862009-08-20 06:17:11134 <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.)
NAKAMURA Takumi823f3eb2011-03-27 02:04:21139 Get them from <a href="http://getgnuwin32.sourceforge.net/">
140 http://getgnuwin32.sourceforge.net/</a>.</li>
Eli Friedman05194922009-08-03 19:42:28141 </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>
Michael J. Spencer7ed7afc2010-12-16 22:01:14154 <li><tt>cd ..\..</tt> (back to where you started)</li>
155 <li><tt>mkdir build</tt> (for building without polluting the source dir)</li>
156 <li><tt>cd build</tt></li>
157 <li>If you are using Visual Studio 2005: <tt>cmake -G "Visual Studio 8 2005" ..\llvm</tt></li>
158 <li>Or if you are using Visual Studio 2008: <tt>cmake -G "Visual Studio 9 2008" ..\llvm</tt></li>
159 <li>Or if you are using Visual Studio 2010: <tt>cmake -G "Visual Studio 10" ..\llvm</tt></li>
John Thompson9bfba162011-04-06 18:22:12160 <li>By default, cmake will target LLVM to X86. If you want all targets
161 (needed if you want to run the LLVM tests), add the <tt>-DLLVM_TARGETS_TO_BUILD=all</tt> option to the
162 cmake command line. Or specify a target from the LLVM_TARGETS_TO_BUILD
163 definition in CMakeLists.txt.</li>
164 <li>See the <a href="http://www.llvm.org/docs/CMake.html">LLVM CMake guide</a> for
165 more information on other configuration options for cmake.</li>
166</li>
Eli Friedman05194922009-08-03 19:42:28167 <li>The above, if successful, will have created an LLVM.sln file in the
NAKAMURA Takumib44cd822011-07-25 12:40:26168 <tt>build</tt> directory.
Eli Friedman05194922009-08-03 19:42:28169 </ul>
170 <li>Build Clang:</li>
171 <ul>
172 <li>Open LLVM.sln in Visual Studio.</li>
Daniel Dunbar520d1e62009-12-11 23:04:35173 <li>Build the "clang" project for just the compiler driver and front end, or
174 the "ALL_BUILD" project to build everything, including tools.</li>
Eli Friedman05194922009-08-03 19:42:28175 </ul>
176 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
177 running examples from above.)</li>
178 <li>See <a href="hacking.html#testingWindows">
179 Hacking on clang - Testing using Visual Studio on Windows</a> for information
180 on running regression tests on Windows.</li>
181</ol>
182
183<p>Note that once you have checked out both llvm and clang, to synchronize
184to the latest code base, use the <tt>svn update</tt> command in both the
185llvm and llvm\tools\clang directories, as they are separate repositories.</p>
186
Daniel Dunbar520d1e62009-12-11 23:04:35187<a name="driver"><h2>Clang Compiler Driver (Drop-in Substitute for GCC)</h2></a>
Ted Kremenekb3e8f652009-03-27 16:32:57188
Daniel Dunbar520d1e62009-12-11 23:04:35189<p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
190designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are
191some examples of how to use the high-level driver:
Ted Kremenekb3e8f652009-03-27 16:32:57192</p>
193
194<pre class="code">
195$ <b>cat t.c</b>
196#include &lt;stdio.h&gt;
197int main(int argc, char **argv) { printf("hello world\n"); }
198$ <b>clang t.c</b>
199$ <b>./a.out</b>
200hello world
201</pre>
202
Chris Lattnere5eb7262009-11-09 03:21:02203<p>The 'clang' driver is designed to work as closely to GCC as possible to
204 maximize portability. The only major difference between the two is that
205 Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see
Chris Lattnere6e64be2009-11-09 04:04:07206 weird link-time errors relating to inline functions, try passing -std=gnu89
Chris Lattnere5eb7262009-11-09 03:21:02207 to clang.</p>
208
Ted Kremenekb3e8f652009-03-27 16:32:57209<h2>Examples of using Clang</h2>
210
Eric Christopher2dc30a5a2008-02-08 06:45:49211<!-- Thanks to
212 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
213Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
214tag. -->
215
216<pre class="code">
217$ <b>cat ~/t.c</b>
218typedef float V __attribute__((vector_size(16)));
219V foo(V a, V b) { return a+b*a; }
220</pre>
221
222
Ted Kremenek77251e92008-06-17 13:48:36223<h3>Preprocessing:</h3>
Eric Christopher2dc30a5a2008-02-08 06:45:49224
225<pre class="code">
226$ <b>clang ~/t.c -E</b>
227# 1 "/Users/sabre/t.c" 1
228
229typedef float V __attribute__((vector_size(16)));
230
231V foo(V a, V b) { return a+b*a; }
232</pre>
233
234
Ted Kremenek77251e92008-06-17 13:48:36235<h3>Type checking:</h3>
Eric Christopher2dc30a5a2008-02-08 06:45:49236
237<pre class="code">
238$ <b>clang -fsyntax-only ~/t.c</b>
239</pre>
240
241
Ted Kremenek77251e92008-06-17 13:48:36242<h3>GCC options:</h3>
Eric Christopher2dc30a5a2008-02-08 06:45:49243
244<pre class="code">
245$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
246/Users/sabre/t.c:2:17: warning: extension used
247typedef float V __attribute__((vector_size(16)));
248 ^
2491 diagnostic generated.
250</pre>
251
252
Ted Kremenek77251e92008-06-17 13:48:36253<h3>Pretty printing from the AST:</h3>
Eric Christopher2dc30a5a2008-02-08 06:45:49254
Daniel Dunbar520d1e62009-12-11 23:04:35255<p>Note, the <tt>-cc1</tt> argument indicates the the compiler front-end, and
256not the driver, should be run. The compiler front-end has several additional
257Clang specific features which are not exposed through the GCC compatible driver
258interface.</p>
259
Eric Christopher2dc30a5a2008-02-08 06:45:49260<pre class="code">
Daniel Dunbar520d1e62009-12-11 23:04:35261$ <b>clang -cc1 ~/t.c -ast-print</b>
Eric Christopher2dc30a5a2008-02-08 06:45:49262typedef float V __attribute__(( vector_size(16) ));
263V foo(V a, V b) {
264 return a + b * a;
265}
266</pre>
267
268
Ted Kremenek77251e92008-06-17 13:48:36269<h3>Code generation with LLVM:</h3>
Eric Christopher2dc30a5a2008-02-08 06:45:49270
271<pre class="code">
Daniel Dunbar520d1e62009-12-11 23:04:35272$ <b>clang ~/t.c -S -emit-llvm -o -</b>
Eric Christopher2dc30a5a2008-02-08 06:45:49273define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
274entry:
275 %mul = mul &lt;4 x float&gt; %b, %a
276 %add = add &lt;4 x float&gt; %mul, %a
277 ret &lt;4 x float&gt; %add
278}
Daniel Dunbar520d1e62009-12-11 23:04:35279$ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
280...
Eric Christopher2dc30a5a2008-02-08 06:45:49281_foo:
Daniel Dunbar520d1e62009-12-11 23:04:35282Leh_func_begin1:
283 mulps %xmm0, %xmm1
284 addps %xmm1, %xmm0
285 ret
286Leh_func_end1:
Eric Christopher2dc30a5a2008-02-08 06:45:49287</pre>
288
Eric Christopher2dc30a5a2008-02-08 06:45:49289</div>
290</body>
291</html>