Add a Notification Settings Button to all web notifications behind the web platform...
[chromium-blink-merge.git] / native_client_sdk / doc_generated / devguide / devcycle / vs-addin.html
blobe4fd5d7ea100175f8ddc427ca66d3bf9e9b4f93c
1 {{+bindTo:partials.standard_nacl_article}}
3 <section id="debugging-with-visual-studio">
4 <span id="devcycle-vs-addin"></span><h1 id="debugging-with-visual-studio"><span id="devcycle-vs-addin"></span>Debugging with Visual Studio</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><a class="reference internal" href="#introduction" id="id1">Introduction</a></li>
9 <li><p class="first"><a class="reference internal" href="#platforms" id="id2">Platforms</a></p>
10 <ul class="small-gap">
11 <li><a class="reference internal" href="#the-ppapi-platform" id="id3">The PPAPI platform</a></li>
12 <li><a class="reference internal" href="#the-native-client-platforms" id="id4">The Native Client platforms</a></li>
13 </ul>
14 </li>
15 <li><p class="first"><a class="reference internal" href="#installing-the-add-in" id="id5">Installing the add-in</a></p>
16 <ul class="small-gap">
17 <li><a class="reference internal" href="#set-environment-variables" id="id6">Set environment variables</a></li>
18 <li><a class="reference internal" href="#download-the-add-in" id="id7">Download the add-in</a></li>
19 <li><a class="reference internal" href="#run-the-installer" id="id8">Run the installer</a></li>
20 </ul>
21 </li>
22 <li><p class="first"><a class="reference internal" href="#try-the-hello-world-gles-sample-project" id="id9">Try the <code>hello_world_gles</code> sample project</a></p>
23 <ul class="small-gap">
24 <li><a class="reference internal" href="#select-the-nacl64-platform" id="id10">Select the NaCl64 platform</a></li>
25 <li><a class="reference internal" href="#build-and-run-the-project" id="id11">Build and run the project</a></li>
26 <li><a class="reference internal" href="#test-the-nacl-gdb-debugger" id="id12">Test the nacl-gdb debugger</a></li>
27 <li><a class="reference internal" href="#test-the-visual-studio-debugger" id="id13">Test the Visual Studio debugger</a></li>
28 <li><a class="reference internal" href="#inspect-the-platform-properties" id="id14">Inspect the platform properties</a></li>
29 </ul>
30 </li>
31 <li><p class="first"><a class="reference internal" href="#developing-for-native-client-in-visual-studio" id="id15">Developing for Native Client in Visual Studio</a></p>
32 <ul class="small-gap">
33 <li><a class="reference internal" href="#adding-platforms-to-a-project" id="id16">Adding platforms to a project</a></li>
34 <li><a class="reference internal" href="#selecting-a-toolchain" id="id17">Selecting a toolchain</a></li>
35 <li><a class="reference internal" href="#adding-libraries-to-a-project" id="id18">Adding libraries to a project</a></li>
36 <li><a class="reference internal" href="#running-a-web-server" id="id19">Running a web server</a></li>
37 <li><a class="reference internal" href="#keeping-track-of-all-the-pieces" id="id20">Keeping track of all the pieces</a></li>
38 <li><a class="reference internal" href="#using-the-debuggers" id="id21">Using the debuggers</a></li>
39 <li><a class="reference internal" href="#disable-chrome-caching" id="id22">Disable Chrome caching</a></li>
40 <li><a class="reference internal" href="#a-warning-about-postmessage" id="id23">A warning about PostMessage</a></li>
41 <li><a class="reference internal" href="#porting-windows-applications-to-native-client-in-visual-studio" id="id24">Porting Windows applications to Native Client in Visual Studio</a></li>
42 </ul>
43 </li>
44 </ul>
46 </div><p>Whether you&#8217;re porting an existing project or starting from scratch, the Native
47 Client Visual Studio add-in makes it easier to set up, build, run and debug
48 your Native Client app by integrating the Native Client SDK development tools
49 into the Visual Studio environment.</p>
50 <aside class="note">
51 The Native Client add-in requires Visual Studio 2012 or Visual Studio 2010
52 with Service Pack 1. No other versions of Visual Studio are currently
53 supported. Visual Studio Express is also not supported.
54 </aside>
55 <h2 id="introduction">Introduction</h2>
56 <p>The Native Client add-in for Visual Studio helps you develop your application
57 more efficiently in many ways:</p>
58 <ul class="small-gap">
59 <li>Organize and maintain your code as a Visual Studio project.</li>
60 <li>Iteratively write and test your application more easily. Visual Studio
61 handles the details of launching a web server to serve your module and run
62 the module in Chrome with a debugger attached.</li>
63 <li>Compile your module into a dynamically-linked library (DLL) using Visual
64 Studio&#8217;s C/C++ compiler and run it as a Pepper plugin. This allows you to
65 develop code incrementally, coding and/or porting one feature at a time into
66 the Pepper APIs while continuing to use native Windows APIs that would
67 otherwise be unavailable in an actual Native Client module.</li>
68 <li>Use Visual Studio&#8217;s built-in debugger to debug your code while it’s running
69 as a Pepper plugin.</li>
70 <li>Compile your module into a .nexe or .pexe file using the Native Client SDK
71 tools and run it as a bona fide Native Client module.</li>
72 <li>Use the Native Client debugger, nacl-gdb, to test your code when it’s running
73 as a Native Client object.</li>
74 </ul>
75 <p>The add-in defines five new Visual Studio platforms: <code>PPAPI</code>, <code>NaCl32</code>,
76 <code>NaCl64</code>, <code>NaClARM</code>, and <code>PNaCl</code>. These platforms can be applied to the
77 debug configuration of solutions and projects. The platforms configure the
78 properties of your project so it can be built and run as either a Pepper plugin
79 or a Native Client module. The platforms also define the behavior associated
80 with the debug command so you can test your code while running in Visual
81 Studio.</p>
82 <h2 id="platforms">Platforms</h2>
83 <p>It is helpful to consider the Visual Studio add-in platforms in two groups. One
84 contains the PPAPI platform only. The other group, which we&#8217;ll call the Native
85 Client platforms, contains platforms that all have &#8220;NaCl&#8221; in their names:
86 <code>NaCl32</code>, <code>NaCl64</code>, <code>NaClARM</code>, and <code>PNaCl</code>. The diagram below shows the
87 platforms, the ways they are normally used, and the build products they produce.</p>
88 <img alt="/native-client/images/visualstudio4.png" src="/native-client/images/visualstudio4.png" />
89 <p>Using platforms, your workflow is faster and more efficient. You can compile,
90 start, and debug your code with one click or key-press. When you press F5, the
91 “start debugging” command, Visual Studio automatically launches a web server to
92 serve your module (if necessary) along with an instance of Chrome that runs
93 your Native Client module, and also attaches an appropriate debugger.</p>
94 <p>You can switch between platforms as you work to compare the behavior of your
95 code.</p>
96 <p>When you run your project, Visual Studio launches the PPAPI and Native Client
97 platforms in different ways, as explained in the next sections.</p>
98 <h3 id="the-ppapi-platform">The PPAPI platform</h3>
99 <p>The PPAPI platform builds your module as a dynamic library and launches a
100 version of Chrome that’s configured to run the library as a plugin when it
101 encounters an <code>&lt;embed&gt;</code> element with <code>type=application/x-nacl</code> (ignoring
102 the information in the manifest file). When running in the PPAPI platform, you
103 can use Windows system calls that are unavailable in a regular Native Client
104 module built and running as a .nexe file. This offers the ability to port
105 existing code incrementally, rewriting functions using the PPAPI interfaces one
106 piece at a time. Since the module is built with Visual Studio’s native compiler
107 (MSBuild) you can use the Visual Studio debugger to control and inspect your
108 code.</p>
109 <h3 id="the-native-client-platforms">The Native Client platforms</h3>
110 <p>There are four Native Client platforms. All of them can be used to build Native
111 Client modules. When you run one of the Native Client platforms Visual Studio
112 builds the corresponding type of Native Client module (either a .nexe or
113 .pexe), starts a web server to serve it up, and launches a copy of Chrome that
114 fetches the module from the server and runs it. Visual Studio will also open a
115 terminal window, launch an instance of nacl-gdb, and attach it to your module&#8217;s
116 process so you can use gdb commands to debug.</p>
117 <h4 id="nacl32-and-nacl64">NaCl32 and NaCl64</h4>
118 <p>The platforms named NaCl32 and NaCl64 are targeted at x86 32-bit and 64-bit
119 systems respectively. You need both platforms to build a full set of .nexe
120 files when you are ready to distribute your application. Note, however, that
121 when you are testing in Visual Studio you must select the NaCl64 platform
122 (because Chrome for Windows runs Native Client in a 64-bit process). If you try
123 to run from the NaCl32 platform you will get an error message.</p>
124 <h4 id="naclarm">NaClARM</h4>
125 <p>The NaClARM platform is targeted at ARM-based processors. You can build .nexe
126 files with the NaClARM platform in Visual Studio but you cannot run them from
127 there. You can use Visual Studio to create a Native Client module that includes
128 an ARM-based .nexe file and then run the module from a Chrome browser on an ARM
129 device, such as one of the newer Chromebook computers. See the instructions at
130 <a class="reference internal" href="/native-client/devguide/devcycle/running.html"><em>Running Native Client Applications</em></a> for more information on
131 testing your module in Chrome.</p>
132 <aside class="note">
133 Note: The NaClARM platform currently supports the newlib toolchain only.
134 </aside>
135 <h4 id="pnacl">PNaCl</h4>
136 <p>The PNaCl (portable NaCl) platform is included in the Visual Studio Native
137 Client add-in versions 1.1 and higher. It supports the .pexe file format. A
138 .pexe file encodes your application as bitcode for a low level virtual machine
139 (LLVM). When you deliver a Native Client application as a PNaCl module, the
140 manifest file will contain a single .pexe file rather than multiple .nexe
141 files. The Chrome client transforms the LLVM bitcode into machine instructions
142 for the local system.</p>
143 <p>When you run the PNaCl platform from Visual Studio, Visual Studio uses the
144 Native Client SDK to transform the .pexe file into a NaCl64 .nexe file and runs
145 it as if you were working with a NaCl64 platform.</p>
146 <aside class="note">
147 Note: The PNaCl platform currently supports the newlib toolchain only.
148 </aside>
149 <h2 id="installing-the-add-in">Installing the add-in</h2>
150 <p>In order to use the Native Client Visual Studio add-in, your development
151 environment should include:</p>
152 <ul class="small-gap">
153 <li>A 64-bit version of Windows Vista or Windows 7.</li>
154 <li>Visual Studio 2012 or Visual Service 2010 with Service Pack 1.</li>
155 <li><a class="reference external" href="https://www.google.com/intl/en/chrome/browser/">Chrome</a> version 23 or
156 greater. You can choose to develop using the latest <a class="reference external" href="https://www.google.com/intl/en/chrome/browser/canary.html">canary</a> build of
157 Chrome, running the canary version side-by-side with (and separately from)
158 your regular version of Chrome.</li>
159 <li><a class="reference internal" href="/native-client/sdk/download.html"><em>The Native Client SDK</em></a> with the <code>pepper_23</code>
160 bundle or greater. The version of Chrome that you use must be equal or
161 greater than the version of the SDK bundle.</li>
162 </ul>
163 <h3 id="set-environment-variables">Set environment variables</h3>
164 <p>Before you run the installer you must define two Windows environment variables.
165 They point to the bundle in the Native Client SDK that you use to build your
166 module, and to the Chrome browser that you choose to use for debugging.</p>
167 <p>To set environment variables in Windows 7, go to the Start menu and search for
168 &#8220;environment.&#8221; One of the links in the results is &#8220;Edit environment variables
169 for your account.&#8221; (You can also reach this link from the <code>Control Panel</code>
170 under <code>User Accounts</code>.) Click on the link and use the buttons in the window
171 to create or change these user variables (the values shown below are only for
172 example):</p>
173 <table border="1" class="docutils">
174 <colgroup>
175 </colgroup>
176 <thead valign="bottom">
177 <tr class="row-odd"><th class="head">Variable Name</th>
178 <th class="head">Description</th>
179 </tr>
180 </thead>
181 <tbody valign="top">
182 <tr class="row-even"><td><code>NACL_SDK_ROOT</code></td>
183 <td>The path to the pepper directory in the SDK.
184 For example: <code>C:\nacl_sdk\pepper_23</code></td>
185 </tr>
186 <tr class="row-odd"><td><code>CHROME_PATH</code></td>
187 <td>The path to the .exe file for the version of Chrome you
188 are testing with. For example:
189 <code>C:\Users\fred\AppData\Local\Google\Chrome
190 SxS\Application\chrome.exe</code></td>
191 </tr>
192 </tbody>
193 </table>
194 <h3 id="download-the-add-in">Download the add-in</h3>
195 <p>The Native Client Visual Studio add-in is a separate bundle in the SDK named
196 <code>vs_addin</code>. Open a command prompt window, go to the top-level SDK directory,
197 and run the update command, specifying the add-in bundle:</p>
198 <pre class="prettyprint">
199 naclsdk update vs_addin
200 </pre>
201 <p>This creates a folder named <code>vs_addin</code>, containing the add-in itself, its
202 installer files, and a directory of examples.</p>
203 <aside class="note">
204 Note: The vs_addin bundle is only visible when you run <code>naclsdk</code> on a
205 Windows system.
206 </aside>
207 <h3 id="run-the-installer">Run the installer</h3>
208 <p>The installer script is located inside the <code>vs_addin</code> folder in the SDK.
209 Right click on the file <code>install.bat</code> and run it as administrator.</p>
210 <p>The script always installs the NativeClient platforms, and asks you if you’d
211 like to install the PPAPI platform as well. You can skip the PPAPI step and run
212 the installer again later to add the PPAPI platform.</p>
213 <p>You can usually run the installer successfully with no arguments. The new
214 platforms are installed in <code>C:\Program Files
215 (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms</code>.</p>
216 <p>In some cases system resources may not be in their default locations. You might
217 need to use these command line arguments when you run <code>install.bat</code>:</p>
218 <ul class="small-gap">
219 <li>The MSBuild folder is assumed to be at <code>C:\Program Files (x86)\MSBuild</code>.
220 You can specify an alternate path with the flag <code>--ms-build-path=&lt;path&gt;</code>.
221 The installer assumes Visual Studio has created a user folder at</li>
222 <li>The addin itself is normally installed in <code>%USERPROFILE%\My Documents\Visual
223 Studio 2012</code> (or 2010 for Visual Studio 2010). You can specify alternate
224 paths with the <code>--vsuser-path=&lt;path&gt;</code> flag.</li>
225 </ul>
226 <p>From time to time an update to the Visual Studio add-in may become available.
227 Updates are performed just like an installation. Download the new add-in using
228 naclsdk update and run <code>install.bat</code> as administrator.</p>
229 <p>To uninstall the add-in, run <code>install.bat</code> as administrator and add the
230 <code>--uninstall</code> flag. You&#8217;ll need to run the Command Prompt program as
231 administrator in order to add the flag. Go the to the Windows start menu,
232 search for &#8220;Command Prompt,&#8221; right click on the program and run it as
233 administrator.</p>
234 <p>You can verify that the add-in has been installed and determine its version by
235 selecting Add-in Manager in the Visual Studio Tools menu. If the add-in has
236 been installed it will appear in the list of available add-ins. Select it and
237 read its description.</p>
238 <h2 id="try-the-hello-world-gles-sample-project">Try the <code>hello_world_gles</code> sample project</h2>
239 <p>The add-in comes with an examples directory. Open the sample project
240 <code>examples\hello_world_gles\hello_world_gles.sln</code>. This project is an
241 application that displays a spinning cube.</p>
242 <h3 id="select-the-nacl64-platform">Select the NaCl64 platform</h3>
243 <p>Open the sample project in Visual Studio, select the <code>Configuration Manager</code>,
244 and confirm that the active solution configuration is <code>Debug</code> and the active
245 project platform is <code>NaCl64</code>. Note that the platform for the
246 <code>hello_world_gles</code> project is also <code>NaCl64</code>. (You can get to the
247 <code>Configuration Manager</code> from the <code>Build</code> menu or the project’s
248 <code>Properties</code> window.)</p>
249 <img alt="/native-client/images/visualstudio1.png" src="/native-client/images/visualstudio1.png" />
250 <h3 id="build-and-run-the-project">Build and run the project</h3>
251 <p>Use the debugging command (F5) to build and run the project. As the wheels
252 start to turn, you may be presented with one or more alerts. They are benign;
253 you can accept them and set options to ignore them when that’s possible. Some
254 of the messages you might see include:</p>
255 <ul class="small-gap">
256 <li>&#8220;This project is out of date, would you like to build it?&#8221;</li>
257 <li>&#8220;Please specify the name of the executable file to be used for the debug
258 session.&#8221; This should be the value of the environment variable CHROME_PATH,
259 which is usually supplied as the default value in the dialog.</li>
260 <li>&#8220;Debugging information for chrome.exe cannot be found.&#8221; This is to be
261 expected, you are debugging your module&#8217;s code, not Chrome.</li>
262 <li>&#8220;Open file - security warning. The publisher could not be verified.&#8221; If
263 Visual Studio is complaining about x86_64-nacl-gdb.exe, that’s our debugger.
264 Let it be.</li>
265 </ul>
266 <p>Once you’ve passed these hurdles, the application starts to run and you’ll see
267 activity in three places:</p>
268 <ol class="arabic simple">
269 <li>A terminal window opens running <code>nacl-gdb</code>.</li>
270 <li>Chrome launches running your module in a tab.</li>
271 <li>The Visual Studio output window displays debugging messages when you select
272 the debug output item.
273 Stop the debugging session by closing the Chrome window, or select the stop
274 debugging command from the debug menu. The nacl-gdb window will close when
275 you stop running the program.</li>
276 </ol>
277 <h3 id="test-the-nacl-gdb-debugger">Test the nacl-gdb debugger</h3>
278 <p>Add a breakpoint at the SwapBuffers call in the function MainLoop, which is in
279 hello_world.cc.</p>
280 <img alt="/native-client/images/visualstudio2.png" src="/native-client/images/visualstudio2.png" />
281 <p>Start the debugger again (F5). This time the existing breakpoint is loaded into
282 nacl-gcb and the program will pause there. Type c to continue running. You can
283 use gdb commands to set more breakpoints and step through the application. For
284 details, see <a class="reference internal" href="/native-client/devguide/devcycle/debugging.html#using-gdb"><em>Debugging with nacl-gdb</em></a> (scroll down to the end
285 of the section to see some commonly used gdb commands).</p>
286 <h3 id="test-the-visual-studio-debugger">Test the Visual Studio debugger</h3>
287 <p>If you’ve installed the <code>PPAPI</code> platform, go back to the <code>Configuration
288 Manager</code> and select the <code>PPAPI</code> platform. This time when Chrome launches the
289 <code>nacl-gdb</code> window will not appear; the Visual Studio debugger is fully
290 engaged and on the job.</p>
291 <h3 id="inspect-the-platform-properties">Inspect the platform properties</h3>
292 <p>At this point, it may be helpful to take a look at the properties that are
293 associated with the PPAPI and Native Client platforms&#8212;see the settings in the
294 sample project as an example.</p>
295 <h2 id="developing-for-native-client-in-visual-studio">Developing for Native Client in Visual Studio</h2>
296 <p>After you’ve installed the add-in and tried the sample project, you’re ready to
297 start working with your own code. You can reuse the sample project and the
298 PPAPI and Native Client platforms it already has by replacing the source code
299 with your own. More likely, you will add the platforms to an existing project,
300 or to a new project that you create from scratch.</p>
301 <h3 id="adding-platforms-to-a-project">Adding platforms to a project</h3>
302 <p>Follow these steps to add the Native Client and PPAPI platforms to a project:</p>
303 <ol class="arabic simple">
304 <li>Open the Configuration Manager.</li>
305 <li>On the row corresponding to your project, click the Platform column dropdown
306 menu and select <code>&lt;New...&gt;</code>.</li>
307 <li>Select <code>PPAPI</code>, <code>NaCl32</code>, <code>NaCl64</code>, or <code>PNaCl</code> from the New platform
308 menu.</li>
309 <li>In most cases, you should select <code>&lt;Empty&gt;</code> in the “Copy settings from”
310 menu. <strong>Never copy settings between ``PPAPI``, ``NaCl32``, ``NaCl64``,
311 ``NaClARM``, or ``PNaCl`` platforms</strong>. You can copy settings from a Win32
312 platform, if one exists, but afterwards be sure that the project properties
313 are properly set for the new platform, as mentioned in step 6 below.</li>
314 <li>If you like, check the “Create new solutions platform” box to create a
315 solution platform in addition to a project platform. (This is optional, but
316 it can be convenient since it lets you switch project platforms from the
317 Visual Studio main window by selecting the solution platform that has the
318 same name.)</li>
319 <li>Review the project properties for the new platform you just added. In most
320 cases, the default properties for each platform should be correct, but it
321 pays to check. Be especially careful about custom properties you may have
322 set beforehand, or copied from a Win32 platform. Also confirm that the
323 Configuration type is correct:<ul class="small-gap">
324 <li><code>Dynamic Library</code> for <code>PPAPI</code></li>
325 <li><code>Application (.pexe)</code> for <code>PNaCl</code></li>
326 <li><code>Application (.nexe)</code> for <code>NaCl32</code>, <code>NaCl64</code>, and <code>NaClARM</code></li>
327 </ul>
328 </li>
329 </ol>
330 <h3 id="selecting-a-toolchain">Selecting a toolchain</h3>
331 <p>When you build a Native Client module directly from the SDK you can use two
332 different toolchains, newlib or glibc. See <a class="reference internal" href="/native-client/devguide/devcycle/dynamic-loading.html"><em>Dynamic Linking and Loading
333 with glibc</em></a> for a description of the two toolchains and
334 instructions on how to build and deploy an application with the glibc
335 toolchain. The Native Client platforms offer you the same toolchain choice. You
336 can specify which toolchain to use in the project properties, under
337 <code>Configuration Properties &gt; General &gt; Native Client &gt; Toolchain</code>.</p>
338 <aside class="note">
339 Currently, the NaClARM and PNaCl platforms only support the newlib toolchain.
340 </aside>
341 <p>There is no toolchain property for the PPAPI platform. The PPAPI platform uses
342 the toolchain and libraries that come with Visual Studio.</p>
343 <h3 id="adding-libraries-to-a-project">Adding libraries to a project</h3>
344 <p>If your Native Client application requires libraries that are not included in
345 the SDK you must add them to the project properties (under <code>Configuration
346 Properties &gt; Linker &gt; Input &gt; Additional Dependencies</code>), just like any other
347 Visual Studio project. This list of dependencies is a semi-colon delimited
348 list. On the PPAPI platform the library names include the .lib extension (e.g.,
349 <code>ppapi_cpp.lib;ppapi.lib</code>). On the Native Client platforms the extension is
350 excluded (e.g., <code>ppapi_cpp;ppapi</code>).</p>
351 <h3 id="running-a-web-server">Running a web server</h3>
352 <p>In order for the Visual Studio add-in to test your Native Client module, you
353 must serve the module from a web server. There are two options:</p>
354 <h4 id="running-your-own-server">Running your own server</h4>
355 <p>When you start a debug run Visual Studio launches Chrome and tries to connect
356 to the web server at the address found in the Chrome command arguments (see the
357 project’s Debugging &gt; Command configuration property), which is usually
358 <code>localhost:$(NaClWebServerPort)</code>. If you are using your own server be sure to
359 specify its address in the command arguments property, and confirm that your
360 server is running before starting a debug session. Also be certain that the
361 server has all the files it needs to deliver a Native Client module (see
362 “Keeping track of all the pieces”, below).</p>
363 <h4 id="running-the-sdk-server">Running the SDK server</h4>
364 <p>If there is no web server running at the specified port, Visual Studio will try
365 to launch the simple Python web server that comes with the Native Client SDK.
366 It looks for a copy of the server in the SDK itself (at
367 <code>%NACL_SDK_ROOT%\tools\httpd.py</code>), and in the project directory
368 (<code>$(ProjectDir)/httpd.py</code>). If the server exists in one of those locations,
369 Visual Studio launches the server. The server output appears in Visual Studio’s
370 Output window, in the pane named “Native Client Web Server Output”. A server
371 launched in this way is terminated when the debugging session ends.</p>
372 <h3 id="keeping-track-of-all-the-pieces">Keeping track of all the pieces</h3>
373 <p>No matter where the web server lives or how it’s launched you must make sure
374 that it has all the files that your application needs:</p>
375 <ul class="small-gap">
376 <li>All Native Client applications must have an <a class="reference internal" href="/native-client/devguide/coding/application-structure.html#html-file"><em>html host page</em></a>. This file is typically called <code>index.html</code>. The host page
377 must have an embed tag with its type attribute set to
378 <code>application-type/x-nacl</code>. If you plan to use a Native Client platform the
379 embed tag must also include a src attribute pointing to a Native Client
380 manifest (.mnf) file.</li>
381 <li>If you are using a Native Client platform you must include a valid
382 <a class="reference internal" href="/native-client/devguide/coding/application-structure.html#manifest-file"><em>manifest file</em></a>. The manifest file points to the .pexe
383 or .nexe files that Visual Studio builds. These will be placed in the
384 directory specified in the project’s <code>General &gt; Output Directory</code>
385 configuration property, which is usually <code>$(ProjectDir)$(ToolchainName)</code>.
386 Visual Studio can use the Native Client SDK script create_nmf.py to
387 automatically generate the manifest file for you. To use this script set the
388 project&#8217;s <code>Linker &gt; General &gt; Create NMF Automatically</code> property to &#8220;yes.&#8221;</li>
389 </ul>
390 <p>If you are letting Visual Studio discover and run the SDK server, these files
391 should be placed in the project directory. If you are running your own server,
392 you must be sure that the host page <code>index.html</code> is placed in your server’s
393 root directory. Remember, if you’re using one of the Native Client platforms
394 the paths for the manifest file and .pexe or .nexe files must be reachable from
395 the server.</p>
396 <p>The structure of the manifest file can be more complicated if your application
397 uses Native Client&#8217;s ability to dynamically link libraries. You may have to add
398 additional information about dynamically linked libraries to the manifest file
399 even if you create it automatically. The use and limitations of the create_nmf
400 tool are explained in <a class="reference internal" href="/native-client/devguide/devcycle/dynamic-loading.html#dynamic-loading-manifest"><em>Generating a Native Client manifest file for a
401 dynamically linked application</em></a>.</p>
402 <p>You can look at the example projects in the SDK to see how the index and
403 manifest files are organized. The example project <code>hello_nacl</code> has a
404 subdirectory also called <code>hello_nacl</code>. That folder contains <code>index.html</code>
405 and <code>hello_nacl.nmf</code>. The nexe file is found in
406 <code>NaCl64\newlib\Debug\hello_nacl_64.nexe</code>. The <code>hello_world_gles</code> example
407 project contains a subdirectory called <cite>hello_world_gles`</cite>. That directory
408 contains html files built with both toolchains (<code>index_glibc.html</code> and
409 <code>index_newlib.html</code>). The .nexe and .nmf files are found in the newlib and
410 glibc subfolders. For additional information about the parts of a Native Client
411 application, see <a class="reference internal" href="/native-client/devguide/coding/application-structure.html"><em>Application Structure</em></a>.</p>
412 <h3 id="using-the-debuggers">Using the debuggers</h3>
413 <p>PPAPI plugins are built natively by Visual Studio’s compiler (MSBuild), and
414 work with Visual Studio’s debugger in the usual way. You can set breakpoints in
415 the Visual Studio source code files before you begin debugging, and on-the-fly
416 while running the program.</p>
417 <p>NaCl32 and NaClARM executables (.nexe files) cannot be run or debugged from
418 Visual Studio.</p>
419 <p>NaCl64 executables (.nexe files) are compiled using one of the Native Client
420 toolchains in the SDK, which create an <a class="reference external" href="http://en.wikipedia.org/wiki/Executable_and_Linkable_Format">ELF-formatted</a> executable. To
421 debug a running .nexe you must use nacl-gdb, which is a command line debugger
422 that is not directly integrated with Visual Studio. When you start a debugging
423 session running from a NaCl64 platform, Visual Studio automatically launches
424 nacl-gdb for you and attaches it to the nexe. Breakpoints that you set in
425 Visual Studio before you start debugging are transferred to nacl-gdb
426 automatically. During a NaCl debugging session you can only use nacl-gdb
427 commands.</p>
428 <p>The PNaCl platform generates a .pexe file. When you run the debugger add-in
429 translates the .pexe file to a .nexe file and runs the resulting binary with
430 nacl-gdb attached.</p>
431 <p>For additional information about nacl-gdb, see <a class="reference internal" href="/native-client/devguide/devcycle/debugging.html#using-gdb"><em>Debugging with nacl-gdb</em></a> (scroll down to the end of the section to see some commonly used
432 gdb commands).</p>
433 <p>Note that you can’t use the Start Without Debugging command (Ctrl+F5) with a
434 project in the Debug configuration. If you do, Chrome will hang because the
435 Debug platform launches Chrome with the command argument
436 <code>--wait-for-debugger-children</code> (in PPAPI) or <code>--enable-nacl-debug</code> (in a
437 Native Client platform). These flags cause Chrome to pause and wait for a
438 debugger to attach. If you use the Start Without Debugging command, no debugger
439 attaches and Chrome just waits patiently. To use Start Without Debugging,
440 switch to the Release configuration, or manually remove the offending argument
441 from the <code>Command Arguments</code> property.</p>
442 <h3 id="disable-chrome-caching">Disable Chrome caching</h3>
443 <p>When you debug with a Native Client platform you might want to <a class="reference internal" href="/native-client/devguide/devcycle/running.html#cache"><em>disable
444 Chrome&#8217;s cache</em></a> to be sure you are testing your latest and greatest
445 code.</p>
446 <h3 id="a-warning-about-postmessage">A warning about PostMessage</h3>
447 <p>Some Windows libraries define the symbol <code>PostMessage</code> as <code>PostMessageW</code>.
448 This can cause havoc if you are working with the PPAPI platform and you use the
449 Pepper <code>PostMessage()</code> call in your module. Some Pepper API header files
450 contain a self-defensive fix that you might need yourself, while you are
451 testing on the PPAPI platform. Here it is:</p>
452 <pre class="prettyprint">
453 // If Windows defines PostMessage, undef it.
454 #ifdef PostMessage
455 #undef PostMessage
456 #endif
457 </pre>
458 <h3 id="porting-windows-applications-to-native-client-in-visual-studio">Porting Windows applications to Native Client in Visual Studio</h3>
459 <p>At Google I/O 2012 we demonstrated how to port a Windows desktop application to
460 Native Client in 60 minutes. The <a class="reference external" href="http://www.youtube.com/watch?v=1zvhs5FR0X8&amp;feature=plcp">video</a> is available to
461 watch on YouTube. The <code>vs_addin/examples</code> folder contains a pair of simple
462 examples that demonstrate porting process. They are designed to be completed
463 in just 5 minutes. The two examples are called <code>hello_nacl</code> and
464 <code>hello_nacl_cpp</code>. They are essentially the same, but the former uses the C
465 PPAPI interface while the latter uses the C++ API. The application is the
466 familiar &#8220;Hello, World.&#8221;</p>
467 <p>Each example begins with the Windows desktop version running in the <code>Win32</code>
468 platform. From there you move to the <code>PPAPI</code> platform, where you perform a
469 series of steps to set up the Native Client framework, use it to run the
470 desktop version, and then port the behavior from Windows calls to the PPAPI
471 interface. You wind up with a program that uses no Windows functions, which
472 can run in either the <code>PPAPI</code> or the <code>NaCl64</code> platform.</p>
473 <p>The example projects use a single source file (<code>hello_nacl.c</code> or
474 <code>hello_nacl_cpp.cpp</code>). Each step in the porting process is accomplished by
475 progressively defining the symbols STEP1 through STEP6 in the source. Inline
476 comments explain how each successive step changes the code. View the example
477 code to see how it&#8217;s actually done. Here is a summary of the process:</p>
478 <h4 id="win32-platform">Win32 Platform</h4>
479 <dl class="docutils">
480 <dt>STEP1 Run the desktop application</dt>
481 <dd>Begin by running the original Windows application in the Win32 platform.</dd>
482 </dl>
483 <h4 id="ppapi-platform">PPAPI Platform</h4>
484 <dl class="docutils">
485 <dt>STEP2 Launch Chrome with an empty Native Client module</dt>
486 <dd>Switch to the PPAPI platform and include the code required to initialize a
487 Native Module instance. The code is bare-boned, it does nothing but
488 initialize the module. This step illustrates how Visual Studio handles all
489 the details of launching a web-server and Chrome, and running the Native
490 Client module as a Pepper plugin.</dd>
491 <dt>STEP3 Run the desktop application synchronously from the Native Client module</dt>
492 <dd>The Native Client creates the window directly and then calls WndProc to run
493 the desktop application. Since WndProc spins in its message loop, the call to
494 initialize the module never returns. Close the Hello World window and the
495 module initialization will finish.</dd>
496 <dt>STEP4 Running the desktop application and Native Client asynchronously</dt>
497 <dd>In WndProc replace the message loop with a callback function. Now the app
498 window and the Native Client module are running concurrently.</dd>
499 <dt>STEP5 Redirect output to the web page</dt>
500 <dd>The module initialization code calls initInstanceInBrowserWindow rather than
501 initInstanceInPCWindow. WndProc is no longer used. Instead, postMessage is
502 called to place text (now &#8220;Hello, Native Client&#8221;) in the web page rather than
503 opening and writing to a window. Once you&#8217;ve reached this step you can start
504 porting pieces of the application one feature at a time.</dd>
505 <dt>STEP6 Remove all the Windows code</dt>
506 <dd>All the Windows code is def&#8217;d out, proving we are PPAPI-compliant. The
507 functional code that is running is the same as STEP5.</dd>
508 </dl>
509 <h4 id="nacl64-platform">NaCl64 Platform</h4>
510 <dl class="docutils">
511 <dt>Run the Native Client Module in the NaCl64 platform</dt>
512 <dd>You are still running the STEP6 code, but as a Native Client module rather
513 than a Pepper plugin.</dd>
514 </dl>
515 </section>
517 {{/partials.standard_nacl_article}}