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