I will not hold on to temporary StringRefs.
[clang.git] / www / get_started.html
blob54f7442b88bb55445f7363ac764c92f306dd6f06
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>
12 <!--#include virtual="menu.html.incl"-->
14 <div id="content">
16 <h1>Getting Started: Building and Running Clang</h1>
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>
24 <h2 id="download">Release Clang Versions</h2>
26 <p>Clang has been released as part of regular LLVM releases since LLVM 2.6. You
27 can download the release versions
28 from <a href="http://llvm.org/releases/">http://llvm.org/releases/</a>.</p>
30 <h2 id="build">Building Clang and Working with the Code</h2>
32 <h3 id="buildNix">On Unix-like Systems</h3>
34 <p>If you would like to check out and build Clang, the current procedure is as
35 follows:</p>
37 <ol>
38 <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>
48 <li>Checkout LLVM:</li>
49 <ul>
50 <li>Change directory to where you want the llvm directory placed.</li>
51 <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li>
52 </ul>
53 <li>Checkout Clang:</li>
54 <ul>
55 <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>
60 <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>
65 <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>
69 </ul>
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>
75 <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
77 system libstdc++ headers, please follow these instructions:</li>
78 <ul>
79 <li>'<tt>gcc -v -x c++ /dev/null -fsyntax-only</tt>' to get the
80 path.</li>
81 <li>Look for the comment "FIXME: temporary hack:
82 hard-coded paths" in <tt>clang/lib/Frontend/InitHeaderSearch.cpp</tt> and
83 change the lines below to include that path.</li>
84 </ul>
85 <li>Try it out (assuming you add llvm/Debug+Asserts/bin to your path):</li>
86 <ul>
87 <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>
92 </ul>
93 </ol>
95 <p>Note that the C front-end uses LLVM, but does not depend on llvm-gcc. If you
96 encounter problems with building Clang, make sure you have the latest SVN
97 version of LLVM. LLVM contains support libraries for Clang that will be updated
98 as well as development on Clang progresses.</p>
100 <h3>Simultaneously Building Clang and LLVM:</h3>
102 <p>Once you have checked out Clang into the llvm source tree it will build along
103 with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at
104 once simply run <tt>make</tt> from the root LLVM directory.</p>
106 <p><em>Note:</em> Observe that Clang is technically part of a separate
107 Subversion repository. As mentioned above, the latest Clang sources are tied to
108 the latest sources in the LLVM tree. You can update your toplevel LLVM project
109 and all (possibly unrelated) projects inside it with <tt><b>make
110 update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related
111 to subversion. </p>
113 <h3 id="buildWindows">Using Visual Studio</h3>
115 <p>The following details setting up for and building Clang on Windows using
116 Visual Studio:</p>
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>
128 <li><b>Visual Studio 2005, 2008, or 2010</b></li>
129 <li><b>Python</b>. This is needed only if you will be running the tests
130 (which is essential, if you will be developing for clang).
131 Get it from:
132 <a href="http://www.python.org/download">
133 http://www.python.org/download</a></li>
134 <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.)
139 Get them from <a href="http://getgnuwin32.sourceforge.net">
140 http://getgnuwin32.sourceforge.net</a>.</li>
141 </ul>
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>
154 <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>
160 <li>The above, if successful, will have created an LLVM.sln file in the
161 llvm directory.
162 </ul>
163 <li>Build Clang:</li>
164 <ul>
165 <li>Open LLVM.sln in Visual Studio.</li>
166 <li>Build the "clang" project for just the compiler driver and front end, or
167 the "ALL_BUILD" project to build everything, including tools.</li>
168 </ul>
169 <li>Try it out (assuming you added llvm/debug/bin to your path). (See the
170 running examples from above.)</li>
171 <li>See <a href="hacking.html#testingWindows">
172 Hacking on clang - Testing using Visual Studio on Windows</a> for information
173 on running regression tests on Windows.</li>
174 </ol>
176 <p>Note that once you have checked out both llvm and clang, to synchronize
177 to the latest code base, use the <tt>svn update</tt> command in both the
178 llvm and llvm\tools\clang directories, as they are separate repositories.</p>
180 <a name="driver"><h2>Clang Compiler Driver (Drop-in Substitute for GCC)</h2></a>
182 <p>The <tt>clang</tt> tool is the compiler driver and front-end, which is
183 designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are
184 some examples of how to use the high-level driver:
185 </p>
187 <pre class="code">
188 $ <b>cat t.c</b>
189 #include &lt;stdio.h&gt;
190 int main(int argc, char **argv) { printf("hello world\n"); }
191 $ <b>clang t.c</b>
192 $ <b>./a.out</b>
193 hello world
194 </pre>
196 <p>The 'clang' driver is designed to work as closely to GCC as possible to
197 maximize portability. The only major difference between the two is that
198 Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see
199 weird link-time errors relating to inline functions, try passing -std=gnu89
200 to clang.</p>
202 <h2>Examples of using Clang</h2>
204 <!-- Thanks to
205 http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings
206 Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre>
207 tag. -->
209 <pre class="code">
210 $ <b>cat ~/t.c</b>
211 typedef float V __attribute__((vector_size(16)));
212 V foo(V a, V b) { return a+b*a; }
213 </pre>
216 <h3>Preprocessing:</h3>
218 <pre class="code">
219 $ <b>clang ~/t.c -E</b>
220 # 1 "/Users/sabre/t.c" 1
222 typedef float V __attribute__((vector_size(16)));
224 V foo(V a, V b) { return a+b*a; }
225 </pre>
228 <h3>Type checking:</h3>
230 <pre class="code">
231 $ <b>clang -fsyntax-only ~/t.c</b>
232 </pre>
235 <h3>GCC options:</h3>
237 <pre class="code">
238 $ <b>clang -fsyntax-only ~/t.c -pedantic</b>
239 /Users/sabre/t.c:2:17: warning: extension used
240 typedef float V __attribute__((vector_size(16)));
242 1 diagnostic generated.
243 </pre>
246 <h3>Pretty printing from the AST:</h3>
248 <p>Note, the <tt>-cc1</tt> argument indicates the the compiler front-end, and
249 not the driver, should be run. The compiler front-end has several additional
250 Clang specific features which are not exposed through the GCC compatible driver
251 interface.</p>
253 <pre class="code">
254 $ <b>clang -cc1 ~/t.c -ast-print</b>
255 typedef float V __attribute__(( vector_size(16) ));
256 V foo(V a, V b) {
257 return a + b * a;
259 </pre>
262 <h3>Code generation with LLVM:</h3>
264 <pre class="code">
265 $ <b>clang ~/t.c -S -emit-llvm -o -</b>
266 define &lt;4 x float&gt; @foo(&lt;4 x float&gt; %a, &lt;4 x float&gt; %b) {
267 entry:
268 %mul = mul &lt;4 x float&gt; %b, %a
269 %add = add &lt;4 x float&gt; %mul, %a
270 ret &lt;4 x float&gt; %add
272 $ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i>
274 _foo:
275 Leh_func_begin1:
276 mulps %xmm0, %xmm1
277 addps %xmm1, %xmm0
279 Leh_func_end1:
280 </pre>
282 </div>
283 </body>
284 </html>