例子

原文地址:https://developer.chrome.com/native-client/sdk/examples

每个Native Client SDK捆绑包都附带一个示例应用程序的文件夹。每个示例演示一个或两个关键的Native Client编程概念。你后下载SDK,按照此页面上的说明来构建和运行的例子。

您的Chrome版本必须等于或大于SDK捆绑包的版本。例如,如果您使用pepper_35捆绑包进行开发,则必须使用Google Chrome 35或更高版本。要找出您所使用的Chrome版本,类型about:chromeabout:version在Chrome地址栏。

构建SDK示例

SDK示例的Makefile脚本可以使用三个SDK工具链(clang-newlib,glibc和PNaCl)中的任何一个以及发布和调试配置构建多个版本的示例。请注意,某些示例( dlopen例如,仅使用特定工具链构建)。

通过查看VALID_TOOLCHAINS Makefile中针对特定示例的变量,查找每个示例的工具链。列出的第一项是默认值。它是在没有参数的情况下运行示例make文件时构建的。例如,在corepepper_35目录中运行make 使用glibc工具链构建示例。

$ cd pepper_35/examples/api/core
$ make
  CXX  glibc/Release/core_x86_32.o
  LINK glibc/Release/core_unstripped_x86_32.nexe
  VALIDATE glibc/Release/core_unstripped_x86_32.nexe
  CXX  glibc/Release/core_x86_64.o
  LINK glibc/Release/core_unstripped_x86_64.nexe
  VALIDATE glibc/Release/core_unstripped_x86_64.nexe
  CXX  glibc/Release/core_arm.o
  LINK glibc/Release/core_unstripped_arm.nexe
  VALIDATE glibc/Release/core_unstripped_arm.nexe
  STRIP glibc/Release/core_x86_32.nexe
  STRIP glibc/Release/core_x86_64.nexe
  STRIP glibc/Release/core_arm.nexe
  CREATE_NMF glibc/Release/core.nmf

如您所见,这会在pepper_35/examples/api/core/Release目录中生成许多​​特定于体系结构的nexe文件。使用CONFIGmake命令的参数创建调试版本。

$make CONFIG=Debug

这会创建类似的输出,但是pepper_35/examples/api/core/Debug

使用TOOLCHAIN参数选择不同的工具链。例如:

$ cd pepper_35/examples/api/core
$ make TOOLCHAIN=pnacl CONFIG=Release
  CXX  pnacl/Release/core.o
  LINK pnacl/Release/core_unstripped.bc
  FINALIZE pnacl/Release/core_unstripped.pexe
  CREATE_NMF pnacl/Release/core.nmf

您还可以设置TOOLCHAINall构建所有发行版本的默认工具链。

$ cd pepper_35/examples/api/core
$ make TOOLCHAIN=all
make TOOLCHAIN=clang-newlib
make[1]: Entering directory 'pepper_35/examples/api/core'
  CXX  clang-newlib/Release/core_x86_32.o
  LINK clang-newlib/Release/core_unstripped_x86_32.nexe
  VALIDATE clang-newlib/Release/core_unstripped_x86_32.nexe
  CXX  clang-newlib/Release/core_x86_64.o
  LINK clang-newlib/Release/core_unstripped_x86_64.nexe
  VALIDATE clang-newlib/Release/core_unstripped_x86_64.nexe
  CXX  clang-newlib/Release/core_arm.o
  LINK clang-newlib/Release/core_unstripped_arm.nexe
  VALIDATE clang-newlib/Release/core_unstripped_arm.nexe
  STRIP clang-newlib/Release/core_x86_32.nexe
  STRIP clang-newlib/Release/core_x86_64.nexe
  STRIP clang-newlib/Release/core_arm.nexe
  CREATE_NMF clang-newlib/Release/core.nmf
make[1]: Leaving directory 'pepper_35/examples/api/core'
make TOOLCHAIN=glibc
make[1]: Entering directory 'pepper_35/examples/api/core'
  CXX  glibc/Release/core_x86_32.o
  LINK glibc/Release/core_unstripped_x86_32.nexe
  VALIDATE glibc/Release/core_unstripped_x86_32.nexe
  CXX  glibc/Release/core_x86_64.o
  LINK glibc/Release/core_unstripped_x86_64.nexe
  VALIDATE glibc/Release/core_unstripped_x86_64.nexe
  ...
  (content excerpted)
  ...

建立结果

运行后make,示例目录将包含一个或多个以下子目录,具体取决于您运行的Makefile:

  • clang-newlib与子目录DebugRelease;
  • glibc与子目录DebugRelease;
  • pnacl与子目录DebugRelease;

对于clang-newlib和glibc工具链,Debug和Release子目录包含所有目标体系结构的.nexe文件。对于PNaCl工具链,它们包含一个.pexe文件。PNaCl调试还生成预翻译的.nexe文件,以便于调试。所有Debug和Release目录都包含一个清单(.nmf)文件,该文件引用相关的.nexe或.pexe文件。有关Native Client清单文件的信息,请参阅技术概述

有关如何使用的详细信息make,请参阅GNU'make'手册。有关如何使用SDK工具链本身的详细信息,请参阅构建本机客户端模块

运行SDK示例

停用Chrome缓存

Chrome的智能缓存可以积极地缓存资源。构建Native Client应用程序时,应禁用缓存以确保Chrome加载最新版本。在Developer Tools打开时,智能缓存仅保持不活动状态。否则,积极缓存将继续。

  1. 单击菜单图标,菜单图标然后选择工具>开发人员工具,打开Chrome的开发人员工具。
  2. 点击齿轮图标Chrome窗口右下角的齿轮图标。
  3. 在“常规”设置下,选中“禁用缓存”旁边的框。

运行示例

要运行SDK示例,请使用以下make run命令:

$ cd pepper_35/examples/api/core
$ make run

这将启动一个服务于该示例的本地HTTP服务器。然后通常会使用此服务器的地址启动Chrome http://localhost:5103。关闭Chrome后,本地HTTP服务器会自动关闭。

此命令试图找到一个名为可执行google-chrome在你的 PATH环境变量。如果不能,您将收到如下错误消息:

pepper_35/tools/common.mk:415: No valid Chrome found at CHROME_PATH=
pepper_35/tools/common.mk:415: *** Set CHROME_PATH via an environment variable, or command-line..  Stop.

为Chrome添加环境变量

将CHROME_PATH环境变量设置为Chrome可执行文件的位置。

  • 在Windows上:

    Chrome的默认安装位置 C:\Program Files (x86)\Google\Chrome\Application\chrome.exe适用于Chrome稳定版和 C:\Users\<username>\AppData\Local\Google\Chrome SxS\Application\chrome.exe Chrome Canary版。首先尝试查看这些目录:

set CHROME_PATH=<Path to chrome.exe>

在Linux上:

$ export CHROME_PATH=<Path to google-chrome>

在Mac上:

Chrome的默认安装位置 /Applications/Google Chrome.app/Contents/MacOS/Google Chrome适用于Chrome Stable和 Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary Chrome Canary。请注意,您必须引用应用程序包内的可执行文件,而不是顶级.app目录:

$ export CHROME_PATH=<Path to Google Chrome>

将SDK示例作为Chrome应用运行

每个示例也可以作为Chrome应用启动。Chrome应用是Chrome网上应用店中托管的特殊zip文件(扩展名为.crx)。此文件包含所有应用程序部分:Chrome Web Store清单文件(manifest.json),图标和所有常规Native Client应用程序文件。有关创建Chrome应用的详细信息,请参阅什么是 Chrome应用。

某些Pepper功能(如TCPUDP套接字访问)仅允许在Chrome应用中使用。使用这些功能的示例必须使用以下命令作为Chrome应用运行 :

$ make run_package

您可以使用上述的参数TOOLCHAINCONFIG参数来运行不同的工具链或配置。

调试SDK示例

NaCl SDK使用GDB来调试Native Client代码。SDK包含与NaCl代码兼容的预建版GDB。要使用它,请make debug从示例目录运行该命令:

$ make debug

这将启动带有--enable-nacl-debug标志集的Chrome 。当首次加载NaCl模块时,此标志会导致Chrome暂停,等待来自gdb的连接。该make debug命令还同时启动GDB并加载该NEXE的符号。要将GDB连接到Chrome,请在GDB控制台中键入:

(gdb) target remote :4014

这告诉GDB连接到localhost:4014Chrome正在侦听的端口上的TCP端口。GDB将回应:

Remote debugging using :4014
0x000000000fa00080 in ?? ()

此时,您可以使用标准GDB命令调试NaCl模块。您将使用调试最常用的命令是continuestep, nextbreakbacktrace。有关调试 Native Client应用程序的详细信息,请参阅 调试

CC-By 3.0许可下提供的内容

posted @ 2018-07-19 15:25  SunkingYang  阅读(182)  评论(0编辑  收藏  举报