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 <p>Your version of Chrome must be equal to or greater than the version of your SDK
10 bundle. For example, if you
’re developing with the
<code>pepper_31
</code> bundle, you
11 must use Google Chrome version
31 or greater. To find out what version of Chrome
12 you
’re using, type
<code>about:chrome
</code> or
<code>about:version
</code> in the Chrome address
14 <h2 id=
"enable-native-client">Enable Native Client
</h2>
16 If you are using Chrome
31 or later, you can skip this section.
18 <p>To run Portable Native Client applications you must specifically enable Native
21 <li><p class=
"first">Type
<code>about:flags
</code> in the Chrome address bar and scroll down to
“Native
23 <ul class=
"small-gap">
24 <li><p class=
"first">If the link below
“Native Client
” says
“Disable
”, then Native Client is
25 already enabled and you don
’t need to do anything else.
</p>
28 <ul class=
"small-gap">
29 <li><p class=
"first">If the link below
“Native Client
” says
“Enable
”, click the
“Enable
”
34 <li><p class=
"first">Scroll down to the bottom of the page, and click
“Relaunch Now
”. All browser
35 windows will restart when you relaunch Chrome.
</p>
38 <h2 id=
"disable-the-chrome-cache">Disable the Chrome cache
</h2>
39 <p>Chrome caches resources aggressively. When you are building a Native Client
40 application you should disable the cache to make sure that Chrome loads the
42 <ol class=
"arabic simple">
43 <li>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.
</li>
45 <li>Click the gear icon
<img alt=
"gear-icon" src=
"/native-client/images/gear-icon.png" /> in the bottom right corner of the Chrome
47 <li>Under the
“General
” settings, check the box next to
“Disable cache
”.
</li>
49 <h2 id=
"build-the-sdk-examples">Build the SDK examples
</h2>
50 <p>The Makefile scripts for the SDK examples build multiple versions of the
51 examples using all three SDK toolchains (newlib, glibc, and PNaCl) and in both
52 release and debug configurations. (Note that some examples build only with
53 particular toolchains).
</p>
54 <h3 id=
"build-all-examples">Build all examples
</h3>
55 <p>To build all the examples, go to the examples directory in a specific SDK
56 bundle and run
<code>make
</code>:
</p>
57 <pre class=
"prettyprint">
58 $ cd pepper_31/examples
61 make[
1]: Entering directory `pepper_31/examples/api'
63 make[
2]: Entering directory `pepper_31/examples/api/audio'
64 CXX newlib/Debug/audio_x86_32.o
65 LINK newlib/Debug/audio_x86_32.nexe
66 CXX newlib/Debug/audio_x86_64.o
67 LINK newlib/Debug/audio_x86_64.nexe
68 CXX newlib/Debug/audio_arm.o
69 LINK newlib/Debug/audio_arm.nexe
70 CREATE_NMF newlib/Debug/audio.nmf
71 make[
2]: Leaving directory `pepper_31/examples/api/audio'
72 make -C url_loader all
73 make[
2]: Entering directory `pepper_31/examples/api/url_loader'
74 CXX newlib/Debug/url_loader_x86_32.o
77 <h3 id=
"build-a-single-example">Build a single example
</h3>
78 <p>Calling
<code>make
</code> from inside a particular example
’s directory will build only
80 <pre class=
"prettyprint">
81 $ cd pepper_31/examples/api/core
83 CXX newlib/Debug/core_x86_32.o
84 LINK newlib/Debug/core_x86_32.nexe
85 CXX newlib/Debug/core_x86_64.o
86 LINK newlib/Debug/core_x86_64.nexe
87 CXX newlib/Debug/core_arm.o
88 LINK newlib/Debug/core_arm.nexe
89 CREATE_NMF newlib/Debug/core.nmf
91 <h3 id=
"override-defaults">Override defaults
</h3>
92 <p>You can call
<code>make
</code> with the
<code>TOOLCHAIN
</code> and
<code>CONFIG
</code> parameters to
93 override the defaults:
</p>
94 <pre class=
"prettyprint">
95 $ make TOOLCHAIN=pnacl CONFIG=Release
96 CXX pnacl/Release/core_pnacl.o
97 LINK pnacl/Release/core.bc
98 FINALIZE pnacl/Release/core.pexe
99 CREATE_NMF pnacl/Release/core.nmf
101 <p>You can also set
<code>TOOLCHAIN
</code> to
“all
” to build one or more examples with
102 all available toolchains:
</p>
103 <pre class=
"prettyprint">
105 make TOOLCHAIN=newlib
106 make[
1]: Entering directory `pepper_31/examples/api/core'
107 CXX newlib/Debug/core_x86_32.o
108 LINK newlib/Debug/core_x86_32.nexe
109 CXX newlib/Debug/core_x86_64.o
110 LINK newlib/Debug/core_x86_64.nexe
111 CXX newlib/Debug/core_arm.o
112 LINK newlib/Debug/core_arm.nexe
113 CREATE_NMF newlib/Debug/core.nmf
114 make[
1]: Leaving directory `pepper_31/examples/api/core'
116 make[
1]: Entering directory `pepper_31/examples/api/core'
117 CXX glibc/Debug/core_x86_32.o
118 LINK glibc/Debug/core_x86_32.nexe
119 CXX glibc/Debug/core_x86_64.o
120 LINK glibc/Debug/core_x86_64.nexe
121 CREATE_NMF glibc/Debug/core.nmf
122 make[
1]: Leaving directory `pepper_31/examples/api/core'
124 make[
1]: Entering directory `pepper_31/examples/api/core'
125 CXX pnacl/Debug/core.o
126 LINK pnacl/Debug/core_unstripped.bc
127 FINALIZE pnacl/Debug/core_unstripped.pexe
128 CREATE_NMF pnacl/Debug/core.nmf
129 make[
1]: Leaving directory `pepper_31/examples/api/core'
131 make[
1]: Entering directory `pepper_31/examples/api/core'
132 CXX linux/Debug/core.o
133 LINK linux/Debug/core.so
134 make[
1]: Leaving directory `pepper_31/examples/api/core'
136 <h2 id=
"build-results">Build results
</h2>
137 <p>After running
<code>make
</code>, each example directory will contain one or more of
138 the following subdirectories:
</p>
139 <ul class=
"small-gap">
140 <li><code>newlib
</code> with subdirectories
<code>Debug
</code> and
<code>Release
</code>;
</li>
141 <li><code>glibc
</code> with subdirectories
<code>Debug
</code> and
<code>Release
</code>;
</li>
142 <li><code>pnacl
</code> with subdirectories
<code>Debug
</code> and
<code>Release
</code>;
</li>
144 <p>For the newlib and glibc toolchains the Debug and Release subdirectories
145 contain .nexe files for all target architectures. For the PNaCl toolchain
146 they contain a single .pexe file. PNaCl debug also produces pre-translated
147 .nexe files, for ease of debugging. All Debug and Release directories contain
148 a manifest (.nmf) file that references the associated .nexe or .pexe files.
149 For information about Native Client manifest files, see the
<a class=
"reference internal" href=
"/native-client/overview.html"><em>Technical
150 Overview
</em></a>.
</p>
151 <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
152 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>
153 <h2 id=
"run-the-sdk-examples"><span id=
"id1"></span>Run the SDK examples
</h2>
154 <p>To run the SDK examples, you can use the
<code>make run
</code> command:
</p>
155 <pre class=
"prettyprint">
156 $ cd pepper_31/examples/api/core
159 <p>This will launch a local HTTP server which will serve the data for the
160 example. It then launches Chrome with the address of this server, usually
161 <code>http://localhost:
5103</code>. After you close Chrome, the local HTTP server is
162 automatically shutdown.
</p>
163 <p>This command will try to find an executable named
<code>google-chrome
</code> in your
164 <code>PATH
</code> environment variable. If it can
’t, you
’ll get an error message like
166 <pre class=
"prettyprint">
167 pepper_31/tools/common.mk:
415: No valid Chrome found at CHROME_PATH=
168 pepper_31/tools/common.mk:
415: *** Set CHROME_PATH via an environment variable, or command-line.. Stop.
170 <p>Set the CHROME_PATH environment variable to the location of your Chrome
172 <ul class=
"small-gap">
173 <li><p class=
"first">On Windows:
</p>
174 <p>The default install location of Chrome is
175 <code>C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
</code> for Chrome
177 <code>C:\Users\
<username
>\AppData\Local\Google\Chrome SxS\Application\chrome.exe
</code>
178 for Chrome Canary; try looking in those directories first:
</p>
179 <pre class=
"prettyprint">
180 > set CHROME_PATH=
<Path to chrome.exe
>
183 <li><p class=
"first">On Linux:
</p>
184 <pre class=
"prettyprint">
185 $ export CHROME_PATH=
<Path to google-chrome
>
188 <li><p class=
"first">On Mac:
</p>
189 <p>The default install location of Chrome is
190 <code>/Applications/Google Chrome.app/Contents/MacOS/Google Chrome
</code> for
192 <code>Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary
</code>
193 for Chrome Canary. Note that you have to reference the executable inside the
194 application bundle, not the top-level
<code>.app
</code> directory:
</p>
195 <pre class=
"prettyprint">
196 $ export CHROME_PATH=
<Path to Google Chrome
>
200 <p>You can run via a different toolchain or configuration by using the
201 <code>TOOLCHAIN
</code> and
<code>CONFIG
</code> parameters to make:
</p>
202 <pre class=
"prettyprint">
203 $ make run TOOLCHAIN=pnacl CONFIG=Debug
205 <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>
206 <p>Each example can also be launched as a packaged app. For more information about
207 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
208 <a class=
"reference external" href=
"/apps/about_apps">Chrome apps documentation
</a>.
</p>
209 <p>Some Pepper features, such as TCP/UDP socket access, are only allowed in
210 packaged apps. The examples that use these features must be run as packaged
211 apps, by using the
<code>make run_package
</code> command:
</p>
212 <pre class=
"prettyprint">
215 <p>You can use
<code>TOOLCHAIN
</code> and
<code>CONFIG
</code> parameters as above to run with a
216 different toolchain or configuration.
</p>
217 <h2 id=
"debugging-the-sdk-examples"><span id=
"id2"></span>Debugging the SDK examples
</h2>
218 <p>The NaCl SDK uses
<a class=
"reference external" href=
"https://www.gnu.org/software/gdb/">GDB
</a> to debug Native
219 Client code. The SDK includes a prebuilt version of GDB that is compatible with
220 NaCl code. To use it, run the
<code>make debug
</code> command from an example directory:
</p>
221 <pre class=
"prettyprint">
224 <p>This will launch Chrome with the
<code>--enable-nacl-debug
</code> flag set. This flag
225 will cause Chrome to pause when a NaCl module is first loaded, waiting for a
226 connection from gdb. The
<code>make debug
</code> command also simultaneously launches
227 GDB and loads the symbols for that NEXE. To connect GDB to Chrome, in the GDB
229 <pre class=
"prettyprint">
230 (gdb) target remote :
4014
232 <p>This tells GDB to connect to a TCP port on
<code>localhost:
4014</code>–the port that
233 Chrome is listening on. GDB will respond:
</p>
234 <pre class=
"prettyprint">
235 Remote debugging using :
4014
236 0x000000000fa00080 in ?? ()
238 <p>At this point, you can use the standard GDB commands to debug your NaCl module.
239 The most common commands you will use to debug are
<code>continue
</code>,
<code>step
</code>,
240 <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
244 {{/partials.standard_nacl_article}}