建立 Gecko SDK

  • 版本网址缩略名: 创建_XPCOM_组件/建立_Gecko_SDK
  • 版本标题: 建立 Gecko SDK
  • 版本 id: 274461
  • 创建于:
  • 创建者: Secure alex
  • 是否是当前版本?
  • 评论 /* 下载和建立 SDK */

修订内容

{{template.PreviousNext("创建 XPCOM 组件:打包 WebLock", "创建 XPCOM 组件:资源")}}

这一章提供建立 Gecko SDK 的基本方法, 下面会告诉开发人员如何下载和组织 Gecko SDK, 如何象 WebLock 一样创建一个新的组件工程.

下载和建立 SDK

Gecko SDK 提供了编译 XPCOM 组件所需要的所有的工具, 头文件和库. SDK 现在有 Windows 和 Linux 两个版本, 其他操作系统上的 SDK 正在开发. SDK 在下面的地址下载:

  • Linux: http://ftp.mozilla.org/pub/mozilla/releases/mozilla1.4a/gecko-sdk-i686-pc-linux-gnu-1.4a.tar.gz
  • Windows: http://ftp.mozilla.org/pub/mozilla/releases/mozilla1.4a/gecko-sdk-win32-1.4a.zip

注意版号要大于1.4a. 可以在下面的地址获取更新的 SDK 版本 http://ftp.mozilla.org/pub/mozilla/releases/.

一旦你下载了SDK, 你可以解压缩到任何合适的目录. 在本附录中, 我们建立Windows Gecko SDK 到 c:\gecko-sdk\. 如果你选择其他的位置, 记得调整这里描述的设置指向这个位置(e.g., in the {{template.Anch("Building a Microsoft Visual Cpp Project")}} section below) .

当你解压缩SDK,它的目录结构看起来应该是:

{{wiki.template('Block-title', [ "Layout of the Extracted SDK" ])}}

Image:sdk-layout.png

The directories represent different modules in the SDK. For example, the headers for networking are all located in the necko directory, and the headers that XPCOM requires are in the XPCOM directory. This directory structure makes build scripts slightly more complicated (since there will be many different include paths), but it helps to organize the parts of the SDK meaningfully.

The two top level header files are special. The file mozilla-config.h lists all of the defines used in the SDK. Including this header file in your project ensures that the component you create uses the same defines as the Gecko libraries themselves. Note that mozilla-config.h may be need to be included before other includes in your component's source code.

Each module directory is divided into three subdirectories:

{{wiki.template('Block-title', [ "Module Subdirectories" ])}}

Image:module-directory-subdirs.png

The bin directory contains static libraries, dynamic libraries, and in some cases tools that may be useful in development. The idl directory contains the public IDL files exported by the module. The includes directory contains C++ header files used by your component.

XPCOM exports a number of binaries that should be mentioned at this point. The table below refers to the Windows file names for the executables.

Application Name Description of functionality
regxpcom.exe Registers or Unregisters components with XPCOM
xpidl.exe Generates typelib and C++ headers from XPIDL
xpt_dump.exe Prints out information about a given typelib
xpt_link.exe Combines multiple typelibs into a single typelib
Library Name Description of functionality
xpcomglue.lib XPCOM Glue library to be used by xpcom components.

编译一个 Microsoft Visual Cpp 工程

Once you set up the Gecko SDK, you can create a Microsoft Visual C++ project to handle component development with the SDK.

创建一个新的工程

After launching Visual C++, select New from the File menu. Then, from the New dialog, select "Win32 Dynamic-Link Library". Use the fields to the right of the dialog to name your project and set its location. (This example uses "SampleGeckoProject" as the project name and C:\ as its location.).

{{wiki.template('Block-title', [ "New Dialog" ])}}

Image:new-vcpp-project.png

Select OK. In the Win32 Dynamic-Link Library dialog that displays (see below), you can choose the default selection "An Empty DLL Project" as the type of DLL.

Image:vcpp-dll-dialog.png

In this dialog, click Finish. Microsoft Studio creates a new project per your specification and presents you with the standard Project view.

把 Gecko SDK 添加到工程设置

In order to build anything that uses Gecko, you have to further modify the project so that it knows where to find the Gecko SDK on the disk. To edit project settings, select Settings from the Project menu (or press Alt-F7).

Most of the changes you make in the following steps apply to all configurations of the project (both Debug and Optimized), so select "All Configurations" from the Settings For dropdown menu:

Image:vcpp-project-settings.png

On the C/C++ tab, select the Preprocessor category. This window is where you add the include paths to the Gecko SDK as well as two preprocessor defines:

  • XPCOM_GLUE
  • MOZILLA_STRICT_API

At a minimum, you must include the nspr, the embedstring and string include directories, and the xpcom include subdirectory. If your component will use other parts of the SDK (e.g., necko), you will have to add these include directories to this field as well.

Assuming you are using the example location for your project, these paths are the following:

  • c:\gecko-sdk\embedstring\include
  • c:\gecko-sdk\xpcom\include
  • c:\gecko-sdk\nspr\include
  • c:\gecko-sdk\string\include

Image:vcpp-project-settings-includes.png

Under the C++ language category, disable exception handling. As described in the section Exceptions in XPCOM, exception handling isn't supported across interface boundaries, so setting this option may catch problems during development.

The WebLock component needs to link against the appropriate libraries to uses XPCOM Glue. To add these libraries, select the Link tab, then choose the Input category. In this panel, instead of linking to the include subdirectories of the nspr, embedstring, and xpcom directories, add the paths to the bin subdirectories.

We also link against a number of libraries in the Object/library modules line:

  • nspr4.lib
  • plds4.lib
  • plc4.lib
  • embedstring.lib
  • xpcomglue.lib

Both of these settings are shown below:

Image:vcpp-project-settings.png

The last change you need to make to set up the Gecko SDK in your project is to change the "Use run-time library" setting to "Multithreaded DLL." Since this change is configuration dependent, you must make set the Release configuration run-time library to the release multithreaded DLL runtime and the Debug configuration to the debug multithreaded dll runtime (this needs clarification):

Image:vcpp-runtime-settings.png

After making these changes, press OK. This finalizes the project settings and gives you a project that will hold and compile XPCOM Components.

Unix 下的一个 Makefile

Linux 下不采用工程而采用 Makefile 来组织代码. Makefile 中放置编译环境中的编译选项, 包括使用 Gecko SDK 编译的路径和配置更新等.

下面是一个使用 SDK 来编译的 Makefile, 这里对 Makefile 的详细用法不做解释. 它与 Visual C++ 的工程({{template.Anch("Building a Microsoft Visual Cpp Project")}})相类似, 关于 Makefile 的命令请参看 Make 手册.

{{wiki.template('Block-title', [ "Gecko SDK 下的一个 Makefile 例子" ])}}

CXX   = c++

CPPFLAGS +=     -fno-rtti              \
                -fno-exceptions        \
                -shared

# Change this to point at your Gecko SDK directory.
GECKO_SDK_PATH = /home/dougt/gecko-sdk

# GCC only define which allows us to not have to #include mozilla-config
# in every .cpp file.  If your not using GCC remove this line and add
# #include "mozilla-config.h" to each of your .cpp files.
GECKO_CONFIG_INCLUDE = -include mozilla-config.h

GECKO_DEFINES  = -DXPCOM_GLUE -DMOZILLA_STRICT_API

GECKO_INCLUDES = -I $(GECKO_SDK_PATH)                    \
                 -I $(GECKO_SDK_PATH)/xpcom/include      \
                 -I $(GECKO_SDK_PATH)/nspr/include       \
                 -I $(GECKO_SDK_PATH)/string/include     \
                 -I $(GECKO_SDK_PATH)/embedstring/include

GECKO_LDFLAGS =  -L $(GECKO_SDK_PATH)/xpcom/bin -lxpcomglue \
                 -L $(GECKO_SDK_PATH)/nspr/bin -lnspr4      \
                 -L $(GECKO_SDK_PATH)/nspr/bin -lplds4      \
                 -L $(GECKO_SDK_PATH)/embedstring/bin/ -lembedstring

build:
        $(CXX) -o MozShim.so $(GECKO_CONFIG_INCLUDE) $(GECKO_DEFINES) $(GECKO_INCLUDES) $(GECK\
O_LDFLAGS) $(CPPFLAGS) $(CXXFLAGS) MozShim.cpp
        chmod +x MozShim.so

clean:
        rm MozShim.so

{{template.PreviousNext("创建 XPCOM 组件:打包 WebLock", "创建 XPCOM 组件:资源")}} {{template.CXCLicenseBlock()}}

修订版来源

<p>
{{template.PreviousNext("创建 XPCOM 组件:打包 WebLock", "创建 XPCOM 组件:资源")}}
</p><p>这一章提供建立 Gecko SDK 的基本方法, 下面会告诉开发人员如何下载和组织 Gecko SDK, 如何象 <i>WebLock</i> 一样创建一个新的组件工程. 
</p>
<h3 name=".E4.B8.8B.E8.BD.BD.E5.92.8C.E5.BB.BA.E7.AB.8B_SDK"> 下载和建立 SDK </h3>
<p>Gecko SDK 提供了编译 XPCOM 组件所需要的所有的工具, 头文件和库. SDK 现在有 Windows 和 Linux 两个版本, 其他操作系统上的 SDK 正在开发. SDK 在下面的地址下载:
</p>
<ul><li> Linux: http://ftp.mozilla.org/pub/mozilla/releases/mozilla1.4a/gecko-sdk-i686-pc-linux-gnu-1.4a.tar.gz
</li><li> Windows: http://ftp.mozilla.org/pub/mozilla/releases/mozilla1.4a/gecko-sdk-win32-1.4a.zip
</li></ul>
<p>注意版号要大于1.4a. 可以在下面的地址获取更新的 SDK 版本 http://ftp.mozilla.org/pub/mozilla/releases/.
</p><p>一旦你下载了SDK, 你可以解压缩到任何合适的目录. 在本附录中, 我们建立Windows Gecko SDK 到 <code>c:\gecko-sdk\</code>. 如果你选择其他的位置, 记得调整这里描述的设置指向这个位置(e.g., in the {{template.Anch("Building a Microsoft Visual Cpp Project")}} section below) .
</p><p>当你解压缩SDK,它的目录结构看起来应该是:
</p><p>{{wiki.template('Block-title', [ "Layout of the Extracted SDK" ])}}
</p><p><img alt="Image:sdk-layout.png" src="File:cn/Media_Gallery/Sdk-layout.png">
</p><p>The directories represent different modules in the SDK. For example, the headers for networking are all located in the <code>necko</code> directory, and the headers that XPCOM requires are in the XPCOM directory. This directory structure makes build scripts slightly more complicated (since there will be many different include paths), but it helps to organize the parts of the SDK meaningfully.
</p><p>The two top level header files are special. The file <code>mozilla-config.h</code> lists all of the defines used in the SDK. Including this header file in your project ensures that the component you create uses the same defines as the Gecko libraries themselves. Note that <code>mozilla-config.h</code> may be need to be included before other includes in your component's source code.
</p><p>Each module directory is divided into three subdirectories:
</p><p>{{wiki.template('Block-title', [ "Module Subdirectories" ])}}
</p><p><img alt="Image:module-directory-subdirs.png" src="File:cn/Media_Gallery/Module-directory-subdirs.png">
</p><p>The <code>bin</code> directory contains static libraries, dynamic libraries, and in some cases tools that may be useful in development. The <code>idl</code> directory contains the public IDL files exported by the module. The <code>includes</code> directory contains C++ header files used by your component.
</p><p>XPCOM exports a number of binaries that should be mentioned at this point. The table below refers to the Windows file names for the executables.
</p>
<table class="standard-table">
  <tbody><tr>
    <td class="header">Application Name</td>
    <td class="header">Description of functionality</td>
  </tr>
  <tr>
    <td><code>regxpcom.exe</code></td>
    <td>Registers or Unregisters components with XPCOM</td>
  </tr>
  <tr>
    <td><code>xpidl.exe</code></td>
    <td>Generates typelib and C++ headers from XPIDL</td>
  </tr>
  <tr>
    <td><code>xpt_dump.exe</code></td>
    <td>Prints out information about a given typelib</td>
  </tr>
  <tr>
    <td><code>xpt_link.exe</code></td>
    <td>Combines multiple typelibs into a single typelib</td>
  </tr>
</tbody></table>
<table class="standard-table">
  <tbody><tr>
    <td class="header">Library Name</td>
    <td class="header">Description of functionality</td>
  </tr>
  <tr>
    <td><code>xpcomglue.lib</code></td>
    <td>XPCOM Glue library to be used by xpcom components.</td>
  </tr>
</tbody></table>
<h3 name=".E7.BC.96.E8.AF.91.E4.B8.80.E4.B8.AA_Microsoft_Visual_Cpp_.E5.B7.A5.E7.A8.8B"> 编译一个 Microsoft Visual Cpp 工程 </h3>
<p>Once you set up the Gecko SDK, you can create a Microsoft Visual C++ project to handle component development with the SDK.
</p>
<h4 name=".E5.88.9B.E5.BB.BA.E4.B8.80.E4.B8.AA.E6.96.B0.E7.9A.84.E5.B7.A5.E7.A8.8B"> 创建一个新的工程 </h4>
<p>After launching Visual C++, select New from the File menu. Then, from the New dialog, select "Win32 Dynamic-Link Library". Use the fields to the right of the dialog to name your project and set its location. (This example uses "SampleGeckoProject" as the project name and <code>C:\</code> as its location.).
</p><p>{{wiki.template('Block-title', [ "New Dialog" ])}}
</p><p><img alt="Image:new-vcpp-project.png" src="File:cn/Media_Gallery/New-vcpp-project.png">
</p><p>Select OK. In the Win32 Dynamic-Link Library dialog that displays (see below), you can choose the default selection "An Empty DLL Project" as the type of DLL.
</p><p><img alt="Image:vcpp-dll-dialog.png" src="File:cn/Media_Gallery/Vcpp-dll-dialog.png">
</p><p>In this dialog, click Finish. Microsoft Studio creates a new project per your specification and presents you with the standard Project view.
</p>
<h4 name=".E6.8A.8A_Gecko_SDK_.E6.B7.BB.E5.8A.A0.E5.88.B0.E5.B7.A5.E7.A8.8B.E8.AE.BE.E7.BD.AE"> 把 Gecko SDK 添加到工程设置 </h4>
<p>In order to build anything that uses Gecko, you have to further modify the project so that it knows where to find the Gecko SDK on the disk. To edit project settings, select Settings from the Project menu (or press Alt-F7).
</p><p>Most of the changes you make in the following steps apply to all configurations of the project (both Debug and Optimized), so select "All Configurations" from the Settings For dropdown menu:
</p><p><img alt="Image:vcpp-project-settings.png" src="File:cn/Media_Gallery/Vcpp-project-settings.png">
</p><p>On the C/C++ tab, select the Preprocessor category. This window is where you add the include paths to the Gecko SDK as well as two preprocessor defines:
</p>
<ul><li> <code>XPCOM_GLUE</code>
</li><li> <code>MOZILLA_STRICT_API</code>
</li></ul>
<p>At a minimum, you must include the <code>nspr</code>, the <code>embedstring</code> and <code>string</code> <code>include</code> directories, and the <code>xpcom</code> <code>include</code> subdirectory. If your component will use other parts of the SDK (e.g., necko), you will have to add these include directories to this field as well.
</p><p>Assuming you are using the example location for your project, these paths are the following:
</p>
<ul><li> <code>c:\gecko-sdk\embedstring\include</code>
</li><li> <code>c:\gecko-sdk\xpcom\include</code>
</li><li> <code>c:\gecko-sdk\nspr\include</code>
</li><li> <code>c:\gecko-sdk\string\include</code>
</li></ul>
<p><img alt="Image:vcpp-project-settings-includes.png" src="File:cn/Media_Gallery/Vcpp-project-settings-includes.png">
</p><p>Under the C++ language category, disable exception handling. As described in the section <a href="cn/An_Overview_of_XPCOM/Exceptions_in_XPCOM">Exceptions in XPCOM</a>, exception handling isn't supported across interface boundaries, so setting this option may catch problems during development.
</p><p>The <b>WebLock</b> component needs to link against the appropriate libraries to uses XPCOM Glue. To add these libraries, select the Link tab, then choose the Input category. In this panel, instead of linking to the <code>include</code> subdirectories of the <code>nspr</code>, <code>embedstring</code>, and <code>xpcom</code> directories, add the paths to the <code>bin</code> subdirectories.
</p><p>We also link against a number of libraries in the Object/library modules line:
</p>
<ul><li> <code>nspr4.lib</code>
</li><li> <code>plds4.lib</code>
</li><li> <code>plc4.lib</code>
</li><li> <code>embedstring.lib</code>
</li><li> <code>xpcomglue.lib</code>
</li></ul>
<p>Both of these settings are shown below:
</p><p><img alt="Image:vcpp-project-settings.png" src="File:cn/Media_Gallery/Vcpp-project-settings.png">
</p><p>The last change you need to make to set up the Gecko SDK in your project is to change the "Use run-time library" setting to "Multithreaded DLL." Since this change is configuration dependent, you <b>must make set the Release configuration run-time library to the release multithreaded DLL runtime and the Debug configuration to the debug multithreaded dll runtime</b> (this needs clarification):
</p><p><img alt="Image:vcpp-runtime-settings.png" src="File:cn/Media_Gallery/Vcpp-runtime-settings.png">
</p><p>After making these changes, press OK. This finalizes the project settings and gives you a project that will hold and compile XPCOM Components.
</p>
<h3 name="Unix_.E4.B8.8B.E7.9A.84.E4.B8.80.E4.B8.AA_Makefile"> Unix 下的一个 Makefile </h3>
<p>Linux 下不采用工程而采用 <code>Makefile</code> 来组织代码. <code>Makefile</code> 中放置编译环境中的编译选项, 包括使用 Gecko SDK 编译的路径和配置更新等. 
</p><p>下面是一个使用 SDK 来编译的 <code>Makefile</code>, 这里对 <code>Makefile</code> 的详细用法不做解释. 它与 Visual C++ 的工程({{template.Anch("Building a Microsoft Visual Cpp Project")}})相类似, 关于 Makefile 的命令请参看 <a class="external" href="http://www.gnu.org/manual/make/">Make 手册</a>.
</p><p>{{wiki.template('Block-title', [ "Gecko SDK 下的一个 Makefile 例子" ])}}
</p>
<pre>CXX   = c++

CPPFLAGS +=     -fno-rtti              \
                -fno-exceptions        \
                -shared

# Change this to point at your Gecko SDK directory.
GECKO_SDK_PATH = /home/dougt/gecko-sdk

# GCC only define which allows us to not have to #include mozilla-config
# in every .cpp file.  If your not using GCC remove this line and add
# #include "mozilla-config.h" to each of your .cpp files.
GECKO_CONFIG_INCLUDE = -include mozilla-config.h

GECKO_DEFINES  = -DXPCOM_GLUE -DMOZILLA_STRICT_API

GECKO_INCLUDES = -I $(GECKO_SDK_PATH)                    \
                 -I $(GECKO_SDK_PATH)/xpcom/include      \
                 -I $(GECKO_SDK_PATH)/nspr/include       \
                 -I $(GECKO_SDK_PATH)/string/include     \
                 -I $(GECKO_SDK_PATH)/embedstring/include

GECKO_LDFLAGS =  -L $(GECKO_SDK_PATH)/xpcom/bin -lxpcomglue \
                 -L $(GECKO_SDK_PATH)/nspr/bin -lnspr4      \
                 -L $(GECKO_SDK_PATH)/nspr/bin -lplds4      \
                 -L $(GECKO_SDK_PATH)/embedstring/bin/ -lembedstring

build:
        $(CXX) -o MozShim.so $(GECKO_CONFIG_INCLUDE) $(GECKO_DEFINES) $(GECKO_INCLUDES) $(GECK\
O_LDFLAGS) $(CPPFLAGS) $(CXXFLAGS) MozShim.cpp
        chmod +x MozShim.so

clean:
        rm MozShim.so
</pre>
<p>{{template.PreviousNext("创建 XPCOM 组件:打包 WebLock", "创建 XPCOM 组件:资源")}}
{{template.CXCLicenseBlock()}}
</p>
恢复到这个版本