XPCOM Glue

  • Revision slug: XPCOM_Glue
  • Revision title: XPCOM Glue
  • Revision id: 83087
  • Created:
  • Creator: Benjamin Smedberg
  • Is current revision? No
  • Comment Internal linkage: hook up new page on migration, and note changes in 1.9 cycle.

Revision Content

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.

Compiling or linking against XPCOM headers

There are three ways to compile/link against XPCOM headers/libraries:

Frozen linkage: dependent glue (dependent on xpcom.dll)

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.

Frozen linkage: standalone glue (no DLL dependencies)

Embedding code which wishes to use only frozen symbols and cannot tolerate a load-time dependency on <tt>xpcom.dll</tt> should <tt>#define XPCOM_GLUE</tt> while compiling, and link against <tt>xpcomglue.lib</tt>. It should not link against <tt>xpcomglue_s.lib</tt> or <tt>xpcom.lib</tt>.

In order to use XPCOM, the embedding application first needs to find where the XPCOM runtime is located. This is typically done using GRE_GetGREPathWithProperties. Then, the code must call XPCOMGlueStartup, which will dynamically link against the XPCOM runtime. Only then can the embedding application initialize and use XPCOM.

This linkage strategy is used when an embedder needs to bootstrap an embedding app by finding a compatible GRE. Extension or XULRunner application components should use the dependent glue.

Embedders using the standalone glue typically also need to avoid linking against NSPR as well.

Using Mozilla internal linkage

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.

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 Migrating from Internal Linkage to Frozen Linkage.

Threadsafe nsISupports implementations

When using glue libraries from Gecko 1.8 or later, a special step needs to be taken to use NS_IMPL_THREADSAFE_ISUPPORTSn. This is because it forces a dependency on the NSPR library, which can otherwise be avoided. As described in {{template.Bug(299664)}}, the preprocessor symbol XPCOM_GLUE_USE_NSPR needs to be defined.

Sample Compiler/Linker Flags

Code compiled using XPCOM headers should always <tt>#include "xpcom-config.h"</tt> from the SDK, to ensure that XPCOM #defines are correct.

Linking Strategy Compiler Flags Linker Flags
Windows Mac Linux
Dependent Glue <tt style="white-space: pre">#include "xpcom-config.h"</tt> <tt>-LIBPATH:c:/path/to/sdk/lib xpcomglue_s.lib xpcom.lib nspr4.lib</tt> <tt>-L/path/to/sdk/lib -L/path/to/sdk/bin -Wl,-executable-path,/path/to/sdk/bin -lxpcomglue_s -lxpcom -lnspr4</tt> <tt>-L/path/to/sdk/lib -L/path/to/sdk/bin -Wl,-rpath-link,/path/to/sdk/bin -lxpcomglue_s -lxpcom -lnspr4</tt>
Standalone Glue <tt style="white-space: pre">#include "xpcom-config.h"
#define XPCOM_GLUE</tt>
<tt>-LIBPATH:c:/path/to/sdk/lib xpcomglue.lib</tt> <tt>-L/path/to/sdk/lib -lxpcomglue</tt> <tt>-L/path/to/sdk/lib -lxpcomglue</tt>

Notes

  • Never link against xpcomglue.lib and xpcomglue_s.lib at the same time.
  • Never link against xpcomglue.lib and xpcom.lib at the same time.
  • 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.

See Also

Revision Source

<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 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 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 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 <tt>xpcom.dll</tt> should <tt>#define XPCOM_GLUE</tt> while compiling, and link against <tt>xpcomglue.lib</tt>. It should <i>not</i> link against <tt>xpcomglue_s.lib</tt> or <tt>xpcom.lib</tt>.
</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="en/GRE_GetGREPathWithProperties">GRE_GetGREPathWithProperties</a>. Then, the code must call <a href="en/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="en/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>
<h4 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="en/Migrating_from_Internal_Linkage_to_Frozen_Linkage">Migrating from Internal Linkage to Frozen Linkage</a>.
</p>
<h3 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 {{template.Bug(299664)}}, the preprocessor symbol <code>XPCOM_GLUE_USE_NSPR</code> needs to be defined.
</p>
<h3 name="Sample_Compiler.2FLinker_Flags"> Sample Compiler/Linker Flags </h3>
<p>Code compiled using XPCOM headers should always <tt>#include "xpcom-config.h"</tt> 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><tt style="white-space: pre">#include "xpcom-config.h"</tt>
  </td><td><tt>-LIBPATH:<var>c:/path/to/sdk/lib</var> xpcomglue_s.lib xpcom.lib nspr4.lib</tt>
  </td><td><tt>-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</tt>
  </td><td><tt>-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</tt>
</td></tr><tr>
  <td>Standalone Glue
  </td><td><tt style="white-space: pre">#include "xpcom-config.h"<br>#define XPCOM_GLUE</tt>
  </td><td><tt>-LIBPATH:<var>c:/path/to/sdk/lib</var> xpcomglue.lib</tt>
  </td><td><tt>-L<var>/path/to/sdk/lib</var> -lxpcomglue</tt>
  </td><td><tt>-L<var>/path/to/sdk/lib</var> -lxpcomglue</tt>
</td></tr></tbody></table>
<h4 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 name="See_Also"> See Also </h3>
<ul><li><a href="en/Using_the_Gecko_SDK">Using the Gecko SDK</a>
</li></ul>
Revert to this revision