[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / native_client_sdk / doc_generated / devguide / devcycle / building.html
blobf3afcf494b4d000734124c43d2d3ceb76a911358
1 {{+bindTo:partials.standard_nacl_article}}
3 <section id="building">
4 <span id="devcycle-building"></span><h1 id="building"><span id="devcycle-building"></span>Building</h1>
5 <div class="contents local" id="table-of-contents" style="display: none">
6 <p class="topic-title first">Table Of Contents</p>
7 <ul class="small-gap">
8 <li><p class="first"><a class="reference internal" href="#introduction" id="id4">Introduction</a></p>
9 <ul class="small-gap">
10 <li><a class="reference internal" href="#target-architectures" id="id5">Target architectures</a></li>
11 <li><a class="reference internal" href="#c-libraries" id="id6">C libraries</a></li>
12 <li><a class="reference internal" href="#c-standard-libraries" id="id7">C++ standard libraries</a></li>
13 <li><a class="reference internal" href="#sdk-toolchains" id="id8">SDK toolchains</a></li>
14 <li><a class="reference internal" href="#sdk-toolchains-versus-your-hosted-toolchain" id="id9">SDK toolchains versus your hosted toolchain</a></li>
15 </ul>
16 </li>
17 <li><a class="reference internal" href="#the-pnacl-toolchain" id="id10">The PNaCl toolchain</a></li>
18 <li><p class="first"><a class="reference internal" href="#using-the-pnacl-tools-to-compile-link-debug-and-deploy" id="id11">Using the PNaCl tools to compile, link, debug, and deploy</a></p>
19 <ul class="small-gap">
20 <li><a class="reference internal" href="#compile" id="id12">Compile</a></li>
21 <li><a class="reference internal" href="#create-a-static-library" id="id13">Create a static library</a></li>
22 <li><a class="reference internal" href="#link-the-application" id="id14">Link the application</a></li>
23 <li><a class="reference internal" href="#finalizing-the-pexe-for-deployment" id="id15">Finalizing the <strong>pexe</strong> for deployment</a></li>
24 <li><a class="reference internal" href="#compressing-the-pexe-for-deployment" id="id16">Compressing the <strong>pexe</strong> for deployment</a></li>
25 </ul>
26 </li>
27 <li><p class="first"><a class="reference internal" href="#the-gnu-based-toolchains" id="id17">The GNU-based toolchains</a></p>
28 <ul class="small-gap">
29 <li><a class="reference internal" href="#compiling" id="id18">Compiling</a></li>
30 <li><a class="reference internal" href="#creating-libraries-and-linking" id="id19">Creating libraries and Linking</a></li>
31 <li><a class="reference internal" href="#finalizing-a-nexe-for-deployment" id="id20">Finalizing a <strong>nexe</strong> for deployment</a></li>
32 </ul>
33 </li>
34 <li><a class="reference internal" href="#using-make" id="id21">Using make</a></li>
35 <li><a class="reference internal" href="#libraries-and-header-files-provided-with-the-sdk" id="id22">Libraries and header files provided with the SDK</a></li>
36 <li><p class="first"><a class="reference internal" href="#troubleshooting" id="id23">Troubleshooting</a></p>
37 <ul class="small-gap">
38 <li><a class="reference internal" href="#undefined-reference-error" id="id24">&#8220;Undefined reference&#8221; error</a></li>
39 <li><a class="reference internal" href="#can-t-find-libraries-containing-necessary-symbols" id="id25">Can&#8217;t find libraries containing necessary symbols</a></li>
40 <li><a class="reference internal" href="#pnacl-abi-verification-errors" id="id26">PNaCl ABI Verification errors</a></li>
41 </ul>
42 </li>
43 </ul>
45 </div><section id="introduction">
46 <h2 id="introduction">Introduction</h2>
47 <p>This document describes how to build Native Client modules. It is intended for
48 developers who have experience writing, compiling, and linking C and C++ code.
49 If you haven&#8217;t read the Native Client <a class="reference internal" href="/native-client/overview.html"><em>Technical Overview</em></a> and <a class="reference internal" href="/native-client/devguide/tutorial/index.html"><em>Tutorial</em></a>, we recommend starting
50 with those.</p>
51 <section id="target-architectures">
52 <span id="id1"></span><h3 id="target-architectures"><span id="id1"></span>Target architectures</h3>
53 <p>Portable Native Client (PNaCl) modules are written in C or C++ and compiled
54 into an executable file ending in a <strong>.pexe</strong> extension using the PNaCl
55 toolchain in the Native Client SDK. Chrome can load <strong>pexe</strong> files
56 embedded in web pages and execute them as part of a web application.</p>
57 <p>As explained in the Technical Overview, PNaCl modules are
58 operating-system-independent <strong>and</strong> processor-independent. The same <strong>pexe</strong>
59 will run on Windows, Mac OS X, Linux, and ChromeOS and it will run on x86-32,
60 x86-64, ARM and MIPS processors.</p>
61 <p>Native Client also supports architecture-specific <strong>nexe</strong> files.
62 These <strong>nexe</strong> files are <strong>also</strong> operating-system-independent,
63 but they are <strong>not</strong> processor-independent. To support a wide variety of
64 devices you must compile separate versions of your Native Client module
65 for different processors on end-user machines. A
66 <a class="reference internal" href="/native-client/overview.html#application-files"><em>manifest file</em></a> will then specify which version
67 of the module to load based on the end-user&#8217;s architecture. The SDK
68 includes a script&#8212;<code>create_nmf.py</code> (in the <code>tools/</code> directory)&#8212;to
69 generate manifest files. For examples of how to compile modules
70 for multiple target architectures and how to generate manifest files, see the
71 Makefiles included with the SDK examples.</p>
72 <p>This section will mostly cover PNaCl, but also describes how to build
73 <strong>nexe</strong> applications.</p>
74 </section><section id="c-libraries">
75 <h3 id="c-libraries">C libraries</h3>
76 <p>The PNaCl SDK has a single choice of C library: <a class="reference external" href="http://sourceware.org/newlib/">newlib</a>.</p>
77 <p>The Native Client SDK also has a GCC-based toolchain for building
78 <strong>nexes</strong>. The GCC-based toolchain has support for two C libraries:
79 <a class="reference external" href="http://sourceware.org/newlib/">newlib</a> and <a class="reference external" href="http://www.gnu.org/software/libc/">glibc</a>. See <a class="reference internal" href="/native-client/devguide/devcycle/dynamic-loading.html"><em>Dynamic Linking &amp; Loading with glibc</em></a> for information about these libraries, including factors to
80 help you decide which to use.</p>
81 </section><section id="c-standard-libraries">
82 <span id="building-cpp-libraries"></span><h3 id="c-standard-libraries"><span id="building-cpp-libraries"></span>C++ standard libraries</h3>
83 <p>The PNaCl SDK can use either LLVM&#8217;s <a class="reference external" href="http://libcxx.llvm.org/">libc++</a>
84 (the current default) or GCC&#8217;s <a class="reference external" href="http://gcc.gnu.org/libstdc++">libstdc++</a> (deprecated). The
85 <code>-stdlib=[libc++|libstdc++]</code> command line argument can be used to
86 choose which standard library to use.</p>
87 <p>The GCC-based Native Client SDK only has support for GCC&#8217;s <a class="reference external" href="http://gcc.gnu.org/libstdc++">libstdc++</a>.</p>
88 <p>C++11 library support is only complete in libc++ but other non-library language
89 features should work regardless of which standard library is used. The
90 <code>-std=gnu++11</code> command line argument can be used to indicate which C++
91 language standard to use (<code>-std=c++11</code> often doesn&#8217;t work well because newlib
92 relies on some GNU extensions).</p>
93 </section><section id="sdk-toolchains">
94 <h3 id="sdk-toolchains">SDK toolchains</h3>
95 <p>The Native Client SDK includes multiple toolchains. It has one PNaCl toolchain
96 and it has multiple GCC-based toolchains that are differentiated by target
97 architectures and C libraries. The single PNaCl toolchain is located
98 in a directory named <code>toolchain/&lt;OS_platform&gt;_pnacl</code>, and the GCC-based
99 toolchains are located in directories named
100 <code>toolchain/&lt;OS_platform&gt;_&lt;architecture&gt;_&lt;library&gt;</code>, where:</p>
101 <ul class="small-gap">
102 <li><dl class="first docutils">
103 <dt><em>&lt;platform&gt;</em> is the platform of your development machine (<em>win</em>, <em>mac</em>, or</dt>
104 <dd><em>linux</em>)</dd>
105 </dl>
106 </li>
107 <li><em>&lt;architecture&gt;</em> is your target architecture (<em>x86</em> or <em>arm</em>)</li>
108 <li><em>&lt;library&gt;</em> is the C library you are compiling with (<em>newlib</em> or <em>glibc</em>)</li>
109 </ul>
110 <p>The compilers, linkers, and other tools are located in the <code>bin/</code>
111 subdirectory in each toolchain. For example, the tools in the Windows SDK
112 for PNaCl has a C++ compiler in <code>toolchain/win_pnacl/bin/pnacl-clang++</code>.
113 As another example, the GCC-based C++ compiler that targets x86 and uses the
114 newlib library, is located at <code>toolchain/win_x86_newlib/bin/x86_64-nacl-g++</code>.</p>
115 <aside class="note">
116 The SDK toolchains descend from the <code>toolchain/</code> directory. The SDK also
117 has a <code>tools/</code> directory; this directory contains utilities that are not
118 properly part of the toolchains but that you may find helpful in building and
119 testing your application (e.g., the <code>create_nmf.py</code> script, which you can
120 use to create a manifest file).
121 </aside>
122 </section><section id="sdk-toolchains-versus-your-hosted-toolchain">
123 <h3 id="sdk-toolchains-versus-your-hosted-toolchain">SDK toolchains versus your hosted toolchain</h3>
124 <p>To build NaCl modules, you must use one of the Native Client toolchains
125 included in the SDK. The SDK toolchains use a variety of techniques to
126 ensure that your NaCl modules comply with the security constraints of
127 the Native Client sandbox.</p>
128 <p>During development, you have another choice: You can build modules using a
129 <em>standard</em> toolchain, such as the hosted toolchain on your development
130 machine. This can be Visual Studio&#8217;s standard compiler, XCode, LLVM, or
131 GNU-based compilers on your development machine. These standard toolchains
132 will not produce executables that comply with the Native Client sandbox
133 security constraints. They are also not portable across operating systems
134 and not portable across different processors. However, using a standard
135 toolchain allows you to develop modules in your favorite IDE and use
136 your favorite debugging and profiling tools. The drawback is that modules
137 compiled in this manner can only run as Pepper (PPAPI) plugins in Chrome.
138 To publish and distribute Native Client modules as part of a web
139 application, you must eventually use a toolchain in the Native
140 Client SDK.</p>
141 <aside class="note">
142 In the future, additional tools will be available to compile Native Client
143 modules written in other programming languages, such as C#. But this
144 document covers only compiling C and C++ code, using the toolchains
145 provided in the SDK.
146 </aside>
147 </section></section><section id="the-pnacl-toolchain">
148 <h2 id="the-pnacl-toolchain">The PNaCl toolchain</h2>
149 <p>The PNaCl toolchain contains modified versions of the tools in the
150 LLVM toolchain, as well as linkers and other tools from binutils.
151 To determine which version of LLVM or binutils the tools are based upon,
152 run the tool with the <code>--version</code> command line flag. These tools
153 are used to compile and link applications into <strong>.pexe</strong> files. The toolchain
154 also contains a tool to translate a <strong>pexe</strong> file into a
155 architecture-specific <strong>.nexe</strong> (e.g., for debugging purposes).</p>
156 <p>Each tool&#8217;s name is preceded by the prefix &#8220;pnacl-&#8221;. Some of the useful
157 tools include:</p>
158 <dl class="docutils">
159 <dt>pnacl-abicheck</dt>
160 <dd>Check that the <strong>pexe</strong> follows the PNaCl ABI rules.</dd>
161 <dt>pnacl-ar</dt>
162 <dd>Creates archives (i.e., static libraries)</dd>
163 <dt>pnacl-clang</dt>
164 <dd>C compiler and compiler driver</dd>
165 <dt>pnacl-clang++</dt>
166 <dd>C++ compiler and compiler driver</dd>
167 <dt>pnacl-compress</dt>
168 <dd>Size compresses a finalized <strong>pexe</strong> file for deployment.</dd>
169 <dt>pnacl-dis</dt>
170 <dd>Disassembler for both <strong>pexe</strong> files and <strong>nexe</strong> files</dd>
171 <dt>pnacl-finalize</dt>
172 <dd>Finalizes <strong>pexe</strong> files for deployment</dd>
173 <dt>pnacl-ld</dt>
174 <dd>Bitcode linker</dd>
175 <dt>pnacl-nm</dt>
176 <dd>Lists symbols in bitcode files, native code, and libraries</dd>
177 <dt>pnacl-ranlib</dt>
178 <dd>Generates a symbol table for archives (i.e., static libraries)</dd>
179 <dt>pnacl-translate</dt>
180 <dd>Translates a <strong>pexe</strong> to a native architecture, outside of the browser</dd>
181 </dl>
182 <p>For the full list of tools, see the
183 <code>&lt;NACL_SDK_ROOT&gt;/toolchain/&lt;platform&gt;_pnacl/bin</code> directory.</p>
184 </section><section id="using-the-pnacl-tools-to-compile-link-debug-and-deploy">
185 <h2 id="using-the-pnacl-tools-to-compile-link-debug-and-deploy">Using the PNaCl tools to compile, link, debug, and deploy</h2>
186 <p>To build an application with the PNaCl SDK toolchain, you must compile
187 your code, link it, test and debug it, and then deploy it. This section goes
188 over some examples of how to use the tools.</p>
189 <section id="compile">
190 <h3 id="compile">Compile</h3>
191 <p>To compile a simple application consisting of <code>file1.cc</code> and <code>file2.cc</code> into
192 <code>hello_world.pexe</code> with a single command, use the <code>pnacl-clang++</code> tool</p>
193 <pre>
194 &lt;NACL_SDK_ROOT&gt;/toolchain/win_pnacl/bin/pnacl-clang++ file1.cc file2.cc ^
195 -I&lt;NACL_SDK_ROOT&gt;/include -L&lt;NACL_SDK_ROOT&gt;/lib/pnacl/Release ^
196 -o hello_world.pexe -g -O2 -lppapi_cpp -lppapi
197 </pre>
198 <p>(The carat <code>^</code> allows the command to span multiple lines on Windows;
199 to do the same on Mac and Linux use a backslash instead. Or you can
200 simply type the command and all its arguments on one
201 line. <code>&lt;NACL_SDK_ROOT&gt;</code> represents the path to the top-level
202 directory of the bundle you are using, e.g.,
203 <code>&lt;location-where-you-installed-the-SDK&gt;/pepper_31</code>.)</p>
204 <p>However, the typical application consists of many files. In that case,
205 each file can be compiled separately so that only files that are
206 affected by a change need to be recompiled. To compile an individual
207 file from your application, you must use either the <code>pnacl-clang</code> C
208 compiler, or the <code>pnacl-clang++</code> C++ compiler. The compiler produces
209 separate bitcode files. For example:</p>
210 <pre>
211 &lt;NACL_SDK_ROOT&gt;/toolchain/win_pnacl/bin/pnacl-clang++ hello_world.cc ^
212 -I&lt;NACL_SDK_ROOT&gt;/include -c -o hello_world.o -g -O0
213 </pre>
214 <p>For a description of each command line flag, run <code>pnacl-clang --help</code>.
215 For convenience, here is a description of some of the flags used in
216 the example.</p>
217 <dl class="docutils" id="compile-flags">
218 <dt><code>-c</code></dt>
219 <dd>indicates that <code>pnacl-clang++</code> should only compile an individual file,
220 rather than continue the build process and link together the
221 full application.</dd>
222 <dt><code>-o &lt;output_file&gt;</code></dt>
223 <dd>indicates the <strong>output</strong> filename.</dd>
224 <dt><code>-g</code></dt>
225 <dd>tells the compiler to include debug information in the result.
226 This debug information can be used during development, and then <strong>stripped</strong>
227 before actually deploying the application to keep the application&#8217;s
228 download size small.</dd>
229 <dt><code>-On</code></dt>
230 <dd><p class="first">sets the optimization level to n. Use <code>-O0</code> when debugging, and <code>-O2</code> or
231 <code>-O3</code> for deployment.</p>
232 <p class="last">The main difference between <code>-O2</code> and <code>-O3</code> is whether the compiler
233 performs optimizations that involve a space-speed tradeoff. It could be the
234 case that <code>-O3</code> optimizations are not desirable due to increased <strong>pexe</strong>
235 download size; you should make your own performance measurements to determine
236 which level of optimization is right for you. When looking at code size, note
237 that what you generally care about is not the size of the <strong>pexe</strong> produced by
238 <code>pnacl-clang</code>, but the size of the compressed <strong>pexe</strong> that you upload to
239 the server or to the Chrome Web Store. Optimizations that increase the size of
240 an uncompressed <strong>pexe</strong> may not increase the size of the compressed <strong>pexe</strong>
241 very much. You should also verify how optimization level affects on-device
242 translation time, this can be tested locally with <code>pnacl-translate</code>.</p>
243 </dd>
244 <dt><code>-I&lt;directory&gt;</code></dt>
245 <dd>adds a directory to the search path for <strong>include</strong> files. The SDK has
246 Pepper (PPAPI) headers located at <code>&lt;NACL_SDK_ROOT&gt;/include</code>, so add
247 that directory when compiling to be able to include the headers.</dd>
248 <dt><code>-mllvm -inline-threshold=n</code></dt>
249 <dd>change how much inlining is performed by LLVM (the default is 225, a smaller
250 value will result in less inlining being performed). The right number to
251 choose is application-specific, you&#8217;ll therefore want to experiment with the
252 value that you pass in: you&#8217;ll be trading off potential performance with
253 <strong>pexe</strong> size and on-device translation speed.</dd>
254 </dl>
255 </section><section id="create-a-static-library">
256 <h3 id="create-a-static-library">Create a static library</h3>
257 <p>The <code>pnacl-ar</code> and <code>pnacl-ranlib</code> tools allow you to create a
258 <strong>static</strong> library from a set of bitcode files, which can later be linked
259 into the full application.</p>
260 <pre>
261 &lt;NACL_SDK_ROOT&gt;/toolchain/win_pnacl/bin/pnacl-ar cr libfoo.a ^
262 foo1.o foo2.o foo3.o
264 &lt;NACL_SDK_ROOT&gt;/toolchain/win_pnacl/bin/pnacl-ranlib libfoo.a
265 </pre>
266 </section><section id="link-the-application">
267 <h3 id="link-the-application">Link the application</h3>
268 <p>The <code>pnacl-clang++</code> tool is used to compile applications, but it can
269 also be used link together compiled bitcode and libraries into a
270 full application.</p>
271 <pre>
272 &lt;NACL_SDK_ROOT&gt;/toolchain/win_pnacl/bin/pnacl-clang++ -o hello_world.pexe ^
273 hello_world.o -L&lt;NACL_SDK_ROOT&gt;/lib/pnacl/Debug ^
274 -lfoo -lppapi_cpp -lppapi
275 </pre>
276 <p>This links the hello world bitcode with the <code>foo</code> library in the example
277 as well as the <em>Debug</em> version of the Pepper libraries which are located
278 in <code>&lt;NACL_SDK_ROOT&gt;/lib/pnacl/Debug</code>. If you wish to link against the
279 <em>Release</em> version of the Pepper libraries, change the
280 <code>-L&lt;NACL_SDK_ROOT&gt;/lib/pnacl/Debug</code> to
281 <code>-L&lt;NACL_SDK_ROOT&gt;/lib/pnacl/Release</code>.</p>
282 <p>In a release build you&#8217;ll want to pass <code>-O2</code> to the compiler <em>as well as to
283 the linker</em> to enable link-time optimizations. This reduces the size and
284 increases the performance of the final <strong>pexe</strong>, and leads to faster downloads
285 and on-device translation.</p>
286 <pre>
287 &lt;NACL_SDK_ROOT&gt;/toolchain/win_pnacl/bin/pnacl-clang++ -o hello_world.pexe ^
288 hello_world.o -L&lt;NACL_SDK_ROOT&gt;/lib/pnacl/Release ^
289 -lfoo -lppapi_cpp -lppapi -O2
290 </pre>
291 <p>By default the link step will turn all C++ exceptions into calls to <code>abort()</code>
292 to reduce the size of the final <strong>pexe</strong> as well as making it translate and run
293 faster. If you want to use C++ exceptions you should use the
294 <code>--pnacl-exceptions=sjlj</code> linker flag as explained in the <a class="reference internal" href="/native-client/reference/pnacl-c-cpp-language-support.html#exception-handling"><em>exception
295 handling</em></a> section of the C++ language support reference.</p>
296 </section><section id="finalizing-the-pexe-for-deployment">
297 <h3 id="finalizing-the-pexe-for-deployment">Finalizing the <strong>pexe</strong> for deployment</h3>
298 <p>Typically you would run the application to test it and debug it if needed before
299 deploying. See the <a class="reference internal" href="/native-client/devguide/devcycle/running.html"><em>running</em></a> documentation for how to run a PNaCl
300 application, and see the <a class="reference internal" href="/native-client/devguide/devcycle/debugging.html"><em>debugging</em></a> documentation for
301 debugging techniques and workflow. After testing a PNaCl application, you must
302 <strong>finalize</strong> it. The <code>pnacl-finalize</code> tool handles this.</p>
303 <pre>
304 &lt;NACL_SDK_ROOT&gt;/toolchain/win_pnacl/bin/pnacl-finalize ^
305 hello_world.pexe -o hello_world.final.pexe
306 </pre>
307 <p>Prior to finalization, the application <strong>pexe</strong> is stored in a binary
308 format that is subject to change. After finalization, the application
309 <strong>pexe</strong> is <strong>rewritten</strong> into a different binary format that is <strong>stable</strong>
310 and will be supported by future versions of PNaCl. The finalization step
311 also helps minimize the size of your application for distribution by
312 stripping out debug information and other metadata.</p>
313 <p>Once the application is finalized, be sure to adjust the manifest file to
314 refer to the final version of the application before deployment.
315 The <code>create_nmf.py</code> tool helps generate an <code>.nmf</code> file, but <code>.nmf</code>
316 files can also be written by hand.</p>
317 </section><section id="compressing-the-pexe-for-deployment">
318 <span id="pnacl-compress"></span><h3 id="compressing-the-pexe-for-deployment"><span id="pnacl-compress"></span>Compressing the <strong>pexe</strong> for deployment</h3>
319 <p>Size compression is an optional step for deployment, and reduces the size of the
320 <strong>pexe</strong> file that must be transmitted over the wire, resulting in faster
321 download speed. The tool <code>pnacl-compress</code> applies compression strategies that
322 are already built into the <strong>stable</strong> binary format of a <strong>pexe</strong>
323 application. As such, compressed <strong>pexe</strong> files do not need any extra time to be
324 decompressed on the client&#8217;s side. All costs are upfront when you call
325 <code>pnacl-compress</code>.</p>
326 <p>Currently, this tool will compress <strong>pexe</strong> files by about 25%. However,
327 it is somewhat slow (can take from seconds to minutes on large
328 appications). Hence, this step is optional.</p>
329 <pre>
330 &lt;NACL_SDK_ROOT&gt;/toolchain/win_pnacl/bin/pnacl-compress ^
331 hello_world.final.pexe
332 </pre>
333 <p><code>pnacl-compress</code> must be called after a <strong>pexe</strong> file has been finalized for
334 deployment (via <code>pnacl-finalize</code>). Alternatively, you can apply this step as
335 part of the finalizing step by adding the <code>--compress</code> flag to the
336 <code>pnacl-finalize</code> command line.</p>
337 <p>This compression step doesn&#8217;t replace the gzip compression performed web servers
338 configured for HTTP compression: both compressions are complementary. You&#8217;ll
339 want to configure your web server to gzip <strong>pexe</strong> files: the gzipped version of
340 a compressed <strong>pexe</strong> file is smaller than the corresponding uncompressed
341 <strong>pexe</strong> file by 7.5% to 10%.</p>
342 </section></section><section id="the-gnu-based-toolchains">
343 <h2 id="the-gnu-based-toolchains">The GNU-based toolchains</h2>
344 <p>Besides the PNaCl toolchain, the Native Client SDK also includes modified
345 versions of the tools in the standard GNU toolchain, including the GCC
346 compilers and the linkers and other tools from binutils. These tools only
347 support building <strong>nexe</strong> files. Run the tool with the <code>--version</code>
348 command line flag to determine the current version of the tools.</p>
349 <p>Each tool in the toolchain is prefixed with the name of the target
350 architecture. In the toolchain for the ARM target architecture, each
351 tool&#8217;s name is preceded by the prefix &#8220;arm-nacl-&#8221;. In the toolchains for
352 the x86 target architecture, there are actually two versions of each
353 tool&#8212;one to build Native Client modules for the x86-32
354 target architecture, and one to build modules for the x86-64 target
355 architecture. &#8220;i686-nacl-&#8221; is the prefix for tools used to build
356 32-bit <strong>.nexes</strong>, and &#8220;x86_64-nacl-&#8221; is the prefix for tools used to
357 build 64-bit <strong>.nexes</strong>.</p>
358 <p>These prefixes conform to gcc naming standards and make it easy to use tools
359 like autoconf. As an example, you can use <code>i686-nacl-gcc</code> to compile 32-bit
360 <strong>.nexes</strong>, and <code>x86_64-nacl-gcc</code> to compile 64-bit <strong>.nexes</strong>. Note that you
361 can typically override a tool&#8217;s default target architecture with command line
362 flags, e.g., you can specify <code>x86_64-nacl-gcc -m32</code> to compile a 32-bit
363 <strong>.nexe</strong>.</p>
364 <p>The GNU-based SDK toolchains include the following tools:</p>
365 <ul class="small-gap">
366 <li>&lt;prefix&gt;addr2line</li>
367 <li>&lt;prefix&gt;ar</li>
368 <li>&lt;prefix&gt;as</li>
369 <li>&lt;prefix&gt;c++</li>
370 <li>&lt;prefix&gt;c++filt</li>
371 <li>&lt;prefix&gt;cpp</li>
372 <li>&lt;prefix&gt;g++</li>
373 <li>&lt;prefix&gt;gcc</li>
374 <li>&lt;prefix&gt;gcc-4.4.3</li>
375 <li>&lt;prefix&gt;gccbug</li>
376 <li>&lt;prefix&gt;gcov</li>
377 <li>&lt;prefix&gt;gprof</li>
378 <li>&lt;prefix&gt;ld</li>
379 <li>&lt;prefix&gt;nm</li>
380 <li>&lt;prefix&gt;objcopy</li>
381 <li>&lt;prefix&gt;objdump</li>
382 <li>&lt;prefix&gt;ranlib</li>
383 <li>&lt;prefix&gt;readelf</li>
384 <li>&lt;prefix&gt;size</li>
385 <li>&lt;prefix&gt;strings</li>
386 <li>&lt;prefix&gt;strip</li>
387 </ul>
388 <section id="compiling">
389 <h3 id="compiling">Compiling</h3>
390 <p>Compiling files with the GNU-based toolchain is similar to compiling
391 files with the PNaCl-based toolchain, except that the output is
392 architecture specific.</p>
393 <p>For example, assuming you&#8217;re developing on a Windows machine, targeting the x86
394 architecture, and using the newlib library, you can compile a 32-bit <strong>.nexe</strong>
395 for the hello_world example with the following command:</p>
396 <pre>
397 &lt;NACL_SDK_ROOT&gt;/toolchain/win_x86_newlib/bin/i686-nacl-gcc hello_world.c ^
398 -I&lt;NACL_SDK_ROOT&gt;/include -L&lt;NACL_SDK_ROOT&gt;/lib/newlib/Release ^
399 -o hello_world_x86_32.nexe -m32 -g -O2 -lppapi
400 </pre>
401 <p>To compile a 64-bit <strong>.nexe</strong>, you can run the same command but use -m64 instead
402 of -m32. Alternatively, you could also use the version of the compiler that
403 targets the x86-64 architecture, i.e., <code>x86_64-nacl-gcc</code>.</p>
404 <p>You should name executable modules with a <strong>.nexe</strong> filename extension,
405 regardless of what platform you&#8217;re using.</p>
406 </section><section id="creating-libraries-and-linking">
407 <h3 id="creating-libraries-and-linking">Creating libraries and Linking</h3>
408 <p>Creating libraries and linking with the GNU-based toolchain is similar
409 to doing the same with the PNaCl toolchain. The relevant tools
410 for creating <strong>static</strong> libraries are <code>&lt;prefix&gt;ar</code> and <code>&lt;prefix&gt;ranlib</code>.
411 Linking can be done with <code>&lt;prefix&gt;g++</code>. See the
412 <a class="reference internal" href="/native-client/devguide/devcycle/dynamic-loading.html"><em>Dynamic Linking &amp; Loading with glibc</em></a>
413 section on how to create <strong>shared</strong> libraries.</p>
414 </section><section id="finalizing-a-nexe-for-deployment">
415 <h3 id="finalizing-a-nexe-for-deployment">Finalizing a <strong>nexe</strong> for deployment</h3>
416 <p>Unlike the PNaCl toolchain, no separate finalization step is required
417 for <strong>nexe</strong> files. The <strong>nexe</strong> files are always in a <strong>stable</strong> format.
418 However, the <strong>nexe</strong> file may contain debug information and symbol information
419 which may make the <strong>nexe</strong> file larger than needed for distribution.
420 To minimize the size of the distributed file, you can run the
421 <code>&lt;prefix&gt;strip</code> tool to strip out debug information.</p>
422 </section></section><section id="using-make">
423 <h2 id="using-make">Using make</h2>
424 <p>This document doesn&#8217;t cover how to use <code>make</code>, but if you want to use
425 <code>make</code> to build your Native Client module, you can base your Makefile on the
426 ones in the SDK examples.</p>
427 <p>The Makefiles for the SDK examples build most of the examples in multiple
428 configurations (using PNaCl vs NaCl, using different C libraries,
429 targeting different architectures, and using different levels of optimization).
430 To select a specific toolchain, set the <strong>environment variable</strong>
431 <code>TOOLCHAIN</code> to either <code>pnacl</code>, <code>newlib</code>, <code>glibc</code>, or <code>host</code>.
432 To select a specific level of optimization set the <strong>environment
433 variable</strong> <code>CONFIG</code> to either <code>Debug</code>, or <code>Release</code>. Running
434 <code>make</code> in each example&#8217;s directory does <strong>one</strong> of the following,
435 depending on the setting of the environment variables.</p>
436 <ul class="small-gap">
437 <li><p class="first">If <code>TOOLCHAIN=pnacl</code> creates a subdirectory called <code>pnacl</code>;</p>
438 <ul class="small-gap">
439 <li>builds a <strong>.pexe</strong> (architecture-independent Native Client executable) using
440 the newlib library</li>
441 <li>generates a Native Client manifest (.nmf) file for the pnacl version of the
442 example</li>
443 </ul>
444 </li>
445 <li><p class="first">If <code>TOOLCHAIN=newlib</code> creates a subdirectory called <code>newlib</code>;</p>
446 <ul class="small-gap">
447 <li>builds <strong>.nexes</strong> for the x86-32, x86-64, and ARM architectures using the
448 newlib library</li>
449 <li>generates a Native Client manifest (.nmf) file for the newlib version of
450 the example</li>
451 </ul>
452 </li>
453 <li><p class="first">If <code>TOOLCHAIN=glibc</code> creates a subdirectory called <code>glibc</code>;</p>
454 <ul class="small-gap">
455 <li>builds <strong>.nexes</strong> for the x86-32 and x86-64 architectures using the glibc
456 library</li>
457 <li>generates a Native Client manifest (.nmf) file for the glibc version of the
458 example</li>
459 </ul>
460 </li>
461 <li><p class="first">If <code>TOOLCHAIN=host</code> creates a subdirectory called <code>windows</code>, <code>linux</code>,
462 or <code>mac</code> (depending on your development machine);</p>
463 <ul class="small-gap">
464 <li>builds a Pepper plugin (.dll for Windows, .so for Linux/Mac) using the
465 hosted toolchain on your development machine</li>
466 <li>generates a Native Client manifest (.nmf) file for the host Pepper plugin
467 version of the example</li>
468 </ul>
469 </li>
470 </ul>
471 <aside class="note">
472 The glibc library is not yet available for the ARM and PNaCl toolchains.
473 </aside>
474 <p>Here is how to build the examples with PNaCl in Release mode on Windows.
475 The resulting files for <code>examples/api/audio</code> will be in
476 <code>examples/api/audio/pnacl/Release</code>, and the directory layout is similar for
477 other examples.</p>
478 <pre>
479 set TOOLCHAIN=pnacl
480 set CONFIG=Release
481 make
482 </pre>
483 <p>Your Makefile can be simpler since you will not likely want to build so many
484 different configurations of your module. The example Makefiles define
485 numerous variables near the top (e.g., <code>CFLAGS</code>) that make it easy
486 to customize the commands that are executed for your project and the options
487 for each command.</p>
488 <p>For details on how to use make, see the <a class="reference external" href="http://www.gnu.org/software/make/manual/make.html">GNU &#8216;make&#8217; Manual</a>.</p>
489 </section><section id="libraries-and-header-files-provided-with-the-sdk">
490 <h2 id="libraries-and-header-files-provided-with-the-sdk">Libraries and header files provided with the SDK</h2>
491 <p>The Native Client SDK includes modified versions of standard toolchain-support
492 libraries, such as libpthread and libc, plus the relevant header files.
493 The standard libraries are located in the following directories:</p>
494 <ul class="small-gap">
495 <li>PNaCl toolchain: <code>toolchain/&lt;platform&gt;_pnacl/usr/lib</code></li>
496 <li>x86 toolchains: <code>toolchain/&lt;platform&gt;_x86_&lt;library&gt;/x86_64-nacl/lib32</code> and
497 <code>/lib64</code> (for the 32-bit and 64-bit target architectures, respectively)</li>
498 <li>ARM toolchain: <code>toolchain/&lt;platform&gt;_arm_&lt;library&gt;/arm-nacl/lib</code></li>
499 </ul>
500 <p>For example, on Windows, the libraries for the x86-64 architecture in the
501 newlib toolchain are in <code>toolchain/win_x86_newlib/x86_64-nacl/lib64</code>.</p>
502 <p>The header files are in:</p>
503 <ul class="small-gap">
504 <li>PNaCl toolchain: <code>toolchain/&lt;platform&gt;_pnacl/usr/include</code></li>
505 <li>x86 toolchains: <code>toolchain/&lt;platform&gt;_x86_&lt;library&gt;/x86_64-nacl/include</code></li>
506 <li>ARM toolchain: <code>toolchain/&lt;platform&gt;_arm_&lt;library&gt;/arm-nacl/include</code></li>
507 </ul>
508 <p>Many other libraries have been ported for use with Native Client; for more
509 information, see the <a class="reference external" href="http://code.google.com/p/naclports/">naclports</a>
510 project. If you port an open-source library for your own use, we recommend
511 adding it to naclports.</p>
512 <p>Besides the standard libraries, the SDK includes Pepper libraries.
513 The PNaCl Pepper libraries are located in the the
514 <code>&lt;NACL_SDK_ROOT&gt;/lib/pnacl/&lt;Release or Debug&gt;</code> directory.
515 The GNU-based toolchain has Pepper libraries in
516 <code>&lt;NACL_SDK_ROOT&gt;/lib/newlib_&lt;arch&gt;/&lt;Release or Debug&gt;</code>
517 and <code>&lt;NACL_SDK_ROOT&gt;/lib/glibc_&lt;arch&gt;/&lt;Release or Debug&gt;</code>.
518 The libraries provided by the SDK allow the application to use Pepper,
519 as well as convenience libraries to simplify porting an application that
520 uses POSIX functions. Here are descriptions of the Pepper libraries provided
521 in the SDK.</p>
522 <dl class="docutils" id="devcycle-building-nacl-io">
523 <dt>libppapi.a</dt>
524 <dd>Implements the Pepper (PPAPI) C interface. Needed for all applications that
525 use Pepper (even C++ applications).</dd>
526 <dt>libppapi_cpp.a</dt>
527 <dd>Implements the Pepper (PPAPI) C++ interface. Needed by C++ applications that
528 use Pepper.</dd>
529 <dt>libppapi_gles2.a</dt>
530 <dd>Implements the Pepper (PPAPI) GLES interface. Needed by applications
531 that use the 3D graphics API.</dd>
532 <dt>libnacl_io.a</dt>
533 <dd>Provides a POSIX layer for NaCl. In particular, the library provides a
534 virtual file system and support for sockets. The virtual file system
535 allows a module to &#8220;mount&#8221; a given directory tree. Once a module has
536 mounted a file system, it can use standard C library file operations:
537 <code>fopen</code>, <code>fread</code>, <code>fwrite</code>, <code>fseek</code>, and <code>fclose</code>.
538 For more detail, see the header <code>include/nacl_io/nacl_io.h</code>.
539 For an example of how to use nacl_io, see <code>examples/demo/nacl_io_demo</code>.</dd>
540 <dt>libppapi_simple.a</dt>
541 <dd>Provides a familiar C programming environment by letting a module have a
542 simple entry point that is registered by <code>PPAPI_SIMPLE_REGISTER_MAIN</code>.
543 The entry point is similar to the standard C <code>main()</code> function, complete
544 with <code>argc</code> and <code>argv[]</code> parameters. For details see
545 <code>include/ppapi_simple/ps.h</code>. For an example of
546 how to use ppapi_simple, <code>see examples/tutorial/using_ppapi_simple</code>.</dd>
547 </dl>
548 <aside class="note">
549 <ul class="small-gap">
550 <li>Since the Native Client toolchains use their own library and header search
551 paths, the tools won&#8217;t find third-party libraries you use in your
552 non-Native-Client development. If you want to use a specific third-party
553 library for Native Client development, look for it in <a class="reference external" href="http://code.google.com/p/naclports/">naclports</a>, or port the library yourself.</li>
554 <li>The order in which you list libraries in your build commands is important,
555 since the linker searches and processes libraries in the order in which they
556 are specified. See the *_LDFLAGS variables in the Makefiles of the SDK
557 examples for the order in which specific libraries should be listed.</li>
558 </ul>
560 </aside>
561 </section><section id="troubleshooting">
562 <h2 id="troubleshooting">Troubleshooting</h2>
563 <p>Some common problems, and how to fix them:</p>
564 <section id="undefined-reference-error">
565 <h3 id="undefined-reference-error">&#8220;Undefined reference&#8221; error</h3>
566 <p>An &#8220;undefined reference&#8221; error may indicate incorrect link order and/or
567 missing libraries. For example, if you leave out <code>-lppapi</code> when
568 compiling Pepper applications you&#8217;ll see a series of undefined
569 reference errors.</p>
570 <p>One common type of &#8220;undefined reference&#8221; error is with respect to certain
571 system calls, e.g., &#8220;undefined reference to &#8216;mkdir&#8217;&#8221;. For security reasons,
572 Native Client does not support a number of system calls. Depending on how
573 your code uses such system calls, you have a few options:</p>
574 <ol class="arabic simple">
575 <li>Link with the <code>-lnosys</code> flag to provide empty/always-fail versions of
576 unsupported system calls. This will at least get you past the link stage.</li>
577 <li>Find and remove use of the unsupported system calls.</li>
578 <li>Create your own implementation of the unsupported system calls to do
579 something useful for your application.</li>
580 </ol>
581 <p>If your code uses mkdir or other file system calls, you might find the
582 <a class="reference internal" href="#devcycle-building-nacl-io"><em>nacl_io</em></a> library useful.
583 The nacl_io library essentially does option (3) for you: It lets your
584 code use POSIX-like file system calls, and implements the calls using
585 various technologies (e.g., HTML5 file system, read-only filesystems that
586 use URL loaders, or an in-memory filesystem).</p>
587 </section><section id="can-t-find-libraries-containing-necessary-symbols">
588 <h3 id="can-t-find-libraries-containing-necessary-symbols">Can&#8217;t find libraries containing necessary symbols</h3>
589 <p>Here is one way to find the appropriate library for a given symbol:</p>
590 <pre>
591 &lt;NACL_SDK_ROOT&gt;/toolchain/&lt;platform&gt;_pnacl/bin/pnacl-nm -o \
592 toolchain/&lt;platform&gt;_pnacl/usr/lib/*.a | grep &lt;MySymbolName&gt;
593 </pre>
594 </section><section id="pnacl-abi-verification-errors">
595 <h3 id="pnacl-abi-verification-errors">PNaCl ABI Verification errors</h3>
596 <p>PNaCl has restrictions on what is supported in bitcode. There is a bitcode
597 ABI verifier which checks that the application conforms to the ABI restrictions,
598 before it is translated and run in the browser. However, it is best to
599 avoid runtime errors for users, so the verifier also runs on the developer&#8217;s
600 machine at link time.</p>
601 <p>For example, the following program which uses 128-bit integers
602 would compile with NaCl GCC for the x86-64 target. However, it is not
603 portable and would not compile with NaCl GCC for the i686 target.
604 With PNaCl, it would fail to pass the ABI verifier:</p>
605 <pre class="prettyprint">
606 typedef unsigned int uint128_t __attribute__((mode(TI)));
608 uint128_t foo(uint128_t x) {
609 return x;
611 </pre>
612 <p>With PNaCl you would get the following error at link time:</p>
613 <pre class="prettyprint">
614 Function foo has disallowed type: i128 (i128)
615 LLVM ERROR: PNaCl ABI verification failed
616 </pre>
617 <p>When faced with a PNaCl ABI verification error, check the list of features
618 that are <a class="reference internal" href="/native-client/nacl-and-pnacl.html#when-to-use-nacl"><em>not supported by PNaCl</em></a>.
619 If the problem you face is not listed as restricted,
620 <a class="reference internal" href="/native-client/help.html#help"><em>let us know</em></a>!</p>
621 </section></section></section>
623 {{/partials.standard_nacl_article}}