blob: 254745b82b5d0cd6a241054383fbf3f43bb65589 [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>
Benjamin Kramereaa262b2012-01-15 15:26:075 <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
Eric Christopher2dc30a5a2008-02-08 06:45:496 <title>Clang - Getting Started</title>
Benjamin Kramereaa262b2012-01-15 15:26:077 <link type="text/css" rel="stylesheet" href="menu.css">
8 <link type="text/css" rel="stylesheet" href="content.css">
Eric Christopher2dc30a5a2008-02-08 06:45:499</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
Sylvestre Ledru7db5ab42014-04-27 15:17:1526<p>Clang is released as part of regular LLVM releases. You can download the release versions from <a href="http://llvm.org/releases/">http://llvm.org/releases/</a>.</p>
27<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>
Daniel Dunbar6b835382010-11-02 22:34:0428
Ted Kremenekb3e8f652009-03-27 16:32:5729<h2 id="build">Building Clang and Working with the Code</h2>
Eric Christopher2dc30a5a2008-02-08 06:45:4930
Eli Friedman05194922009-08-03 19:42:2831<h3 id="buildNix">On Unix-like Systems</h3>
32
Ted Kremenekb3e8f652009-03-27 16:32:5733<p>If you would like to check out and build Clang, the current procedure is as
34follows:</p>
Eric Christopher2dc30a5a2008-02-08 06:45:4935
36<ol>
Anton Korobeynikov3ebc08b2009-08-06 13:00:0837 <li>Get the required tools.
38 <ul>
39 <li>See
40 <a href="http://llvm.org/docs/GettingStarted.html#requirements">
41 Getting Started with the LLVM System - Requirements</a>.</li>
42 <li>Note also that Python is needed for running the test suite.
43 Get it at: <a href="http://www.python.org/download">
44 http://www.python.org/download</a></li>
Alexey Samsonovec0bff92015-04-21 18:38:3145 <li>Standard build process uses CMake. Get it at:
46 <a href="http://www.cmake.org/download">
47 http://www.cmake.org/download</a></li>
Anton Korobeynikov3ebc08b2009-08-06 13:00:0848 </ul>
49
Benjamin Kramereaa262b2012-01-15 15:26:0750 <li>Checkout LLVM:
Eric Christopher2dc30a5a2008-02-08 06:45:4951 <ul>
Chris Lattnerf6bad862009-08-20 06:17:1152 <li>Change directory to where you want the llvm directory placed.</li>
Eric Christopher2dc30a5a2008-02-08 06:45:4953 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
Eric Christopher2dc30a5a2008-02-08 06:45:4954 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:0755 </li>
56 <li>Checkout Clang:
Eric Christopher2dc30a5a2008-02-08 06:45:4957 <ul>
Chandler Carruth18d67452012-08-14 00:41:3058 <li><tt>cd llvm/tools</tt></li>
Chris Lattnerf6bad862009-08-20 06:17:1159 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
Chandler Carruth18d67452012-08-14 00:41:3060 <li><tt>cd ../..</tt></li>
Chris Lattnerf6bad862009-08-20 06:17:1161 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:0762 </li>
Chandler Carruth72af8c32012-08-14 07:19:0963 <li>Checkout extra Clang Tools: (optional)
64 <ul>
65 <li><tt>cd llvm/tools/clang/tools</tt></li>
66 <li><tt>svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk
67 extra</tt></li>
68 <li><tt>cd ../../../..</tt></li>
69 </ul>
70 </li>
Benjamin Kramereaa262b2012-01-15 15:26:0771 <li>Checkout Compiler-RT:
Daniel Dunbar2931cd92011-12-07 03:04:4272 <ul>
Chandler Carruth18d67452012-08-14 00:41:3073 <li><tt>cd llvm/projects</tt></li>
Daniel Dunbar2931cd92011-12-07 03:04:4274 <li><tt>svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk
75 compiler-rt</tt></li>
Chandler Carruth18d67452012-08-14 00:41:3076 <li><tt>cd ../..</tt></li>
Daniel Dunbar2931cd92011-12-07 03:04:4277 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:0778 </li>
79 <li>Build LLVM and Clang:
Chris Lattnerf6bad862009-08-20 06:17:1180 <ul>
Alexey Samsonovec0bff92015-04-21 18:38:3181 <li><tt>mkdir build</tt> (in-tree build is not supported)</li>
Rafael Espindola4f8857e2010-10-29 22:05:1782 <li><tt>cd build</tt></li>
Alexey Samsonovec0bff92015-04-21 18:38:3183 <li><tt>cmake -G "Unix Makefiles" ../llvm</tt></li>
Chris Lattnerf6bad862009-08-20 06:17:1184 <li><tt>make</tt></li>
85 <li>This builds both LLVM and Clang for debug mode.</li>
Alexey Samsonovec0bff92015-04-21 18:38:3186 <li>Note: For subsequent Clang development, you can just run
87 <tt>make clang</tt>.</li>
88 <li>CMake allows you to generate project files for several IDEs: Xcode,
89 Eclipse CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator),
90 KDevelop3. For more details see
91 <a href="http://llvm.org/docs/CMake.html">Building LLVM with CMake</a>
92 page.</li>
93 <li>You can also build Clang with
94 <a href="http://llvm.org/docs/BuildingLLVMWithAutotools.html">
95 autotools</a>, but some features may be unavailable there.</li>
Eric Christopher2dc30a5a2008-02-08 06:45:4996 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:0797 </li>
Chris Lattner6eeb2212010-05-06 21:57:5798
Richard Smith9deb9d92013-03-12 19:56:0999 <li>If you intend to use Clang's C++ support, you may need to tell it how
Chandler Carruthcc312312012-05-16 08:18:58100 to find your C++ standard library headers. In general, Clang will detect
101 the best version of libstdc++ headers available and use them - it will
102 look both for system installations of libstdc++ as well as installations
103 adjacent to Clang itself. If your configuration fits neither of these
104 scenarios, you can use the <tt>--with-gcc-toolchain</tt> configure option
105 to tell Clang where the gcc containing the desired libstdc++ is installed.
Benjamin Kramereaa262b2012-01-15 15:26:07106 </li>
107 <li>Try it out (assuming you add llvm/Debug+Asserts/bin to your path):
Eric Christopher2dc30a5a2008-02-08 06:45:49108 <ul>
Daniel Dunbar520d1e62009-12-11 23:04:35109 <li><tt>clang --help</tt></li>
110 <li><tt>clang file.c -fsyntax-only</tt> (check for correctness)</li>
111 <li><tt>clang file.c -S -emit-llvm -o -</tt> (print out unoptimized llvm code)</li>
112 <li><tt>clang file.c -S -emit-llvm -o - -O3</tt></li>
113 <li><tt>clang file.c -S -O3 -o -</tt> (output native machine code)</li>
Eric Christopher2dc30a5a2008-02-08 06:45:49114 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07115 </li>
Eric Christopher2dc30a5a2008-02-08 06:45:49116</ol>
117
Ted Kremenekb3e8f652009-03-27 16:32:57118<p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
119encounter problems with building Clang, make sure you have the latest SVN
120version of LLVM. LLVM contains support libraries for Clang that will be updated
121as well as development on Clang progresses.</p>
Ted Kremenek09d6ba12008-02-08 07:32:26122
Ted Kremenekb3e8f652009-03-27 16:32:57123<h3>Simultaneously Building Clang and LLVM:</h3>
Eric Christopher6aa87cb2008-02-08 07:10:48124
Ted Kremenekb3e8f652009-03-27 16:32:57125<p>Once you have checked out Clang into the llvm source tree it will build along
126with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
127once simply run <tt>make</tt> from the root LLVM directory.</p>
128
129<p><em>Note:</em> Observe that Clang is technically part of a separate
130Subversion repository. As mentioned above, the latest Clang sources are tied to
131the latest sources in the LLVM tree. You can update your toplevel LLVM project
132and all (possibly unrelated) projects inside it with <tt><b>make
133update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
134to subversion. </p>
Eric Christopher2dc30a5a2008-02-08 06:45:49135
Eli Friedman05194922009-08-03 19:42:28136<h3 id="buildWindows">Using Visual Studio</h3>
137
138<p>The following details setting up for and building Clang on Windows using
139Visual Studio:</p>
140
141<ol>
Benjamin Kramereaa262b2012-01-15 15:26:07142 <li>Get the required tools:
Eli Friedman05194922009-08-03 19:42:28143 <ul>
144 <li><b>Subversion</b>. Source code control program. Get it from:
Yaron Keren9f887022015-05-07 15:15:16145 <a href="http://subversion.apache.org/packages.html">
146 http://subversion.apache.org/packages.html</a></li>
NAKAMURA Takumia9f3c0f2012-11-27 23:34:08147 <li><b>CMake</b>. This is used for generating Visual Studio solution and
Eli Friedman05194922009-08-03 19:42:28148 project files. Get it from:
149 <a href="http://www.cmake.org/cmake/resources/software.html">
150 http://www.cmake.org/cmake/resources/software.html</a></li>
Yaron Keren37f99ed2015-05-07 15:12:30151 <li><b>Visual Studio 2013 or later</b></li>
Chris Lattnerf6bad862009-08-20 06:17:11152 <li><b>Python</b>. This is needed only if you will be running the tests
Eli Friedman05194922009-08-03 19:42:28153 (which is essential, if you will be developing for clang).
154 Get it from:
NAKAMURA Takumi823f3eb2011-03-27 02:04:21155 <a href="http://www.python.org/download/">
156 http://www.python.org/download/</a></li>
Chris Lattnerf6bad862009-08-20 06:17:11157 <li><b>GnuWin32 tools</b>
158 These are also necessary for running the tests.
159 (Note that the grep from MSYS or Cygwin doesn't work with the tests
160 because of embedded double-quotes in the search strings. The GNU
161 grep does work in this case.)
NAKAMURA Takumi823f3eb2011-03-27 02:04:21162 Get them from <a href="http://getgnuwin32.sourceforge.net/">
163 http://getgnuwin32.sourceforge.net/</a>.</li>
Eli Friedman05194922009-08-03 19:42:28164 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07165 </li>
Eli Friedman05194922009-08-03 19:42:28166
Benjamin Kramereaa262b2012-01-15 15:26:07167 <li>Checkout LLVM:
Eli Friedman05194922009-08-03 19:42:28168 <ul>
169 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
170 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07171 </li>
172 <li>Checkout Clang:
Eli Friedman05194922009-08-03 19:42:28173 <ul>
174 <li><tt>cd llvm\tools</tt>
175 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
176 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07177 </li>
NAKAMURA Takumia9f3c0f2012-11-27 23:34:08178 <li>Run CMake to generate the Visual Studio solution and project files:
Eli Friedman05194922009-08-03 19:42:28179 <ul>
Michael J. Spencer7ed7afc2010-12-16 22:01:14180 <li><tt>cd ..\..</tt> (back to where you started)</li>
181 <li><tt>mkdir build</tt> (for building without polluting the source dir)</li>
182 <li><tt>cd build</tt></li>
Yaron Keren37f99ed2015-05-07 15:12:30183 <li>If you are using Visual Studio 2013: <tt>cmake -G "Visual Studio 12" ..\llvm</tt></li>
John Thompson9bfba162011-04-06 18:22:12184 <li>See the <a href="http://www.llvm.org/docs/CMake.html">LLVM CMake guide</a> for
NAKAMURA Takumia9f3c0f2012-11-27 23:34:08185 more information on other configuration options for CMake.</li>
Eli Friedman05194922009-08-03 19:42:28186 <li>The above, if successful, will have created an LLVM.sln file in the
NAKAMURA Takumib44cd822011-07-25 12:40:26187 <tt>build</tt> directory.
Eli Friedman05194922009-08-03 19:42:28188 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07189 </li>
190 <li>Build Clang:
Eli Friedman05194922009-08-03 19:42:28191 <ul>
192 <li>Open LLVM.sln in Visual Studio.</li>
Daniel Dunbar520d1e62009-12-11 23:04:35193 <li>Build the "clang" project for just the compiler driver and front end, or
194 the "ALL_BUILD" project to build everything, including tools.</li>
Eli Friedman05194922009-08-03 19:42:28195 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07196 </li>
Eli Friedman05194922009-08-03 19:42:28197 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
198 running examples from above.)</li>
199 <li>See <a href="hacking.html#testingWindows">
200 Hacking on clang - Testing using Visual Studio on Windows</a> for information
201 on running regression tests on Windows.</li>
202</ol>
203
204<p>Note that once you have checked out both llvm and clang, to synchronize
205to the latest code base, use the <tt>svn update</tt> command in both the
206llvm and llvm\tools\clang directories, as they are separate repositories.</p>
207
Benjamin Kramereaa262b2012-01-15 15:26:07208<h2 id="driver">Clang Compiler Driver (Drop-in Substitute for GCC)</h2>
Ted Kremenekb3e8f652009-03-27 16:32:57209
Daniel Dunbar520d1e62009-12-11 23:04:35210<p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
211designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are
212some examples of how to use the high-level driver:
Ted Kremenekb3e8f652009-03-27 16:32:57213</p>
214
215<pre class="code">
216$ <b>cat t.c</b>
217#include &lt;stdio.h&gt;
218int main(int argc, char **argv) { printf("hello world\n"); }
219$ <b>clang t.c</b>
220$ <b>./a.out</b>
221hello world
222</pre>
223
Chris Lattnere5eb7262009-11-09 03:21:02224<p>The 'clang' driver is designed to work as closely to GCC as possible to
225 maximize portability. The only major difference between the two is that
226 Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see
Chris Lattnere6e64be2009-11-09 04:04:07227 weird link-time errors relating to inline functions, try passing -std=gnu89
Chris Lattnere5eb7262009-11-09 03:21:02228 to clang.</p>
229
Ted Kremenekb3e8f652009-03-27 16:32:57230<h2>Examples of using Clang</h2>
231
Eric Christopher2dc30a5a2008-02-08 06:45:49232<!-- Thanks to
233 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
234Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
235tag. -->
236
237<pre class="code">
238$ <b>cat ~/t.c</b>
239typedef float V __attribute__((vector_size(16)));
240V foo(V a, V b) { return a+b*a; }
241</pre>
242
243
Ted Kremenek77251e92008-06-17 13:48:36244<h3>Preprocessing:</h3>
Eric Christopher2dc30a5a2008-02-08 06:45:49245
246<pre class="code">
247$ <b>clang ~/t.c -E</b>
248# 1 "/Users/sabre/t.c" 1
249
250typedef float V __attribute__((vector_size(16)));
251
252V foo(V a, V b) { return a+b*a; }
253</pre>
254
255
Ted Kremenek77251e92008-06-17 13:48:36256<h3>Type checking:</h3>
Eric Christopher2dc30a5a2008-02-08 06:45:49257
258<pre class="code">
259$ <b>clang -fsyntax-only ~/t.c</b>
260</pre>
261
262
Ted Kremenek77251e92008-06-17 13:48:36263<h3>GCC options:</h3>
Eric Christopher2dc30a5a2008-02-08 06:45:49264
265<pre class="code">
266$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
Benjamin Kramereaa262b2012-01-15 15:26:07267/Users/sabre/t.c:2:17: <span style="color:magenta">warning:</span> extension used
268<span style="color:darkgreen">typedef float V __attribute__((vector_size(16)));</span>
269<span style="color:blue"> ^</span>
Eric Christopher2dc30a5a2008-02-08 06:45:492701 diagnostic generated.
271</pre>
272
273
Ted Kremenek77251e92008-06-17 13:48:36274<h3>Pretty printing from the AST:</h3>
Eric Christopher2dc30a5a2008-02-08 06:45:49275
Sylvestre Ledru830885c2012-07-23 08:59:39276<p>Note, the <tt>-cc1</tt> argument indicates the compiler front-end, and
Daniel Dunbar520d1e62009-12-11 23:04:35277not the driver, should be run. The compiler front-end has several additional
278Clang specific features which are not exposed through the GCC compatible driver
279interface.</p>
280
Eric Christopher2dc30a5a2008-02-08 06:45:49281<pre class="code">
Daniel Dunbar520d1e62009-12-11 23:04:35282$ <b>clang -cc1 ~/t.c -ast-print</b>
Eric Christopher2dc30a5a2008-02-08 06:45:49283typedef float V __attribute__(( vector_size(16) ));
284V foo(V a, V b) {
285 return a + b * a;
286}
287</pre>
288
289
Ted Kremenek77251e92008-06-17 13:48:36290<h3>Code generation with LLVM:</h3>
Eric Christopher2dc30a5a2008-02-08 06:45:49291
292<pre class="code">
Daniel Dunbar520d1e62009-12-11 23:04:35293$ <b>clang ~/t.c -S -emit-llvm -o -</b>
Eric Christopher2dc30a5a2008-02-08 06:45:49294define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
295entry:
296 %mul = mul &lt;4 x float&gt; %b, %a
297 %add = add &lt;4 x float&gt; %mul, %a
298 ret &lt;4 x float&gt; %add
299}
Daniel Dunbar520d1e62009-12-11 23:04:35300$ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
301...
Eric Christopher2dc30a5a2008-02-08 06:45:49302_foo:
Daniel Dunbar520d1e62009-12-11 23:04:35303Leh_func_begin1:
304 mulps %xmm0, %xmm1
305 addps %xmm1, %xmm0
306 ret
307Leh_func_end1:
Eric Christopher2dc30a5a2008-02-08 06:45:49308</pre>
309
Eric Christopher2dc30a5a2008-02-08 06:45:49310</div>
311</body>
312</html>