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