Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 3 | <html> |
| 4 | <head> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 5 | <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 6 | <title>Clang - Getting Started</title> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 7 | <link type="text/css" rel="stylesheet" href="menu.css"> |
| 8 | <link type="text/css" rel="stylesheet" href="content.css"> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | b3e8f65 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 18 | <p>This page gives you the shortest path to checking out Clang and demos a few |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 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 |
Chris Lattner | b8ca954 | 2009-11-09 03:18:18 +0000 | [diff] [blame] | 21 | involved</a> with the Clang community. If you run into problems, please file |
Douglas Gregor | a881010 | 2010-10-07 20:20:57 +0000 | [diff] [blame] | 22 | bugs in <a href="http://llvm.org/bugs/">LLVM Bugzilla</a>.</p> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 23 | |
Daniel Dunbar | 6b83538 | 2010-11-02 22:34:04 +0000 | [diff] [blame] | 24 | <h2 id="download">Release Clang Versions</h2> |
| 25 | |
Sylvestre Ledru | 7db5ab4 | 2014-04-27 15:17:15 +0000 | [diff] [blame] | 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> |
Daniel Dunbar | 6b83538 | 2010-11-02 22:34:04 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | b3e8f65 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 29 | <h2 id="build">Building Clang and Working with the Code</h2> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 30 | |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 31 | <h3 id="buildNix">On Unix-like Systems</h3> |
| 32 | |
Ted Kremenek | b3e8f65 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 33 | <p>If you would like to check out and build Clang, the current procedure is as |
| 34 | follows:</p> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 35 | |
| 36 | <ol> |
Anton Korobeynikov | 3ebc08b | 2009-08-06 13:00:08 +0000 | [diff] [blame] | 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> |
Alexey Samsonov | ec0bff9 | 2015-04-21 18:38:31 +0000 | [diff] [blame] | 45 | <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 Korobeynikov | 3ebc08b | 2009-08-06 13:00:08 +0000 | [diff] [blame] | 48 | </ul> |
| 49 | |
Adrian McCarthy | 02d0a63 | 2016-04-20 16:43:34 +0000 | [diff] [blame] | 50 | <li>Check out LLVM: |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 51 | <ul> |
Chris Lattner | f6bad86 | 2009-08-20 06:17:11 +0000 | [diff] [blame] | 52 | <li>Change directory to where you want the llvm directory placed.</li> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 53 | <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 54 | </ul> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 55 | </li> |
Adrian McCarthy | 02d0a63 | 2016-04-20 16:43:34 +0000 | [diff] [blame] | 56 | <li>Check out Clang: |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 57 | <ul> |
Chandler Carruth | 18d6745 | 2012-08-14 00:41:30 +0000 | [diff] [blame] | 58 | <li><tt>cd llvm/tools</tt></li> |
Chris Lattner | f6bad86 | 2009-08-20 06:17:11 +0000 | [diff] [blame] | 59 | <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li> |
Chandler Carruth | 18d6745 | 2012-08-14 00:41:30 +0000 | [diff] [blame] | 60 | <li><tt>cd ../..</tt></li> |
Chris Lattner | f6bad86 | 2009-08-20 06:17:11 +0000 | [diff] [blame] | 61 | </ul> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 62 | </li> |
Adrian McCarthy | 02d0a63 | 2016-04-20 16:43:34 +0000 | [diff] [blame] | 63 | <li>Check out extra Clang tools: (optional) |
Chandler Carruth | 72af8c3 | 2012-08-14 07:19:09 +0000 | [diff] [blame] | 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> |
Adrian McCarthy | 02d0a63 | 2016-04-20 16:43:34 +0000 | [diff] [blame] | 71 | <li>Check out Compiler-RT (optional): |
Daniel Dunbar | 2931cd9 | 2011-12-07 03:04:42 +0000 | [diff] [blame] | 72 | <ul> |
Chandler Carruth | 18d6745 | 2012-08-14 00:41:30 +0000 | [diff] [blame] | 73 | <li><tt>cd llvm/projects</tt></li> |
Daniel Dunbar | 2931cd9 | 2011-12-07 03:04:42 +0000 | [diff] [blame] | 74 | <li><tt>svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk |
| 75 | compiler-rt</tt></li> |
Chandler Carruth | 18d6745 | 2012-08-14 00:41:30 +0000 | [diff] [blame] | 76 | <li><tt>cd ../..</tt></li> |
Daniel Dunbar | 2931cd9 | 2011-12-07 03:04:42 +0000 | [diff] [blame] | 77 | </ul> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 78 | </li> |
Adrian McCarthy | 02d0a63 | 2016-04-20 16:43:34 +0000 | [diff] [blame] | 79 | <li>Check out libcxx: (only required to build and run Compiler-RT tests on OS X, optional otherwise) |
Yury Gribov | daace1b | 2015-09-15 15:05:47 +0000 | [diff] [blame] | 80 | <ul> |
| 81 | <li><tt>cd llvm/projects</tt></li> |
| 82 | <li><tt>svn co http://llvm.org/svn/llvm-project/libcxx/trunk |
| 83 | libcxx</tt></li> |
| 84 | <li><tt>cd ../..</tt></li> |
| 85 | </ul> |
| 86 | </li> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 87 | <li>Build LLVM and Clang: |
Chris Lattner | f6bad86 | 2009-08-20 06:17:11 +0000 | [diff] [blame] | 88 | <ul> |
Alexey Samsonov | ec0bff9 | 2015-04-21 18:38:31 +0000 | [diff] [blame] | 89 | <li><tt>mkdir build</tt> (in-tree build is not supported)</li> |
Rafael Espindola | 4f8857e | 2010-10-29 22:05:17 +0000 | [diff] [blame] | 90 | <li><tt>cd build</tt></li> |
Alexey Samsonov | ec0bff9 | 2015-04-21 18:38:31 +0000 | [diff] [blame] | 91 | <li><tt>cmake -G "Unix Makefiles" ../llvm</tt></li> |
Chris Lattner | f6bad86 | 2009-08-20 06:17:11 +0000 | [diff] [blame] | 92 | <li><tt>make</tt></li> |
| 93 | <li>This builds both LLVM and Clang for debug mode.</li> |
Alexey Samsonov | ec0bff9 | 2015-04-21 18:38:31 +0000 | [diff] [blame] | 94 | <li>Note: For subsequent Clang development, you can just run |
| 95 | <tt>make clang</tt>.</li> |
| 96 | <li>CMake allows you to generate project files for several IDEs: Xcode, |
| 97 | Eclipse CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator), |
| 98 | KDevelop3. For more details see |
| 99 | <a href="http://llvm.org/docs/CMake.html">Building LLVM with CMake</a> |
| 100 | page.</li> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 101 | </ul> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 102 | </li> |
Chris Lattner | 6eeb221 | 2010-05-06 21:57:57 +0000 | [diff] [blame] | 103 | |
Richard Smith | 9deb9d9 | 2013-03-12 19:56:09 +0000 | [diff] [blame] | 104 | <li>If you intend to use Clang's C++ support, you may need to tell it how |
Chandler Carruth | cc31231 | 2012-05-16 08:18:58 +0000 | [diff] [blame] | 105 | to find your C++ standard library headers. In general, Clang will detect |
| 106 | the best version of libstdc++ headers available and use them - it will |
| 107 | look both for system installations of libstdc++ as well as installations |
| 108 | adjacent to Clang itself. If your configuration fits neither of these |
Justin Bogner | 78a9db7 | 2015-12-10 20:52:59 +0000 | [diff] [blame] | 109 | scenarios, you can use the <tt>-DGCC_INSTALL_PREFIX</tt> cmake option |
Chandler Carruth | cc31231 | 2012-05-16 08:18:58 +0000 | [diff] [blame] | 110 | to tell Clang where the gcc containing the desired libstdc++ is installed. |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 111 | </li> |
Reid Kleckner | 88cbe5a | 2016-04-11 17:02:34 +0000 | [diff] [blame] | 112 | <li>Try it out (assuming you add llvm/build/bin to your path): |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 113 | <ul> |
Daniel Dunbar | 520d1e6 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 114 | <li><tt>clang --help</tt></li> |
| 115 | <li><tt>clang file.c -fsyntax-only</tt> (check for correctness)</li> |
| 116 | <li><tt>clang file.c -S -emit-llvm -o -</tt> (print out unoptimized llvm code)</li> |
| 117 | <li><tt>clang file.c -S -emit-llvm -o - -O3</tt></li> |
| 118 | <li><tt>clang file.c -S -O3 -o -</tt> (output native machine code)</li> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 119 | </ul> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 120 | </li> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 121 | </ol> |
| 122 | |
Reid Kleckner | 88cbe5a | 2016-04-11 17:02:34 +0000 | [diff] [blame] | 123 | <p>If you encounter problems while building Clang, make sure that your LLVM |
| 124 | checkout is at the same revision as your Clang checkout. LLVM's interfaces |
| 125 | change over time, and mismatched revisions are not expected to work |
| 126 | together.</p> |
Adrian McCarthy | 02d0a63 | 2016-04-20 16:43:34 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | b3e8f65 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 128 | <h3>Simultaneously Building Clang and LLVM:</h3> |
Eric Christopher | 6aa87cb | 2008-02-08 07:10:48 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | b3e8f65 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 130 | <p>Once you have checked out Clang into the llvm source tree it will build along |
| 131 | with the rest of <tt>llvm</tt>. To build all of LLVM and Clang together all at |
| 132 | once simply run <tt>make</tt> from the root LLVM directory.</p> |
Adrian McCarthy | 02d0a63 | 2016-04-20 16:43:34 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | b3e8f65 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 134 | <p><em>Note:</em> Observe that Clang is technically part of a separate |
| 135 | Subversion repository. As mentioned above, the latest Clang sources are tied to |
| 136 | the latest sources in the LLVM tree. You can update your toplevel LLVM project |
| 137 | and all (possibly unrelated) projects inside it with <tt><b>make |
| 138 | update</b></tt>. This will run <tt>svn update</tt> on all subdirectories related |
| 139 | to subversion. </p> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 140 | |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 141 | <h3 id="buildWindows">Using Visual Studio</h3> |
| 142 | |
| 143 | <p>The following details setting up for and building Clang on Windows using |
| 144 | Visual Studio:</p> |
| 145 | |
| 146 | <ol> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 147 | <li>Get the required tools: |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 148 | <ul> |
| 149 | <li><b>Subversion</b>. Source code control program. Get it from: |
Yaron Keren | 9f88702 | 2015-05-07 15:15:16 +0000 | [diff] [blame] | 150 | <a href="http://subversion.apache.org/packages.html"> |
| 151 | http://subversion.apache.org/packages.html</a></li> |
NAKAMURA Takumi | a9f3c0f | 2012-11-27 23:34:08 +0000 | [diff] [blame] | 152 | <li><b>CMake</b>. This is used for generating Visual Studio solution and |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 153 | project files. Get it from: |
| 154 | <a href="http://www.cmake.org/cmake/resources/software.html"> |
| 155 | http://www.cmake.org/cmake/resources/software.html</a></li> |
Yaron Keren | 37f99ed | 2015-05-07 15:12:30 +0000 | [diff] [blame] | 156 | <li><b>Visual Studio 2013 or later</b></li> |
Chris Lattner | f6bad86 | 2009-08-20 06:17:11 +0000 | [diff] [blame] | 157 | <li><b>Python</b>. This is needed only if you will be running the tests |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 158 | (which is essential, if you will be developing for clang). |
| 159 | Get it from: |
NAKAMURA Takumi | 823f3eb | 2011-03-27 02:04:21 +0000 | [diff] [blame] | 160 | <a href="http://www.python.org/download/"> |
| 161 | http://www.python.org/download/</a></li> |
Chris Lattner | f6bad86 | 2009-08-20 06:17:11 +0000 | [diff] [blame] | 162 | <li><b>GnuWin32 tools</b> |
| 163 | These are also necessary for running the tests. |
| 164 | (Note that the grep from MSYS or Cygwin doesn't work with the tests |
| 165 | because of embedded double-quotes in the search strings. The GNU |
| 166 | grep does work in this case.) |
NAKAMURA Takumi | 823f3eb | 2011-03-27 02:04:21 +0000 | [diff] [blame] | 167 | Get them from <a href="http://getgnuwin32.sourceforge.net/"> |
| 168 | http://getgnuwin32.sourceforge.net/</a>.</li> |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 169 | </ul> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 170 | </li> |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 171 | |
Adrian McCarthy | 02d0a63 | 2016-04-20 16:43:34 +0000 | [diff] [blame] | 172 | <li>Check out LLVM: |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 173 | <ul> |
| 174 | <li><tt>svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</tt></li> |
| 175 | </ul> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 176 | </li> |
Adrian McCarthy | 02d0a63 | 2016-04-20 16:43:34 +0000 | [diff] [blame] | 177 | <li>Check out Clang: |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 178 | <ul> |
| 179 | <li><tt>cd llvm\tools</tt> |
| 180 | <li><tt>svn co http://llvm.org/svn/llvm-project/cfe/trunk clang</tt></li> |
| 181 | </ul> |
Adrian McCarthy | 02d0a63 | 2016-04-20 16:43:34 +0000 | [diff] [blame] | 182 | <p><em>Note</em>: Some Clang tests are sensitive to the line endings. Ensure |
| 183 | that checking out the files does not convert LF line endings to CR+LF. |
| 184 | If you use git-svn, make sure your <tt>core.autocrlf</tt> setting is false.</p> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 185 | </li> |
NAKAMURA Takumi | a9f3c0f | 2012-11-27 23:34:08 +0000 | [diff] [blame] | 186 | <li>Run CMake to generate the Visual Studio solution and project files: |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 187 | <ul> |
Michael J. Spencer | 7ed7afc | 2010-12-16 22:01:14 +0000 | [diff] [blame] | 188 | <li><tt>cd ..\..</tt> (back to where you started)</li> |
| 189 | <li><tt>mkdir build</tt> (for building without polluting the source dir)</li> |
| 190 | <li><tt>cd build</tt></li> |
Yaron Keren | 37f99ed | 2015-05-07 15:12:30 +0000 | [diff] [blame] | 191 | <li>If you are using Visual Studio 2013: <tt>cmake -G "Visual Studio 12" ..\llvm</tt></li> |
John Thompson | 9bfba16 | 2011-04-06 18:22:12 +0000 | [diff] [blame] | 192 | <li>See the <a href="http://www.llvm.org/docs/CMake.html">LLVM CMake guide</a> for |
NAKAMURA Takumi | a9f3c0f | 2012-11-27 23:34:08 +0000 | [diff] [blame] | 193 | more information on other configuration options for CMake.</li> |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 194 | <li>The above, if successful, will have created an LLVM.sln file in the |
NAKAMURA Takumi | b44cd82 | 2011-07-25 12:40:26 +0000 | [diff] [blame] | 195 | <tt>build</tt> directory. |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 196 | </ul> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 197 | </li> |
| 198 | <li>Build Clang: |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 199 | <ul> |
| 200 | <li>Open LLVM.sln in Visual Studio.</li> |
Daniel Dunbar | 520d1e6 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 201 | <li>Build the "clang" project for just the compiler driver and front end, or |
| 202 | the "ALL_BUILD" project to build everything, including tools.</li> |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 203 | </ul> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 204 | </li> |
Eli Friedman | 0519492 | 2009-08-03 19:42:28 +0000 | [diff] [blame] | 205 | <li>Try it out (assuming you added llvm/debug/bin to your path). (See the |
| 206 | running examples from above.)</li> |
| 207 | <li>See <a href="hacking.html#testingWindows"> |
| 208 | Hacking on clang - Testing using Visual Studio on Windows</a> for information |
| 209 | on running regression tests on Windows.</li> |
| 210 | </ol> |
| 211 | |
| 212 | <p>Note that once you have checked out both llvm and clang, to synchronize |
| 213 | to the latest code base, use the <tt>svn update</tt> command in both the |
| 214 | llvm and llvm\tools\clang directories, as they are separate repositories.</p> |
| 215 | |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 216 | <h2 id="driver">Clang Compiler Driver (Drop-in Substitute for GCC)</h2> |
Ted Kremenek | b3e8f65 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 217 | |
Daniel Dunbar | 520d1e6 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 218 | <p>The <tt>clang</tt> tool is the compiler driver and front-end, which is |
| 219 | designed to be a drop-in replacement for the <tt>gcc</tt> command. Here are |
| 220 | some examples of how to use the high-level driver: |
Ted Kremenek | b3e8f65 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 221 | </p> |
| 222 | |
| 223 | <pre class="code"> |
| 224 | $ <b>cat t.c</b> |
| 225 | #include <stdio.h> |
| 226 | int main(int argc, char **argv) { printf("hello world\n"); } |
| 227 | $ <b>clang t.c</b> |
| 228 | $ <b>./a.out</b> |
| 229 | hello world |
| 230 | </pre> |
| 231 | |
Chris Lattner | e5eb726 | 2009-11-09 03:21:02 +0000 | [diff] [blame] | 232 | <p>The 'clang' driver is designed to work as closely to GCC as possible to |
| 233 | maximize portability. The only major difference between the two is that |
| 234 | Clang defaults to gnu99 mode while GCC defaults to gnu89 mode. If you see |
Chris Lattner | e6e64be | 2009-11-09 04:04:07 +0000 | [diff] [blame] | 235 | weird link-time errors relating to inline functions, try passing -std=gnu89 |
Chris Lattner | e5eb726 | 2009-11-09 03:21:02 +0000 | [diff] [blame] | 236 | to clang.</p> |
| 237 | |
Ted Kremenek | b3e8f65 | 2009-03-27 16:32:57 +0000 | [diff] [blame] | 238 | <h2>Examples of using Clang</h2> |
| 239 | |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 240 | <!-- Thanks to |
| 241 | http://shiflett.org/blog/2006/oct/formatting-and-highlighting-php-code-listings |
| 242 | Site suggested using pre in CSS, but doesn't work in IE, so went for the <pre> |
| 243 | tag. --> |
| 244 | |
| 245 | <pre class="code"> |
| 246 | $ <b>cat ~/t.c</b> |
| 247 | typedef float V __attribute__((vector_size(16))); |
| 248 | V foo(V a, V b) { return a+b*a; } |
| 249 | </pre> |
| 250 | |
| 251 | |
Ted Kremenek | 77251e9 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 252 | <h3>Preprocessing:</h3> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 253 | |
| 254 | <pre class="code"> |
| 255 | $ <b>clang ~/t.c -E</b> |
| 256 | # 1 "/Users/sabre/t.c" 1 |
| 257 | |
| 258 | typedef float V __attribute__((vector_size(16))); |
| 259 | |
| 260 | V foo(V a, V b) { return a+b*a; } |
| 261 | </pre> |
| 262 | |
| 263 | |
Ted Kremenek | 77251e9 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 264 | <h3>Type checking:</h3> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 265 | |
| 266 | <pre class="code"> |
| 267 | $ <b>clang -fsyntax-only ~/t.c</b> |
| 268 | </pre> |
| 269 | |
| 270 | |
Ted Kremenek | 77251e9 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 271 | <h3>GCC options:</h3> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 272 | |
| 273 | <pre class="code"> |
| 274 | $ <b>clang -fsyntax-only ~/t.c -pedantic</b> |
Benjamin Kramer | eaa262b | 2012-01-15 15:26:07 +0000 | [diff] [blame] | 275 | /Users/sabre/t.c:2:17: <span style="color:magenta">warning:</span> extension used |
| 276 | <span style="color:darkgreen">typedef float V __attribute__((vector_size(16)));</span> |
| 277 | <span style="color:blue"> ^</span> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 278 | 1 diagnostic generated. |
| 279 | </pre> |
| 280 | |
| 281 | |
Ted Kremenek | 77251e9 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 282 | <h3>Pretty printing from the AST:</h3> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 283 | |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 284 | <p>Note, the <tt>-cc1</tt> argument indicates the compiler front-end, and |
Daniel Dunbar | 520d1e6 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 285 | not the driver, should be run. The compiler front-end has several additional |
| 286 | Clang specific features which are not exposed through the GCC compatible driver |
| 287 | interface.</p> |
| 288 | |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 289 | <pre class="code"> |
Daniel Dunbar | 520d1e6 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 290 | $ <b>clang -cc1 ~/t.c -ast-print</b> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 291 | typedef float V __attribute__(( vector_size(16) )); |
| 292 | V foo(V a, V b) { |
| 293 | return a + b * a; |
| 294 | } |
| 295 | </pre> |
| 296 | |
| 297 | |
Ted Kremenek | 77251e9 | 2008-06-17 13:48:36 +0000 | [diff] [blame] | 298 | <h3>Code generation with LLVM:</h3> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 299 | |
| 300 | <pre class="code"> |
Daniel Dunbar | 520d1e6 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 301 | $ <b>clang ~/t.c -S -emit-llvm -o -</b> |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 302 | define <4 x float> @foo(<4 x float> %a, <4 x float> %b) { |
| 303 | entry: |
| 304 | %mul = mul <4 x float> %b, %a |
| 305 | %add = add <4 x float> %mul, %a |
| 306 | ret <4 x float> %add |
| 307 | } |
Daniel Dunbar | 520d1e6 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 308 | $ <b>clang -fomit-frame-pointer -O3 -S -o - t.c</b> <i># On x86_64</i> |
| 309 | ... |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 310 | _foo: |
Daniel Dunbar | 520d1e6 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 311 | Leh_func_begin1: |
| 312 | mulps %xmm0, %xmm1 |
| 313 | addps %xmm1, %xmm0 |
| 314 | ret |
| 315 | Leh_func_end1: |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 316 | </pre> |
| 317 | |
Eric Christopher | 2dc30a5 | 2008-02-08 06:45:49 +0000 | [diff] [blame] | 318 | </div> |
| 319 | </body> |
| 320 | </html> |