]> granicus.if.org Git - clang/blob - www/get_started.html
Mark DR777 only as fixed in clang SVN.
[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>If you would like to check out and build Clang, the current procedure is as
34 follows:</p>
35
36 <ol>
37   <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>
45   </ul>
46
47   <li>Checkout LLVM:
48   <ul>
49     <li>Change directory to where you want the llvm directory placed.</li>
50     <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
51   </ul>
52   </li>
53   <li>Checkout Clang:
54   <ul>
55     <li><tt>cd llvm/tools</tt></li>
56     <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
57     <li><tt>cd ../..</tt></li>
58   </ul>
59   </li>
60   <li>Checkout extra Clang Tools: (optional)
61   <ul>
62     <li><tt>cd llvm/tools/clang/tools</tt></li>
63     <li><tt>svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk
64         extra</tt></li>
65     <li><tt>cd ../../../..</tt></li>
66   </ul>
67   </li>
68   <li>Checkout Compiler-RT:
69   <ul>
70     <li><tt>cd llvm/projects</tt></li>
71     <li><tt>svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk
72         compiler-rt</tt></li>
73     <li><tt>cd ../..</tt></li>
74   </ul>
75   </li>
76   <li>Build LLVM and Clang:
77   <ul>
78     <li><tt>mkdir build</tt> (for building without polluting the source dir)
79     </li>
80     <li><tt>cd build</tt></li>
81     <li><tt>../llvm/configure</tt></li>
82     <li><tt>make</tt></li>
83     <li>This builds both LLVM and Clang for debug mode.</li>
84     <li>Note: For subsequent Clang development, you can just do make at the
85     clang directory level.</li>
86     <li>It is also possible to use CMake instead of the makefiles. With CMake
87     it is possible to generate project files for several IDEs: Xcode, Eclipse
88     CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator), KDevelop3.</li>
89   </ul>
90   </li>
91
92   <li>If you intend to use Clang's C++ support, you may need to tell it how
93       to find your C++ standard library headers. In general, Clang will detect
94       the best version of libstdc++ headers available and use them - it will
95       look both for system installations of libstdc++ as well as installations
96       adjacent to Clang itself. If your configuration fits neither of these
97       scenarios, you can use the <tt>--with-gcc-toolchain</tt> configure option
98       to tell Clang where the gcc containing the desired libstdc++ is installed.
99   </li>
100   <li>Try it out (assuming you add llvm/Debug+Asserts/bin to your path):
101   <ul>
102     <li><tt>clang --help</tt></li>
103     <li><tt>clang file.c -fsyntax-only</tt> (check for correctness)</li>
104     <li><tt>clang file.c -S -emit-llvm -o -</tt> (print out unoptimized llvm code)</li>
105     <li><tt>clang file.c -S -emit-llvm -o - -O3</tt></li>
106     <li><tt>clang file.c -S -O3 -o -</tt> (output native machine code)</li>
107   </ul>
108   </li>
109 </ol>
110
111 <p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
112 encounter problems with building Clang, make sure you have the latest SVN
113 version of LLVM. LLVM contains support libraries for Clang that will be updated
114 as well as development on Clang progresses.</p>
115   
116 <h3>Simultaneously Building Clang and LLVM:</h3>
117
118 <p>Once you have checked out Clang into the llvm source tree it will build along
119 with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
120 once simply run <tt>make</tt> from the root LLVM directory.</p>
121     
122 <p><em>Note:</em> Observe that Clang is technically part of a separate
123 Subversion repository. As mentioned above, the latest Clang sources are tied to
124 the latest sources in the LLVM tree. You can update your toplevel LLVM project
125 and all (possibly unrelated) projects inside it with <tt><b>make
126 update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
127 to subversion. </p>
128
129 <h3 id="buildWindows">Using Visual Studio</h3>
130
131 <p>The following details setting up for and building Clang on Windows using
132 Visual Studio:</p>
133
134 <ol>
135   <li>Get the required tools:
136   <ul>
137     <li><b>Subversion</b>.  Source code control program.  Get it from:
138         <a href="http://subversion.tigris.org/getting.html">
139         http://subversion.tigris.org/getting.html</a></li>
140     <li><b>CMake</b>.  This is used for generating Visual Studio solution and
141         project files.  Get it from:
142         <a href="http://www.cmake.org/cmake/resources/software.html">
143         http://www.cmake.org/cmake/resources/software.html</a></li>
144     <li><b>Visual Studio 2012 or later</b></li>
145     <li><b>Python</b>.  This is needed only if you will be running the tests
146         (which is essential, if you will be developing for clang).
147         Get it from:
148         <a href="http://www.python.org/download/">
149         http://www.python.org/download/</a></li>
150     <li><b>GnuWin32 tools</b>
151         These are also necessary for running the tests.
152         (Note that the grep from MSYS or Cygwin doesn't work with the tests
153         because of embedded double-quotes in the search strings.  The GNU
154         grep does work in this case.)
155         Get them from <a href="http://getgnuwin32.sourceforge.net/">
156         http://getgnuwin32.sourceforge.net/</a>.</li>
157   </ul>
158   </li>
159
160   <li>Checkout LLVM:
161   <ul>
162     <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
163   </ul>
164   </li>
165   <li>Checkout Clang:
166   <ul>
167      <li><tt>cd llvm\tools</tt>
168      <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li>
169   </ul>
170   </li>
171   <li>Run CMake to generate the Visual Studio solution and project files:
172   <ul>
173     <li><tt>cd ..\..</tt>  (back to where you started)</li>
174     <li><tt>mkdir build</tt> (for building without polluting the source dir)</li>
175     <li><tt>cd build</tt></li>
176     <li>If you are using Visual Studio 2012:  <tt>cmake -G "Visual Studio 11" ..\llvm</tt></li>
177     <li>See the <a href="http://www.llvm.org/docs/CMake.html">LLVM CMake guide</a> for
178         more information on other configuration options for CMake.</li>
179     <li>The above, if successful, will have created an LLVM.sln file in the
180        <tt>build</tt> directory.
181   </ul>
182   </li>
183   <li>Build Clang:
184   <ul>
185     <li>Open LLVM.sln in Visual Studio.</li>
186     <li>Build the "clang" project for just the compiler driver and front end, or
187       the "ALL_BUILD" project to build everything, including tools.</li>
188   </ul>
189   </li>
190   <li>Try it out (assuming you added llvm/debug/bin to your path).  (See the
191     running examples from above.)</li>
192   <li>See <a href="hacking.html#testingWindows">
193      Hacking on clang - Testing using Visual Studio on Windows</a> for information
194      on running regression tests on Windows.</li>
195 </ol>
196
197 <p>Note that once you have checked out both llvm and clang, to synchronize
198 to the latest code base, use the <tt>svn update</tt> command in both the
199 llvm and llvm\tools\clang directories, as they are separate repositories.</p>
200
201 <h2 id="driver">Clang Compiler Driver (Drop-in Substitute for GCC)</h2>
202
203 <p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
204 designed to be a drop-in replacement for the <tt>gcc</tt> command.  Here are
205 some examples of how to use the high-level driver:
206 </p>
207
208 <pre class="code">
209 $ <b>cat t.c</b>
210 #include &lt;stdio.h&gt;
211 int main(int argc, char **argv) { printf("hello world\n"); }
212 $ <b>clang t.c</b>
213 $ <b>./a.out</b>
214 hello world
215 </pre>
216
217 <p>The 'clang' driver is designed to work as closely to GCC as possible to
218   maximize portability.  The only major difference between the two is that
219   Clang defaults to gnu99 mode while GCC defaults to gnu89 mode.  If you see
220   weird link-time errors relating to inline functions, try passing -std=gnu89
221   to clang.</p>
222
223 <h2>Examples of using Clang</h2>
224
225 <!-- Thanks to
226  http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
227 Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
228 tag. -->
229
230 <pre class="code">
231 $ <b>cat ~/t.c</b>
232 typedef float V __attribute__((vector_size(16)));
233 V foo(V a, V b) { return a+b*a; }
234 </pre>
235
236
237 <h3>Preprocessing:</h3>
238
239 <pre class="code">
240 $ <b>clang ~/t.c -E</b>
241 # 1 "/Users/sabre/t.c" 1
242
243 typedef float V __attribute__((vector_size(16)));
244
245 V foo(V a, V b) { return a+b*a; }
246 </pre>
247
248
249 <h3>Type checking:</h3>
250
251 <pre class="code">
252 $ <b>clang -fsyntax-only ~/t.c</b>
253 </pre>
254
255
256 <h3>GCC options:</h3>
257
258 <pre class="code">
259 $ <b>clang -fsyntax-only ~/t.c -pedantic</b>
260 /Users/sabre/t.c:2:17: <span style="color:magenta">warning:</span> extension used
261 <span style="color:darkgreen">typedef float V __attribute__((vector_size(16)));</span>
262 <span style="color:blue">                ^</span>
263 1 diagnostic generated.
264 </pre>
265
266
267 <h3>Pretty printing from the AST:</h3>
268
269 <p>Note, the <tt>-cc1</tt> argument indicates the compiler front-end, and
270 not the driver, should be run. The compiler front-end has several additional
271 Clang specific features which are not exposed through the GCC compatible driver
272 interface.</p>
273
274 <pre class="code">
275 $ <b>clang -cc1 ~/t.c -ast-print</b>
276 typedef float V __attribute__(( vector_size(16) ));
277 V foo(V a, V b) {
278    return a + b * a;
279 }
280 </pre>
281
282
283 <h3>Code generation with LLVM:</h3>
284
285 <pre class="code">
286 $ <b>clang ~/t.c -S -emit-llvm -o -</b>
287 define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
288 entry:
289          %mul = mul &lt;4 x float&gt; %b, %a
290          %add = add &lt;4 x float&gt; %mul, %a
291          ret &lt;4 x float&gt; %add
292 }
293 $ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
294 ...
295 _foo:
296 Leh_func_begin1:
297         mulps   %xmm0, %xmm1
298         addps   %xmm1, %xmm0
299         ret
300 Leh_func_end1:
301 </pre>
302
303 </div>
304 </body>
305 </html>