]> granicus.if.org Git - clang/blob - www/get_started.html
This call-site should have been updated as part of D34304.
[clang] / www / get_started.html
1 <!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
18 <p>This page gives you the shortest path to checking out Clang and demos a few
19 options.  This should get you up and running with the minimum of muss and fuss.
20 If you like what you see, please consider <a href="get_involved.html">getting
21 involved</a> with the Clang community.  If you run into problems, please file
22 bugs in <a href="http://llvm.org/bugs/">LLVM Bugzilla</a>.</p>
23
24 <h2 id="download">Release Clang Versions</h2>
25
26 <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>
28
29 <h2 id="build">Building Clang and Working with the Code</h2>
30
31 <h3 id="buildNix">On Unix-like Systems</h3>
32
33 <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="http://llvm.org/docs/GettingStarted.html#for-developers-to-work-with-a-git-monorepo">For developers to work with a git monorepo</a>"</p>
34
35 <p>If you would like to check out and build Clang, the current procedure is as
36 follows:</p>
37
38 <ol>
39   <li>Get the required tools.
40   <ul>
41     <li>See
42       <a href="http://llvm.org/docs/GettingStarted.html#requirements">
43       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>
47     <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>
50   </ul>
51
52   <li>Check out LLVM:
53   <ul>
54     <li>Change directory to where you want the llvm directory placed.</li>
55     <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
56   </ul>
57   </li>
58   <li>Check out Clang:
59   <ul>
60     <li><tt>cd llvm/tools</tt></li>
61     <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
62     <li><tt>cd ../..</tt></li>
63   </ul>
64   </li>
65   <li>Check out extra Clang tools: (optional)
66   <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>
73   <li>Check out Compiler-RT (optional):
74   <ul>
75     <li><tt>cd llvm/projects</tt></li>
76     <li><tt>svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk
77         compiler-rt</tt></li>
78     <li><tt>cd ../..</tt></li>
79   </ul>
80   </li>
81   <li>Check out libcxx: (only required to build and run Compiler-RT tests on OS X, optional otherwise)
82   <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>
89   <li>Build LLVM and Clang:
90   <ul>
91     <li><tt>mkdir build</tt> (in-tree build is not supported)</li>
92     <li><tt>cd build</tt></li>
93     <li><tt>cmake -G "Unix Makefiles" ../llvm</tt></li>
94     <li><tt>make</tt></li>
95     <li>This builds both LLVM and Clang for debug mode.</li>
96     <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
101         <a href="http://llvm.org/docs/CMake.html">Building LLVM with CMake</a>
102         page.</li>
103   </ul>
104   </li>
105
106   <li>If you intend to use Clang's C++ support, you may need to tell it how
107       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
111       scenarios, you can use the <tt>-DGCC_INSTALL_PREFIX</tt> cmake option
112       to tell Clang where the gcc containing the desired libstdc++ is installed.
113   </li>
114   <li>Try it out (assuming you add llvm/build/bin to your path):
115   <ul>
116     <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>
121   </ul>
122   </li>
123   <li>Run the testsuite:
124   <ul>
125     <li><tt>make check-clang</tt></li>
126   </ul>
127   </li>
128 </ol>
129
130 <p>If you encounter problems while building Clang, make sure that your LLVM
131 checkout is at the same revision as your Clang checkout. LLVM's interfaces
132 change over time, and mismatched revisions are not expected to work
133 together.</p>
134
135 <h3>Simultaneously Building Clang and LLVM:</h3>
136
137 <p>Once you have checked out Clang into the llvm source tree it will build along
138 with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
139 once simply run <tt>make</tt> from the root LLVM directory.</p>
140
141 <p><em>Note:</em> Observe that Clang is technically part of a separate
142 Subversion repository. As mentioned above, the latest Clang sources are tied to
143 the latest sources in the LLVM tree. You can update your toplevel LLVM project
144 and all (possibly unrelated) projects inside it with <tt><b>make
145 update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
146 to subversion. </p>
147
148 <h3 id="buildWindows">Using Visual Studio</h3>
149
150 <p>The following details setting up for and building Clang on Windows using
151 Visual Studio:</p>
152
153 <ol>
154   <li>Get the required tools:
155   <ul>
156     <li><b>Subversion</b>.  Source code control program.  Get it from:
157         <a href="http://subversion.apache.org/packages.html">
158         http://subversion.apache.org/packages.html</a></li>
159     <li><b>CMake</b>.  This is used for generating Visual Studio solution and
160         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>
163     <li><b>Visual Studio 2013 or later</b></li>
164     <li><b>Python</b>.  This is needed only if you will be running the tests
165         (which is essential, if you will be developing for clang).
166         Get it from:
167         <a href="http://www.python.org/download/">
168         http://www.python.org/download/</a></li>
169     <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.)
174         Get them from <a href="http://getgnuwin32.sourceforge.net/">
175         http://getgnuwin32.sourceforge.net/</a>.</li>
176   </ul>
177   </li>
178
179   <li>Check out LLVM:
180   <ul>
181     <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
182   </ul>
183   </li>
184   <li>Check out Clang:
185   <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>
189   <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>
192   </li>
193   <li>Run CMake to generate the Visual Studio solution and project files:
194   <ul>
195     <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>
198     <li>If you are using Visual Studio 2013:  <tt>cmake -G "Visual Studio 12" ..\llvm</tt></li>
199     <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>
203     <li>See the <a href="http://www.llvm.org/docs/CMake.html">LLVM CMake guide</a> for
204         more information on other configuration options for CMake.</li>
205     <li>The above, if successful, will have created an LLVM.sln file in the
206        <tt>build</tt> directory.
207   </ul>
208   </li>
209   <li>Build Clang:
210   <ul>
211     <li>Open LLVM.sln in Visual Studio.</li>
212     <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>
214   </ul>
215   </li>
216   <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
224 to the latest code base, use the <tt>svn update</tt> command in both the
225 llvm and llvm\tools\clang directories, as they are separate repositories.</p>
226
227 <h2 id="driver">Clang Compiler Driver (Drop-in Substitute for GCC)</h2>
228
229 <p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
230 designed to be a drop-in replacement for the <tt>gcc</tt> command.  Here are
231 some examples of how to use the high-level driver:
232 </p>
233
234 <pre class="code">
235 $ <b>cat t.c</b>
236 #include &lt;stdio.h&gt;
237 int main(int argc, char **argv) { printf("hello world\n"); }
238 $ <b>clang t.c</b>
239 $ <b>./a.out</b>
240 hello world
241 </pre>
242
243 <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
246   weird link-time errors relating to inline functions, try passing -std=gnu89
247   to clang.</p>
248
249 <h2>Examples of using Clang</h2>
250
251 <!-- Thanks to
252  http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
253 Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
254 tag. -->
255
256 <pre class="code">
257 $ <b>cat ~/t.c</b>
258 typedef float V __attribute__((vector_size(16)));
259 V foo(V a, V b) { return a+b*a; }
260 </pre>
261
262
263 <h3>Preprocessing:</h3>
264
265 <pre class="code">
266 $ <b>clang ~/t.c -E</b>
267 # 1 "/Users/sabre/t.c" 1
268
269 typedef float V __attribute__((vector_size(16)));
270
271 V foo(V a, V b) { return a+b*a; }
272 </pre>
273
274
275 <h3>Type checking:</h3>
276
277 <pre class="code">
278 $ <b>clang -fsyntax-only ~/t.c</b>
279 </pre>
280
281
282 <h3>GCC options:</h3>
283
284 <pre class="code">
285 $ <b>clang -fsyntax-only ~/t.c -pedantic</b>
286 /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>
289 1 diagnostic generated.
290 </pre>
291
292
293 <h3>Pretty printing from the AST:</h3>
294
295 <p>Note, the <tt>-cc1</tt> argument indicates the compiler front-end, and
296 not the driver, should be run. The compiler front-end has several additional
297 Clang specific features which are not exposed through the GCC compatible driver
298 interface.</p>
299
300 <pre class="code">
301 $ <b>clang -cc1 ~/t.c -ast-print</b>
302 typedef float V __attribute__(( vector_size(16) ));
303 V foo(V a, V b) {
304    return a + b * a;
305 }
306 </pre>
307
308
309 <h3>Code generation with LLVM:</h3>
310
311 <pre class="code">
312 $ <b>clang ~/t.c -S -emit-llvm -o -</b>
313 define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
314 entry:
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 }
319 $ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
320 ...
321 _foo:
322 Leh_func_begin1:
323         mulps   %xmm0, %xmm1
324         addps   %xmm1, %xmm0
325         ret
326 Leh_func_end1:
327 </pre>
328
329 </div>
330 </body>
331 </html>