aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/mozilla/tech/xpcom/glue/index.html
blob: 58bfdc731e5827ccd8ea58f00b058ebf09b6f9c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
---
title: XPCOM Glue
slug: Mozilla/Tech/XPCOM/Glue
tags:
  - XPCOM
  - 所有分类
translation_of: Mozilla/Tech/XPCOM/Glue
---
<p>The XPCOM Glue is a static library which component developers and embedders can link against. It allows developers to link only against the frozen XPCOM method symbols and maintain compatibility with multiple versions of XPCOM.</p>
<h3 id="Compiling_or_linking_against_XPCOM_headers" name="Compiling_or_linking_against_XPCOM_headers">Compiling or linking against XPCOM headers</h3>
<p>There are three ways to compile/link against XPCOM headers/libraries:</p>
<h4 id="Frozen_linkage:_dependent_glue_.28dependent_on_xpcom.dll.29" name="Frozen_linkage:_dependent_glue_.28dependent_on_xpcom.dll.29">Frozen linkage: dependent glue (dependent on xpcom.dll)</h4>
<p>Code which wishes to use only frozen symbols but can tolerate a load-time dependency on xpcom.dll should link against xpcom.lib and xpcomglue_s.lib. This is the case for XPCOM components.</p>
<h4 id="Frozen_linkage:_standalone_glue_.28no_DLL_dependencies.29" name="Frozen_linkage:_standalone_glue_.28no_DLL_dependencies.29">Frozen linkage: standalone glue (no DLL dependencies)</h4>
<p>Embedding code which wishes to use only frozen symbols and cannot tolerate a load-time dependency on &lt;tt&gt;xpcom.dll&lt;/tt&gt; should &lt;tt&gt;#define XPCOM_GLUE 1&lt;/tt&gt; while compiling, and link against &lt;tt&gt;xpcomglue.lib&lt;/tt&gt;. It should
 <i>
  not</i>
 link against &lt;tt&gt;xpcomglue_s.lib&lt;/tt&gt; or &lt;tt&gt;xpcom.lib&lt;/tt&gt;.</p>
<p>In order to use XPCOM, the embedding application first needs to find where the XPCOM runtime is located. This is typically done using <a href="cn/GRE_GetGREPathWithProperties">GRE_GetGREPathWithProperties</a>. Then, the code must call <a href="cn/XPCOMGlueStartup">XPCOMGlueStartup</a>, which will dynamically link against the XPCOM runtime. Only then can the embedding application initialize and use XPCOM.</p>
<p>This linkage strategy is used when an embedder needs to bootstrap an embedding app by finding a compatible <a href="cn/GRE">GRE</a>. Extension or XULRunner application components should use the dependent glue.</p>
<p>Embedders using the standalone glue typically also need to avoid linking against NSPR as well.</p>
<p>Emlak ilanlar</p>
<h4 id="Using_Mozilla_internal_linkage" name="Using_Mozilla_internal_linkage">Using Mozilla internal linkage</h4>
<p>Mozilla internal code defines MOZILLA_INTERNAL_API while compiling and links against xpcom.lib and xpcom_core.lib. In almost all cases embedders should *not* use internal linkage. Components using internal linkage will have shared-library dependencies against non-frozen symbols in the XPCOM libraries, and will not work with any other versions of XPCOM other than the one it was compiled against.</p>
<p>Internal linkage will be unavailable to extension authors in XULRunner 1.9 (Firefox 3) because the nonfrozen symbols will not be exported from libxul. Extension and application authors currently using internal linkage should read the guide on <a href="cn/Migrating_from_Internal_Linkage_to_Frozen_Linkage">Migrating from Internal Linkage to Frozen Linkage</a>.</p>
<h3 id="Threadsafe_nsISupports_implementations" name="Threadsafe_nsISupports_implementations">Threadsafe nsISupports implementations</h3>
<p>When using glue libraries from Gecko 1.8 or later, a special step needs to be taken to use <code>NS_IMPL_THREADSAFE_ISUPPORTS
 <i>
  n</i>
 </code>. This is because it forces a dependency on the NSPR library, which can otherwise be avoided. As described in <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=299664" title="FIXED: add support for MOZ_USE_NSPR w/ XPCOM_GLUE">bug 299664</a>, the preprocessor symbol <code>XPCOM_GLUE_USE_NSPR</code> needs to be defined.</p>
<h3 id="Sample_Compiler.2FLinker_Flags" name="Sample_Compiler.2FLinker_Flags">Sample Compiler/Linker Flags</h3>
<p>Code compiled using XPCOM headers should always &lt;tt&gt;#include "xpcom-config.h"&lt;/tt&gt; from the SDK, to ensure that XPCOM #defines are correct.</p>
<table class="fullwidth-table">
 <tbody>
  <tr>
   <th rowspan="2">Linking Strategy</th>
   <th rowspan="2">Compiler Flags</th>
   <th colspan="3">Linker Flags</th>
  </tr>
  <tr>
   <th>Windows</th>
   <th>Mac</th>
   <th>Linux</th>
  </tr>
  <tr>
   <td>Dependent Glue</td>
   <td>&lt;tt style="white-space: pre"&gt;#include "xpcom-config.h"&lt;/tt&gt;</td>
   <td>&lt;tt&gt;-LIBPATH:<var>c:/path/to/sdk/lib</var> xpcomglue_s.lib xpcom.lib nspr4.lib&lt;/tt&gt;</td>
   <td>&lt;tt&gt;-L<var>/path/to/sdk/lib</var> -L<var>/path/to/sdk/bin</var> -Wl,-executable-path,<var>/path/to/sdk/bin</var> -lxpcomglue_s -lxpcom -lnspr4&lt;/tt&gt;</td>
   <td>&lt;tt&gt;-L<var>/path/to/sdk/lib</var> -L<var>/path/to/sdk/bin</var> -Wl,-rpath-link,<var>/path/to/sdk/bin</var> -lxpcomglue_s -lxpcom -lnspr4&lt;/tt&gt;</td>
  </tr>
  <tr>
   <td>Standalone Glue</td>
   <td>&lt;tt style="white-space: pre"&gt;#include "xpcom-config.h"<br>
    #define XPCOM_GLUE 1&lt;/tt&gt;</td>
   <td>&lt;tt&gt;-LIBPATH:<var>c:/path/to/sdk/lib</var> xpcomglue.lib&lt;/tt&gt;</td>
   <td>&lt;tt&gt;-L<var>/path/to/sdk/lib</var> -lxpcomglue&lt;/tt&gt;</td>
   <td>&lt;tt&gt;-L<var>/path/to/sdk/lib</var> -lxpcomglue&lt;/tt&gt;</td>
  </tr>
 </tbody>
</table>
<h4 id="Notes" name="Notes">Notes</h4>
<ul>
 <li>Never link against xpcomglue.lib and xpcomglue_s.lib at the same time.</li>
 <li>Never link against xpcomglue.lib and xpcom.lib at the same time.</li>
 <li>These instructions are for Mozilla 1.8 and above. When using a SDK from Mozilla 1.7 or earlier, you must define MOZILLA_STRICT_API when using any form of the glue.</li>
</ul>
<h3 id="See_Also" name="See_Also">See Also</h3>
<ul>
 <li><a href="cn/Using_the_Gecko_SDK">Using the Gecko SDK</a></li>
</ul>