1 {{+bindTo:partials.standard_nacl_article}}
3 <section id="running-the-sdk-examples">
4 <span id="sdk-examples-2"></span><h1 id="running-the-sdk-examples"><span id="sdk-examples-2"></span>Running the SDK Examples</h1>
5 <p>Every Native Client SDK bundle comes with a folder of example applications.
6 Each example demonstrates one or two key Native Client programming concepts.
7 After you’ve <a class="reference internal" href="/native-client/sdk/download.html"><em>downloaded the SDK</em></a>, follow the instructions
8 on this page to build and run the examples.</p>
9 <h2 id="configure-the-google-chrome-browser">Configure the Google Chrome Browser</h2>
11 <li><p class="first">Your version of Chrome must be equal to or greater than the version of
12 your SDK bundle. For example, if you’re developing with the <code>pepper_31</code>
13 bundle, you must use Google Chrome version 31 or greater. To find out what
14 version of Chrome you’re using, type <code>about:chrome</code> or <code>about:version</code>
15 in the Chrome address bar.</p>
17 <li><p class="first">For Portable Native Client, no extra Chrome flags are needed as of
18 Chrome version 31.</p>
19 <p>For other Native Client applications, or to <strong>debug</strong> Portable Native
20 Client applications by translating the <strong>pexe</strong> to a <strong>nexe</strong> ahead of
21 time, enable the Native Client flag. Native Client is enabled by default
22 only for applications distributed through the Chrome Web Store. To run
23 Native Client applications that are not distributed through the Chrome
24 Web Store, like the SDK examples, you must specifically enable the Native
25 Client flag in Chrome:</p>
26 <ul class="small-gap">
27 <li><p class="first">Type <code>about:flags</code> in the Chrome address bar and scroll down to
28 “Native Client”.</p>
30 <li><p class="first">If the link below “Native Client” says “Disable”, then Native Client is
31 already enabled and you don’t need to do anything else.</p>
33 <li><p class="first">If the link below “Native Client” says “Enable”, click the “Enable”
34 link, scroll down to the bottom of the page, and click the “Relaunch
35 Now” button. All browser windows will restart when you relaunch Chrome.</p>
39 <li><p class="first">Disable the Chrome cache. Chrome caches resources aggressively; when you
40 are building a Native Client application you should disable the cache to
41 make sure that Chrome loads the latest version:</p>
42 <ul class="small-gap">
43 <li><p class="first">Open Chrome’s developer tools by clicking the menu icon <img alt="menu-icon" src="/native-client/images/menu-icon.png" /> and
44 choosing Tools > Developer tools.</p>
46 <li><p class="first">Click the gear icon <img alt="gear-icon" src="/native-client/images/gear-icon.png" /> in the bottom right corner of the
49 <li><p class="first">Under the “General” settings, check the box next to “Disable cache”.</p>
54 <h2 id="build-the-sdk-examples">Build the SDK examples</h2>
55 <p>Starting with the <code>pepper_24</code> bundle, the Makefile scripts for the SDK
56 examples build multiple versions of the examples using all three SDK
57 toolchains (newlib, glibc, and PNaCl) and in both release and debug
58 configurations. (Note that some examples build only with the particular
60 <p>To build all the examples, go to the examples directory in a specific SDK
61 bundle and run <code>make</code>:</p>
62 <pre class="prettyprint">
63 $ cd pepper_31/examples
66 make[1]: Entering directory `pepper_31/examples/api'
68 make[2]: Entering directory `pepper_31/examples/api/audio'
69 CXX newlib/Debug/audio_x86_32.o
70 LINK newlib/Debug/audio_x86_32.nexe
71 CXX newlib/Debug/audio_x86_64.o
72 LINK newlib/Debug/audio_x86_64.nexe
73 CXX newlib/Debug/audio_arm.o
74 LINK newlib/Debug/audio_arm.nexe
75 CREATE_NMF newlib/Debug/audio.nmf
76 make[2]: Leaving directory `pepper_31/examples/api/audio'
77 make -C url_loader all
78 make[2]: Entering directory `pepper_31/examples/api/url_loader'
79 CXX newlib/Debug/url_loader_x86_32.o
82 <p>Calling <code>make</code> from inside a particular example’s directory will build only
84 <pre class="prettyprint">
85 $ cd pepper_31/examples/api/core
87 CXX newlib/Debug/core_x86_32.o
88 LINK newlib/Debug/core_x86_32.nexe
89 CXX newlib/Debug/core_x86_64.o
90 LINK newlib/Debug/core_x86_64.nexe
91 CXX newlib/Debug/core_arm.o
92 LINK newlib/Debug/core_arm.nexe
93 CREATE_NMF newlib/Debug/core.nmf
95 <p>You can call <code>make</code> with the <code>TOOLCHAIN</code> and <code>CONFIG</code> parameters to
96 override the defaults:</p>
97 <pre class="prettyprint">
98 $ make TOOLCHAIN=pnacl CONFIG=Release
99 CXX pnacl/Release/core_pnacl.o
100 LINK pnacl/Release/core.bc
101 FINALIZE pnacl/Release/core.pexe
102 CREATE_NMF pnacl/Release/core.nmf
104 <p>You can also set <code>TOOLCHAIN</code> to “all” to build one or more examples with
105 all available toolchains:</p>
106 <pre class="prettyprint">
108 make TOOLCHAIN=newlib
109 make[1]: Entering directory `pepper_31/examples/api/core'
110 CXX newlib/Debug/core_x86_32.o
111 LINK newlib/Debug/core_x86_32.nexe
112 CXX newlib/Debug/core_x86_64.o
113 LINK newlib/Debug/core_x86_64.nexe
114 CXX newlib/Debug/core_arm.o
115 LINK newlib/Debug/core_arm.nexe
116 CREATE_NMF newlib/Debug/core.nmf
117 make[1]: Leaving directory `pepper_31/examples/api/core'
119 make[1]: Entering directory `pepper_31/examples/api/core'
120 CXX glibc/Debug/core_x86_32.o
121 LINK glibc/Debug/core_x86_32.nexe
122 CXX glibc/Debug/core_x86_64.o
123 LINK glibc/Debug/core_x86_64.nexe
124 CREATE_NMF glibc/Debug/core.nmf
125 make[1]: Leaving directory `pepper_31/examples/api/core'
127 make[1]: Entering directory `pepper_31/examples/api/core'
128 CXX pnacl/Debug/core.o
129 LINK pnacl/Debug/core_unstripped.bc
130 FINALIZE pnacl/Debug/core_unstripped.pexe
131 CREATE_NMF pnacl/Debug/core.nmf
132 make[1]: Leaving directory `pepper_31/examples/api/core'
134 make[1]: Entering directory `pepper_31/examples/api/core'
135 CXX linux/Debug/core.o
136 LINK linux/Debug/core.so
137 make[1]: Leaving directory `pepper_31/examples/api/core'
139 <p>After running <code>make</code>, each example directory will contain one or more of
140 the following subdirectories:</p>
141 <ul class="small-gap">
142 <li>a <code>newlib</code> directory with subdirectories <code>Debug</code> and <code>Release</code>;</li>
143 <li>a <code>glibc</code> directory with subdirectories <code>Debug</code> and <code>Release</code>;</li>
144 <li>a <code>pnacl</code> directory with subdirectories <code>Debug</code> and <code>Release</code>;</li>
146 <p>For the newlib and glibc toolchains the Debug and Release subdirectories
147 contain .nexe files for all target architectures. For the PNaCl toolchain
148 they contain a single .pexe file. PNaCl debug also produces pre-translated
149 .nexe files, for ease of debugging. All Debug and Release directories contain
150 a manifest (.nmf) file that references the associated .nexe or .pexe files.
151 For information about Native Client manifest files, see the <a class="reference internal" href="/native-client/overview.html"><em>Technical
152 Overview</em></a>.</p>
153 <p>For details on how to use <code>make</code>, see the <a class="reference external" href="http://www.gnu.org/software/make/manual/make.html">GNU ‘make’ Manual</a>. For details on how to
154 use the SDK toolchain itself, see <a class="reference internal" href="/native-client/devguide/devcycle/building.html"><em>Building Native Client Modules</em></a>.</p>
155 <h2 id="run-the-sdk-examples"><span id="id1"></span>Run the SDK examples</h2>
156 <p>To run the SDK examples, you can use the <code>make run</code> command:</p>
157 <pre class="prettyprint">
158 $ cd pepper_31/examples/api/core
161 <p>This will launch a local HTTP server which will serve the data for the
162 example. It then launches Chrome with the address of this server, usually
163 <code>http://localhost:5103</code>. After you close Chrome, the local HTTP server is
164 automatically shutdown.</p>
165 <p>This command will try to find an executable named <code>google-chrome</code> in your
166 <code>PATH</code> environment variable. If it can’t, you’ll get an error message like
168 <pre class="prettyprint">
169 pepper_31/tools/common.mk:415: No valid Chrome found at CHROME_PATH=
170 pepper_31/tools/common.mk:415: *** Set CHROME_PATH via an environment variable, or command-line.. Stop.
172 <p>Set the CHROME_PATH environment variable to the location of your Chrome
174 <ul class="small-gap">
175 <li><p class="first">On Windows:</p>
176 <p>The default install location of Chrome is
177 <code>C:\Program Files (x86)\Google\Chrome\Application\chrome.exe</code> for Chrome
179 <code>C:\Users\<username>\AppData\Local\Google\Chrome SxS\Application\chrome.exe</code>
180 for Chrome Canary; try looking in those directories first:</p>
181 <pre class="prettyprint">
182 > set CHROME_PATH=<Path to chrome.exe>
185 <li><p class="first">On Linux:</p>
186 <pre class="prettyprint">
187 $ export CHROME_PATH=<Path to google-chrome>
190 <li><p class="first">On Mac:</p>
191 <p>The default install location of Chrome is
192 <code>/Applications/Google Chrome.app/Contents/MacOS/Google Chrome</code> for
194 <code>Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary</code>
195 for Chrome Canary. Note that you have to reference the executable inside the
196 application bundle, not the top-level <code>.app</code> directory:</p>
197 <pre class="prettyprint">
198 $ export CHROME_PATH=<Path to Google Chrome>
202 <p>You can run via a different toolchain or configuration by using the
203 <code>TOOLCHAIN</code> and <code>CONFIG</code> parameters to make:</p>
204 <pre class="prettyprint">
205 $ make run TOOLCHAIN=pnacl CONFIG=Debug
207 <h2 id="run-the-sdk-examples-as-packaged-apps"><span id="run-sdk-examples-as-packaged"></span>Run the SDK examples as packaged apps</h2>
208 <p>Each example can also be launched as a packaged app. For more information about
209 using Native Client for packaged apps, see <a class="reference internal" href="/native-client/devguide/distributing.html#distributing-packaged"><em>Packaged application</em></a>. For general information about packaged apps, see the
210 <a class="reference external" href="/apps/about_apps">Chrome apps documentation</a>.</p>
211 <p>Some Pepper features, such as TCP/UDP socket access, are only allowed in
212 packaged apps. The examples that use these features must be run as packaged
213 apps, by using the <code>make run_package</code> command:</p>
214 <pre class="prettyprint">
217 <p>You can use <code>TOOLCHAIN</code> and <code>CONFIG</code> parameters as above to run with a
218 different toolchain or configuration.</p>
219 <h2 id="debugging-the-sdk-examples"><span id="id2"></span>Debugging the SDK examples</h2>
220 <p>The NaCl SDK uses <a class="reference external" href="https://www.gnu.org/software/gdb/">GDB</a> to debug Native
221 Client code. The SDK includes a prebuilt version of GDB that is compatible with
222 NaCl code. To use it, run the <code>make debug</code> command from an example directory:</p>
223 <pre class="prettyprint">
226 <p>This will launch Chrome with the <code>--enable-nacl-debug</code> flag set. This flag
227 will cause Chrome to pause when a NaCl module is first loaded, waiting for a
228 connection from gdb. The <code>make debug</code> command also simultaneously launches
229 GDB and loads the symbols for that NEXE. To connect GDB to Chrome, in the GDB
231 <pre class="prettyprint">
232 (gdb) target remote :4014
234 <p>This tells GDB to connect to a TCP port on <code>localhost:4014</code>–the port that
235 Chrome is listening on. GDB will respond:</p>
236 <pre class="prettyprint">
237 Remote debugging using :4014
238 0x000000000fa00080 in ?? ()
240 <p>At this point, you can use the standard GDB commands to debug your NaCl module.
241 The most common commands you will use to debug are <code>continue</code>, <code>step</code>,
242 <code>next</code>, <code>break</code> and <code>backtrace</code>. See <a class="reference internal" href="/native-client/devguide/devcycle/debugging.html"><em>Debugging</em></a> for more information about debugging a Native Client
246 {{/partials.standard_nacl_article}}