back to <a
<h1>Dynamically loading SpatiaLite as an <u><i>extension
<h3>Theoretical foundations</h3>
Speacking in rigorous technical terms, SpatiaLite simply is an extension to SQLite.<br>
Consequently we are strictly required to deploy some appropriate mechanism allowing to load both binary libraries (<i>libsqlite3</i> and <i>libspatialite</i>) then properly registering within SQLite3 the many <b><i>extended Spatial SQL implemented by SpatiaLite.<br>
A further compication should be taken in proper debt; <i>libspatialite</i> isn't a simple stand-alone self-contained library and depends on several further libraries (<i>libgeos</i>, <i>libproj</i>, <i>libxml2</i> and so on).<br><br>
There are several possible alternative paths leading to this final goal:
<ul>
<li>using <b><i>static a straighforward solution avoiding many subsequent headhaches related to installation and run-time configuration.<br>
Following this approach a single monolithic executable internally containing all required binary code will be created at build time; this monolithic executable will have no external dependencies at all, thus radically simplifying any installation task.<br>
Such strategy nicely works in the case of stand-alone self-contained apps (e.g. both <b>spatialite.exe</b> and <b>spatialite_gui.exe</b> Windows binaries are usually built this way).<br>
Unhappily this elementary simple solution doesn't fit well in Linux and Unix-like environments; and even worst often directly conflicts with the internal architecture supported by many language connectors or by other complex frameworks (as e.g. is
<li>using <b><i>shared libraries<i></b> (aka on Windows).<br>
This second approach will carefully avoid to link once for all a monolithic executable; at the opposite, each single code component will be shipped as an autonomous individual member, and only at run-time all components will be finally bound together.<br>
The good new is that this way each library/component will be (hopefully) installed only once on your system, and that every executable depending on that library will load exactly the same binary code, thus granting a robust overall consistency.<br>
The bad new is in that if a single required component couldn't be loaded at run-time (for any possible reason) the executable as a whole will fail to run.<br>
To make things even worst, this schema works nicely on Linux, Unix and Mac OS X; but on Windows it's plagued by many serious issues (the infamous <b><i>DLL
<li>using real <b><i>dynamic aka <b><i>late (which hasn't to be confused with simply using shared libraries).<br>
Following this third approach an extension module could be selectively loaded or not and the actual action will be activated only at run-time.
If for any reason the main executable fails to load an eventual extension module, it will still continue to work by simply ingoring every optional feature requiring the failing module.<br>
As a direct consequence, an executable built this way can theoretically growth to the infinite, and can practically ignore any specific detail about the optional features supported by its extensions; this corresponds to the so called architecture.<br>
Happily enough SQLite3 fully supports this more advanced mechanism; and this option is widely supported by many language connectors.</li>
</li>
</ul><br>
<h3>How it works</h3>
<i>libsqlite3</i> has the capability to dynamically load extension modules. This feature is available as a C-language API, but is avaibles as a <i>special</i> SQL function as well:
<verbatim>
SELECT load_extension('mod_spatialite');
</verbatim>
This SQL function make elementary simple (and language independent) the problem of loading any possible extension; you are simply expected to execute a rather trivial SQL statement in order to load an external extension when it's actually required.<br><br>
Loadable extension anathomy:
<ul>
<li>any SQLite extension module is expected to declare a conventional interface.<br>
Except for this, any SQLite's own extension simply is an ordinary shared library or DLL as any other.</li>
<li>a real extension module will never explicitly depend on SQLite3, because the following actions will be automatically taken by SQLite3 when loading:
<ul>
<li>all SQL fuctions defined/supported by the extension will be added to the standard list; and eventually an extension could ever overload any pre-defined SQL function directly defined by SQLite3.</li>
<li>that's not all; SQLite3 will implicitly pass to the extension a <i><u>mirror of its own APIs: this will ensure that exactly the same code will support both the SQLite's own core and the extension, and will effectively avoid any possible conflict between them.</li>

</ul>
<br>
Common failure causes:
<ul>
<li>if the expected conventional interface isn't correctly declared by the shared library being loaded, then SQLite3 will simply assume that it's not a valid extension module.<br>
And consequently will refuse to load the module without any other consequence (except may be printing some appropriate error message).</li>
<li>a more obvious failure cause could be the one to attempt loading a not existing module (or possibly, a module whom actual address couldn't be resolved by applying the standard system-dependent seach rules).</li>
<li>finally, the module could effectively exists and could correctly declare a valid conventional interface. Anyway, if the corresponding shared library has further dependecies some broken link could possibly forbid a succesful loading.</li>
<li>the last hill-fated combination: SQLite3 is a strongly configurable component. If for any highly opinable choice your system packages had decided to disable at all the extension module mechamism you'll then never be able to load any extension. Full stop. (anyway you'll probably be able to switch to some more reasoble / less paranoid distribution).
</ul>
<h3>What's new in
You'll probably be already accustomed to load SpatiaLite as a dynamic extension; but any previous version before <b>4.2</b> made no real distinction between loadable modules and general-purpose shared libraries.<br>
As we painfully learned by direct on-the-field experience, this apparently simpler configuration caused lot of troubles: instabilities, sudden crashes and so on. Making a clearer distinction between a <u><i>general-purpose shared and a <u><i>pure loadable seems to be the definitive solution.<br>
Exactly here is the radical innovation introduce starting since version <b>4.2</b>; now SpatiaLite is distributed in two alternative flavors:
<ul>
and so on): a genuine classic shared library.<br>
It will always depend on some external <i>libsqlite3</i>, and is uniquely intended to be used by stand-alone applications (such as e.g. or
You'll never be able to succesfully load this shared library as a dynamic extension via the <b>SELECT load_extension</b> mechanism, simply because it doesn't declare the required conventional interface.</li>
and so on): this is simply intended as pure loadable module lacking any explicit SQLite3 dependency.<br>
You never be able to directly link this shared library following the classic way, because it doesn't declare any external link symbol except that the conventional interface.
The unique possible way to load and activate this module is by calling a <b>SELECT load_extension</b> SQL statement.</li>
</ul>
<hr><br>
back to <a