blob: 0dc8f5324071342f0e00371ed7f2294d25420e0b [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
Sylvestre Ledrub06fe482018-09-22 07:39:44 +000022bugs in <a href="https://bugs.llvm.org/">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
Sylvestre Ledrub06fe482018-09-22 07:39:44 +000026<p>Clang is released as part of regular LLVM releases. You can download the release versions from <a href="https://llvm.org/releases/">https://llvm.org/releases/</a>.</p>
Sylvestre Ledru7db5ab42014-04-27 15:17:15 +000027<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:04 +000028
Ted Kremenekb3e8f652009-03-27 16:32:57 +000029<h2 id="build">Building Clang and Working with the Code</h2>
Eric Christopher2dc30a52008-02-08 06:45:49 +000030
Eli Friedman05194922009-08-03 19:42:28 +000031<h3 id="buildNix">On Unix-like Systems</h3>
32
Sylvestre Ledrub06fe482018-09-22 07:39:44 +000033<p>Note: as an experimental setup, you can use a <b>single checkout</b> with all the projects, and an <b>easy CMake invocation</b>, see the LLVM Doc "<a href="https://llvm.org/docs/GettingStarted.html#for-developers-to-work-with-a-git-monorepo">For developers to work with a git monorepo</a>"</p>
Mehdi Amini0f5b90c2016-11-12 01:18:17 +000034
Ted Kremenekb3e8f652009-03-27 16:32:57 +000035<p>If you would like to check out and build Clang, the current procedure is as
36follows:</p>
Eric Christopher2dc30a52008-02-08 06:45:49 +000037
38<ol>
Anton Korobeynikov3ebc08b2009-08-06 13:00:08 +000039 <li>Get the required tools.
40 <ul>
41 <li>See
Sylvestre Ledrub06fe482018-09-22 07:39:44 +000042 <a href="https://llvm.org/docs/GettingStarted.html#requirements">
Anton Korobeynikov3ebc08b2009-08-06 13:00:08 +000043 Getting Started with the LLVM System - Requirements</a>.</li>
44 <li>Note also that Python is needed for running the test suite.
45 Get it at: <a href="http://www.python.org/download">
46 http://www.python.org/download</a></li>
Alexey Samsonovec0bff92015-04-21 18:38:31 +000047 <li>Standard build process uses CMake. Get it at:
48 <a href="http://www.cmake.org/download">
49 http://www.cmake.org/download</a></li>
Anton Korobeynikov3ebc08b2009-08-06 13:00:08 +000050 </ul>
51
Adrian McCarthy02d0a632016-04-20 16:43:34 +000052 <li>Check out LLVM:
Eric Christopher2dc30a52008-02-08 06:45:49 +000053 <ul>
Chris Lattnerf6bad862009-08-20 06:17:11 +000054 <li>Change directory to where you want the llvm directory placed.</li>
Eric Christopher2dc30a52008-02-08 06:45:49 +000055 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
Eric Christopher2dc30a52008-02-08 06:45:49 +000056 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000057 </li>
Adrian McCarthy02d0a632016-04-20 16:43:34 +000058 <li>Check out Clang:
Eric Christopher2dc30a52008-02-08 06:45:49 +000059 <ul>
Chandler Carruth18d67452012-08-14 00:41:30 +000060 <li><tt>cd llvm/tools</tt></li>
Chris Lattnerf6bad862009-08-20 06:17:11 +000061 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
Chandler Carruth18d67452012-08-14 00:41:30 +000062 <li><tt>cd ../..</tt></li>
Chris Lattnerf6bad862009-08-20 06:17:11 +000063 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000064 </li>
Adrian McCarthy02d0a632016-04-20 16:43:34 +000065 <li>Check out extra Clang tools: (optional)
Chandler Carruth72af8c32012-08-14 07:19:09 +000066 <ul>
67 <li><tt>cd llvm/tools/clang/tools</tt></li>
68 <li><tt>svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk
69 extra</tt></li>
70 <li><tt>cd ../../../..</tt></li>
71 </ul>
72 </li>
Adrian McCarthy02d0a632016-04-20 16:43:34 +000073 <li>Check out Compiler-RT (optional):
Daniel Dunbar2931cd92011-12-07 03:04:42 +000074 <ul>
Chandler Carruth18d67452012-08-14 00:41:30 +000075 <li><tt>cd llvm/projects</tt></li>
Daniel Dunbar2931cd92011-12-07 03:04:42 +000076 <li><tt>svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk
77 compiler-rt</tt></li>
Chandler Carruth18d67452012-08-14 00:41:30 +000078 <li><tt>cd ../..</tt></li>
Daniel Dunbar2931cd92011-12-07 03:04:42 +000079 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000080 </li>
Adrian McCarthy02d0a632016-04-20 16:43:34 +000081 <li>Check out libcxx: (only required to build and run Compiler-RT tests on OS X, optional otherwise)
Yury Gribovdaace1b2015-09-15 15:05:47 +000082 <ul>
83 <li><tt>cd llvm/projects</tt></li>
84 <li><tt>svn co http://llvm.org/svn/llvm-project/libcxx/trunk
85 libcxx</tt></li>
86 <li><tt>cd ../..</tt></li>
87 </ul>
88 </li>
Benjamin Kramereaa262b2012-01-15 15:26:07 +000089 <li>Build LLVM and Clang:
Chris Lattnerf6bad862009-08-20 06:17:11 +000090 <ul>
Alexey Samsonovec0bff92015-04-21 18:38:31 +000091 <li><tt>mkdir build</tt> (in-tree build is not supported)</li>
Rafael Espindola4f8857e2010-10-29 22:05:17 +000092 <li><tt>cd build</tt></li>
Alexey Samsonovec0bff92015-04-21 18:38:31 +000093 <li><tt>cmake -G "Unix Makefiles" ../llvm</tt></li>
Chris Lattnerf6bad862009-08-20 06:17:11 +000094 <li><tt>make</tt></li>
95 <li>This builds both LLVM and Clang for debug mode.</li>
Alexey Samsonovec0bff92015-04-21 18:38:31 +000096 <li>Note: For subsequent Clang development, you can just run
97 <tt>make clang</tt>.</li>
98 <li>CMake allows you to generate project files for several IDEs: Xcode,
99 Eclipse CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator),
100 KDevelop3. For more details see
Sylvestre Ledrub06fe482018-09-22 07:39:44 +0000101 <a href="https://llvm.org/docs/CMake.html">Building LLVM with CMake</a>
Alexey Samsonovec0bff92015-04-21 18:38:31 +0000102 page.</li>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000103 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000104 </li>
Chris Lattner6eeb2212010-05-06 21:57:57 +0000105
Richard Smith9deb9d92013-03-12 19:56:09 +0000106 <li>If you intend to use Clang's C++ support, you may need to tell it how
Chandler Carruthcc312312012-05-16 08:18:58 +0000107 to find your C++ standard library headers. In general, Clang will detect
108 the best version of libstdc++ headers available and use them - it will
109 look both for system installations of libstdc++ as well as installations
110 adjacent to Clang itself. If your configuration fits neither of these
Justin Bogner78a9db72015-12-10 20:52:59 +0000111 scenarios, you can use the <tt>-DGCC_INSTALL_PREFIX</tt> cmake option
Chandler Carruthcc312312012-05-16 08:18:58 +0000112 to tell Clang where the gcc containing the desired libstdc++ is installed.
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000113 </li>
Reid Kleckner88cbe5a2016-04-11 17:02:34 +0000114 <li>Try it out (assuming you add llvm/build/bin to your path):
Eric Christopher2dc30a52008-02-08 06:45:49 +0000115 <ul>
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000116 <li><tt>clang --help</tt></li>
117 <li><tt>clang file.c -fsyntax-only</tt> (check for correctness)</li>
118 <li><tt>clang file.c -S -emit-llvm -o -</tt> (print out unoptimized llvm code)</li>
119 <li><tt>clang file.c -S -emit-llvm -o - -O3</tt></li>
120 <li><tt>clang file.c -S -O3 -o -</tt> (output native machine code)</li>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000121 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000122 </li>
Sylvestre Ledru16f4ebb2016-09-26 13:22:34 +0000123 <li>Run the testsuite:
124 <ul>
125 <li><tt>make check-clang</tt></li>
126 </ul>
127 </li>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000128</ol>
129
Reid Kleckner88cbe5a2016-04-11 17:02:34 +0000130<p>If you encounter problems while building Clang, make sure that your LLVM
131checkout is at the same revision as your Clang checkout. LLVM's interfaces
132change over time, and mismatched revisions are not expected to work
133together.</p>
Adrian McCarthy02d0a632016-04-20 16:43:34 +0000134
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000135<h3>Simultaneously Building Clang and LLVM:</h3>
Eric Christopher6aa87cb2008-02-08 07:10:48 +0000136
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000137<p>Once you have checked out Clang into the llvm source tree it will build along
138with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
139once simply run <tt>make</tt> from the root LLVM directory.</p>
Adrian McCarthy02d0a632016-04-20 16:43:34 +0000140
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000141<p><em>Note:</em> Observe that Clang is technically part of a separate
142Subversion repository. As mentioned above, the latest Clang sources are tied to
143the latest sources in the LLVM tree. You can update your toplevel LLVM project
144and all (possibly unrelated) projects inside it with <tt><b>make
145update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
146to subversion. </p>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000147
Eli Friedman05194922009-08-03 19:42:28 +0000148<h3 id="buildWindows">Using Visual Studio</h3>
149
150<p>The following details setting up for and building Clang on Windows using
151Visual Studio:</p>
152
153<ol>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000154 <li>Get the required tools:
Eli Friedman05194922009-08-03 19:42:28 +0000155 <ul>
156 <li><b>Subversion</b>. Source code control program. Get it from:
Yaron Keren9f887022015-05-07 15:15:16 +0000157 <a href="http://subversion.apache.org/packages.html">
158 http://subversion.apache.org/packages.html</a></li>
NAKAMURA Takumia9f3c0f2012-11-27 23:34:08 +0000159 <li><b>CMake</b>. This is used for generating Visual Studio solution and
Eli Friedman05194922009-08-03 19:42:28 +0000160 project files. Get it from:
161 <a href="http://www.cmake.org/cmake/resources/software.html">
162 http://www.cmake.org/cmake/resources/software.html</a></li>
Yaron Keren37f99ed2015-05-07 15:12:30 +0000163 <li><b>Visual Studio 2013 or later</b></li>
Chris Lattnerf6bad862009-08-20 06:17:11 +0000164 <li><b>Python</b>. This is needed only if you will be running the tests
Eli Friedman05194922009-08-03 19:42:28 +0000165 (which is essential, if you will be developing for clang).
166 Get it from:
NAKAMURA Takumi823f3eb2011-03-27 02:04:21 +0000167 <a href="http://www.python.org/download/">
168 http://www.python.org/download/</a></li>
Chris Lattnerf6bad862009-08-20 06:17:11 +0000169 <li><b>GnuWin32 tools</b>
170 These are also necessary for running the tests.
171 (Note that the grep from MSYS or Cygwin doesn't work with the tests
172 because of embedded double-quotes in the search strings. The GNU
173 grep does work in this case.)
NAKAMURA Takumi823f3eb2011-03-27 02:04:21 +0000174 Get them from <a href="http://getgnuwin32.sourceforge.net/">
175 http://getgnuwin32.sourceforge.net/</a>.</li>
Eli Friedman05194922009-08-03 19:42:28 +0000176 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000177 </li>
Eli Friedman05194922009-08-03 19:42:28 +0000178
Adrian McCarthy02d0a632016-04-20 16:43:34 +0000179 <li>Check out LLVM:
Eli Friedman05194922009-08-03 19:42:28 +0000180 <ul>
181 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
182 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000183 </li>
Adrian McCarthy02d0a632016-04-20 16:43:34 +0000184 <li>Check out Clang:
Eli Friedman05194922009-08-03 19:42:28 +0000185 <ul>
186 <li><tt>cd llvm\tools</tt>
187 <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
188 </ul>
Adrian McCarthy02d0a632016-04-20 16:43:34 +0000189 <p><em>Note</em>: Some Clang tests are sensitive to the line endings. Ensure
190 that checking out the files does not convert LF line endings to CR+LF.
191 If you use git-svn, make sure your <tt>core.autocrlf</tt> setting is false.</p>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000192 </li>
NAKAMURA Takumia9f3c0f2012-11-27 23:34:08 +0000193 <li>Run CMake to generate the Visual Studio solution and project files:
Eli Friedman05194922009-08-03 19:42:28 +0000194 <ul>
Michael J. Spencer7ed7afc2010-12-16 22:01:14 +0000195 <li><tt>cd ..\..</tt> (back to where you started)</li>
196 <li><tt>mkdir build</tt> (for building without polluting the source dir)</li>
197 <li><tt>cd build</tt></li>
Yaron Keren37f99ed2015-05-07 15:12:30 +0000198 <li>If you are using Visual Studio 2013: <tt>cmake -G "Visual Studio 12" ..\llvm</tt></li>
Aaron Ballmanc674a052017-05-25 21:02:49 +0000199 <li>By default, the Visual Studio project files generated by CMake use the
200 32-bit toolset. If you are developing on a 64-bit version of Windows and
201 want to use the 64-bit toolset, pass the ``-Thost=x64`` flag when
202 generating the Visual Studio solution. This requires CMake 3.8.0 or later.</li>
Sylvestre Ledrub06fe482018-09-22 07:39:44 +0000203 <li>See the <a href="https://www.llvm.org/docs/CMake.html">LLVM CMake guide</a> for
NAKAMURA Takumia9f3c0f2012-11-27 23:34:08 +0000204 more information on other configuration options for CMake.</li>
Eli Friedman05194922009-08-03 19:42:28 +0000205 <li>The above, if successful, will have created an LLVM.sln file in the
NAKAMURA Takumib44cd822011-07-25 12:40:26 +0000206 <tt>build</tt> directory.
Eli Friedman05194922009-08-03 19:42:28 +0000207 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000208 </li>
209 <li>Build Clang:
Eli Friedman05194922009-08-03 19:42:28 +0000210 <ul>
211 <li>Open LLVM.sln in Visual Studio.</li>
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000212 <li>Build the "clang" project for just the compiler driver and front end, or
213 the "ALL_BUILD" project to build everything, including tools.</li>
Eli Friedman05194922009-08-03 19:42:28 +0000214 </ul>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000215 </li>
Eli Friedman05194922009-08-03 19:42:28 +0000216 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
217 running examples from above.)</li>
218 <li>See <a href="hacking.html#testingWindows">
219 Hacking on clang - Testing using Visual Studio on Windows</a> for information
220 on running regression tests on Windows.</li>
221</ol>
222
223<p>Note that once you have checked out both llvm and clang, to synchronize
224to the latest code base, use the <tt>svn update</tt> command in both the
225llvm and llvm\tools\clang directories, as they are separate repositories.</p>
226
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000227<h2 id="driver">Clang Compiler Driver (Drop-in Substitute for GCC)</h2>
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000228
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000229<p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
230designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are
231some examples of how to use the high-level driver:
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000232</p>
233
234<pre class="code">
235$ <b>cat t.c</b>
236#include &lt;stdio.h&gt;
237int main(int argc, char **argv) { printf("hello world\n"); }
238$ <b>clang t.c</b>
239$ <b>./a.out</b>
240hello world
241</pre>
242
Chris Lattnere5eb7262009-11-09 03:21:02 +0000243<p>The 'clang' driver is designed to work as closely to GCC as possible to
244 maximize portability. The only major difference between the two is that
245 Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see
Chris Lattnere6e64be2009-11-09 04:04:07 +0000246 weird link-time errors relating to inline functions, try passing -std=gnu89
Chris Lattnere5eb7262009-11-09 03:21:02 +0000247 to clang.</p>
248
Ted Kremenekb3e8f652009-03-27 16:32:57 +0000249<h2>Examples of using Clang</h2>
250
Eric Christopher2dc30a52008-02-08 06:45:49 +0000251<!-- Thanks to
252 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
253Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
254tag. -->
255
256<pre class="code">
257$ <b>cat ~/t.c</b>
258typedef float V __attribute__((vector_size(16)));
259V foo(V a, V b) { return a+b*a; }
260</pre>
261
262
Ted Kremenek77251e92008-06-17 13:48:36 +0000263<h3>Preprocessing:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000264
265<pre class="code">
266$ <b>clang ~/t.c -E</b>
267# 1 "/Users/sabre/t.c" 1
268
269typedef float V __attribute__((vector_size(16)));
270
271V foo(V a, V b) { return a+b*a; }
272</pre>
273
274
Ted Kremenek77251e92008-06-17 13:48:36 +0000275<h3>Type checking:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000276
277<pre class="code">
278$ <b>clang -fsyntax-only ~/t.c</b>
279</pre>
280
281
Ted Kremenek77251e92008-06-17 13:48:36 +0000282<h3>GCC options:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000283
284<pre class="code">
285$ <b>clang -fsyntax-only ~/t.c -pedantic</b>
Benjamin Kramereaa262b2012-01-15 15:26:07 +0000286/Users/sabre/t.c:2:17: <span style="color:magenta">warning:</span> extension used
287<span style="color:darkgreen">typedef float V __attribute__((vector_size(16)));</span>
288<span style="color:blue"> ^</span>
Eric Christopher2dc30a52008-02-08 06:45:49 +00002891 diagnostic generated.
290</pre>
291
292
Ted Kremenek77251e92008-06-17 13:48:36 +0000293<h3>Pretty printing from the AST:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000294
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000295<p>Note, the <tt>-cc1</tt> argument indicates the compiler front-end, and
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000296not the driver, should be run. The compiler front-end has several additional
297Clang specific features which are not exposed through the GCC compatible driver
298interface.</p>
299
Eric Christopher2dc30a52008-02-08 06:45:49 +0000300<pre class="code">
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000301$ <b>clang -cc1 ~/t.c -ast-print</b>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000302typedef float V __attribute__(( vector_size(16) ));
303V foo(V a, V b) {
304 return a + b * a;
305}
306</pre>
307
308
Ted Kremenek77251e92008-06-17 13:48:36 +0000309<h3>Code generation with LLVM:</h3>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000310
311<pre class="code">
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000312$ <b>clang ~/t.c -S -emit-llvm -o -</b>
Eric Christopher2dc30a52008-02-08 06:45:49 +0000313define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
314entry:
315 %mul = mul &lt;4 x float&gt; %b, %a
316 %add = add &lt;4 x float&gt; %mul, %a
317 ret &lt;4 x float&gt; %add
318}
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000319$ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
320...
Eric Christopher2dc30a52008-02-08 06:45:49 +0000321_foo:
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000322Leh_func_begin1:
323 mulps %xmm0, %xmm1
324 addps %xmm1, %xmm0
325 ret
326Leh_func_end1:
Eric Christopher2dc30a52008-02-08 06:45:49 +0000327</pre>
328
Eric Christopher2dc30a52008-02-08 06:45:49 +0000329</div>
330</body>
331</html>