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