windows static link of mod_spatialite

56 views
Skip to first unread message

Bojan Vukasovic

unread,
Dec 2, 2021, 4:36:56 AM12/2/21
to SpatiaLite Users
I managed to follow tutorial from the website, and managed to compile mod_spatialite.dll. What I wanted to have is to make spatialite include all other libraries (.dlls) that are currently necessary in single file. I saw your instructions related to `Makefile-static-mingw64`, but seems I cannot make it work.

This is what I tried:

gcc -static versioninfo/.libs/mod_spatialite_la-version.o  -Wl,--whole-archive -L/mingw64/local/lib -L./../.. -L=C:/msys64/mingw64/bin/../lib -L==C:/msys64/mingw64/bin/../lib -LC:/msys64/mingw64/bin/../lib ./s.o ./gaiaaux/.libs/gaiaaux.a ./gaiaexif/.libs/gaiaexif.a ./gaiageo/.libs/gaiageo.a ./geopackage/.libs/geopackage.a ./spatialite/.libs/splite.a ./shapefiles/.libs/shapefiles.a ./dxf/.libs/dxf.a ./md5/.libs/md5.a ./control_points/.libs/control_points.a ./cutter/.libs/cutter.a ./topology/.libs/topology.a ./srsinit/.libs/srsinit.a ./stored_procedures/.libs/stored_procedures.a ./connection_cache/.libs/connection_cache.a ./virtualtext/.libs/virtualtext.a ./wfs/.libs/wfs.a /mingw64/local/lib/libexpat.dll.a -Wl,--no-whole-archive  -L/mingw64/local/lib -L./../.. -L=C:/msys64/mingw64/bin/../lib -L==C:/msys64/mingw64/bin/../lib -ltiff /mingw64/local/lib/libcurl.dll.a -lm -lpthread -lcrypt32 -lssl -lcrypto -lgdi32 -lwldap32 -lzstd -lws2_32 /mingw64/local/lib/libgeos.dll.a -LC:/msys64/mingw64/bin/../lib -lxml2 /mingw64/local/lib/libminizip.dll.a /mingw64/local/lib/librttopo.dll.a /mingw64/local/lib/libfreexl.dll.a /mingw64/local/lib/libiconv.dll.a /mingw64/local/lib/libproj.dll.a /mingw64/local/lib/libz.dll.a -lsqlite3 /mingw64/local/lib/libgeos_c.dll.a  -O2   -o static_bin/mod_spatialite.dll

and what I get:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: ./connection_cache/.libs/connection_cache.a(connection_cache_la-alloc_cache.o): in function `splite_cache_semaphore_lock':
C:\Users\default.DESKTOP-NFJ032S\Downloads\libspatialite-5.0.1\src\connection_cache/alloc_cache.c:1268: undefined reference to `__imp_pthread_mutex_lock'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: ./connection_cache/.libs/connection_cache.a(connection_cache_la-alloc_cache.o): in function `splite_cache_semaphore_unlock':
C:\Users\default.DESKTOP-NFJ032S\Downloads\libspatialite-5.0.1\src\connection_cache/alloc_cache.c:1278: undefined reference to `__imp_pthread_mutex_trylock'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\default.DESKTOP-NFJ032S\Downloads\libspatialite-5.0.1\src\connection_cache/alloc_cache.c:1279: undefined reference to `__imp_pthread_mutex_unlock'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib\libxml2.a(nanohttp.o):nanohttp.c:(.text+0xfa): undefined reference to `__imp_closesocket'
...

Do you know possibly what could be the problem here?

Brad Hards

unread,
Dec 2, 2021, 4:51:28 AM12/2/21
to SpatiaLite Users, Bojan Vukasovic
On Thursday, 2 December 2021 8:36:56 PM AEDT Bojan Vukasovic wrote:
> C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_6
> 4-w64-mingw32/bin/ld.exe:
> ./connection_cache/.libs/connection_cache.a(connection_cache_la-alloc_cache
> .o): in function `splite_cache_semaphore_lock':
> C:\Users\default.DESKTOP-NFJ032S\Downloads\libspatialite-5.0.1\src\connectio
> n_cache/alloc_cache.c:1268: undefined reference to
> `__imp_pthread_mutex_lock'
Perhaps you wanted to add the pthread dependency to the build?

Possibly some other dependencies are missing, but it should be obvious how to
extend the build once you get past this error.

Brad


Bojan Vukasovic

unread,
Dec 2, 2021, 5:03:45 AM12/2/21
to SpatiaLite Users
I think I added it by using " -lpthread" ?

Original code (that was working) had "-shared" instead of mine "-static" (since I guess I want to include libs in this one output file). And also, I moved "libexpat.dll.a" before "--no-whole-archive" - just to try if it would include this one inside the library.

Bojan Vukasovic

unread,
Dec 2, 2021, 6:09:08 AM12/2/21
to SpatiaLite Users
I managed to do smth with this command:

gcc versioninfo/.libs/mod_spatialite_la-version.o  -Wl,--allow-multiple-definition,--whole-archive -L/mingw64/local/lib -L./../.. -L=C:/msys64/mingw64/bin/../lib -L==C:/msys64/mingw64/bin/../lib -LC:/msys64/mingw64/bin/../lib ./s.o ./gaiaaux/.libs/gaiaaux.a ./gaiaexif/.libs/gaiaexif.a ./gaiageo/.libs/gaiageo.a ./geopackage/.libs/geopackage.a ./spatialite/.libs/splite.a ./shapefiles/.libs/shapefiles.a ./dxf/.libs/dxf.a ./md5/.libs/md5.a ./control_points/.libs/control_points.a ./cutter/.libs/cutter.a ./topology/.libs/topology.a ./srsinit/.libs/srsinit.a ./stored_procedures/.libs/stored_procedures.a ./connection_cache/.libs/connection_cache.a ./virtualtext/.libs/virtualtext.a ./wfs/.libs/wfs.a /mingw64/local/lib/libexpat.dll.a /mingw64/local/lib/libcurl.dll.a /mingw64/local/lib/libgeos.dll.a /mingw64/local/lib/libminizip.dll.a /mingw64/local/lib/librttopo.dll.a /mingw64/local/lib/libfreexl.dll.a /mingw64/local/lib/libiconv.dll.a /mingw64/local/lib/libproj.dll.a /mingw64/local/lib/libz.dll.a /mingw64/local/lib/libgeos_c.dll.a -Wl,--no-whole-archive  -L/mingw64/local/lib -L./../.. -L=C:/msys64/mingw64/bin/../lib -L==C:/msys64/mingw64/bin/../lib -ltiff -lcrypt32 -lssl -lcrypto -lgdi32 -lwldap32 -lzstd -lws2_32 -LC:/msys64/mingw64/bin/../lib -lxml2 -lsqlite3 -O2   -o static_bin/mod_spatialite.dll

It produces mod_spatialite.dll and it has ~27MB.

$ ldd mod_spatialite.dll
        ntdll.dll => /c/Windows/SYSTEM32/ntdll.dll (0x7ffdadc90000)
        KERNEL32.DLL => /c/Windows/System32/KERNEL32.DLL (0x7ffdad650000)
        KERNELBASE.dll => /c/Windows/System32/KERNELBASE.dll (0x7ffdaa8e0000)

The problem is that it fails to be loaded as an extension.

P.S. in order to compile this, I had to use "./s.o" which has only WinMain inside (otherwise I got complain that it is missing WinMain).

a.fu...@lqt.it

unread,
Dec 2, 2021, 7:40:52 AM12/2/21
to spatiali...@googlegroups.com
On Thu, 2 Dec 2021 03:09:08 -0800 (PST), Bojan Vukasovic wrote:
> I managed to do smth with this command:
>
> ----------- <snip> ------------------
>
> It produces mod_spatialite.dll and it has ~27MB.
>
> The problem is that it fails to be loaded as an extension.
>
> P.S. in order to compile this, I had to use "./s.o" which has only
> WinMain inside (otherwise I got complain that it is missing WinMain).
>

Hi Bojan,

attempting to build a monolithic copy of mod_spatialite (i.e. one
statically linked to all its dependencies is intrinsically bad for
at least two reasons:

1. technically speaking, mod_spatialite is a dynamically loadable
module, that is to say a sw compoment intrinsically designed
for dynamic loading.
introducing static linkage in this overall architecture is
like putting a massive medieval castle on the flimsy roof of
a greenhouse ... not the most elegant solution I can think of

2. (even more serious) I suppose that you intend to deploy
mod_spatialite on Java or may be on Python.
Be warned: using static linkage in such environments is
a sure recipe for disaster, because you are seriously
risking to load at run time conflicting vesions of the
same libraries (one dynamically linked and the other
statically linked), may be compiled by different compilers,
a very dangerous condition leading to frequent instabilities
and unexpected crashes.


> The problem is that it fails to be loaded as an extension.
>
> P.S. in order to compile this, I had to use "./s.o" which has only
> WinMain inside (otherwise I got complain that it is missing WinMain).
>

please note: mod_spatialite isn't a DLL as any other, it's a
dynamically loadable module, that is a completely different
thing. you must absolutely specify the -module option to the
linker in order to get a dynamically loadable module instead
of the most usual DLL

bye Sandro

Bojan Vukasovic

unread,
Dec 2, 2021, 9:44:15 AM12/2/21
to SpatiaLite Users
@Sandro. Thanks for reply. I'm making a Java App that will be compiled to native-image using GraalVM. Inside that single .exe file I would like to have sqlite database with support for spatialite. So far, I can pack inside the dll and extract it to filesystem /Temp folder for loading at the runtime using `load_module`. The problem is that it seems I cannot pack other .dlls and the system where I distribute my java_packed.exe do not already have these files.

So far, the furthest I came to is this:

gcc -static versioninfo/.libs/mod_spatialite_la-version.o  -Wl,--allow-multiple-definition,--whole-archive ./gaiaaux/.libs/gaiaaux.a ./gaiaexif/.libs/gaiaexif.a ./gaiageo/.libs/gaiageo.a ./geopackage/.libs/geopackage.a ./spatialite/.libs/splite.a ./shapefiles/.libs/shapefiles.a ./dxf/.libs/dxf.a ./md5/.libs/md5.a ./control_points/.libs/control_points.a ./cutter/.libs/cutter.a ./topology/.libs/topology.a ./srsinit/.libs/srsinit.a ./stored_procedures/.libs/stored_procedures.a ./connection_cache/.libs/connection_cache.a ./virtualtext/.libs/virtualtext.a ./wfs/.libs/wfs.a /mingw64/x86_64-w64-mingw32/lib/libwinpthread.dll.a /mingw64/x86_64-w64-mingw32/lib/libwsock32.a /mingw64/x86_64-w64-mingw32/lib/libws2_32.a /mingw64/lib/liblzma.dll.a /mingw64/local/lib/libexpat.dll.a /mingw64/local/lib/libcurl.dll.a /mingw64/local/lib/libgeos.dll.a /mingw64/local/lib/libminizip.dll.a /mingw64/local/lib/librttopo.dll.a /mingw64/local/lib/libfreexl.dll.a /mingw64/local/lib/libiconv.dll.a /mingw64/local/lib/libproj.dll.a /mingw64/local/lib/libz.dll.a /mingw64/local/lib/libgeos_c.dll.a -Wl,--no-whole-archive -L/mingw64/local/lib -L./../.. -L=C:/msys64/mingw64/bin/../lib -L=C:/msys64/mingw64/x86_64-w64-mingw32/lib -I/msys64/mingw64/x86_64-w64-mingw32/include -L==C:/msys64/mingw64/bin/../lib -ltiff -lcrypt32 -lssl -lcrypto -lgdi32 -lwldap32 -lzstd -LC:/msys64/mingw64/bin/../lib -lxml2 -lsqlite3 -O2   -o static_bin/mod_spatialite.dll

There is no more pthread error. Now I get this:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: warning: C:/msys64/mingw64/x86_64-w64-mingw32/lib/libwsock32.a(libws2_32t.o): local symbol `data$6' has no section
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: BFD (GNU Binutils) 2.37 assertion fail ../../binutils-2.37/bfd/cofflink.c:2323
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: warning: C:/msys64/mingw64/x86_64-w64-mingw32/lib/libwsock32.a(libws2_32h.o): local symbol `$6' has no section
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: warning: C:/msys64/mingw64/x86_64-w64-mingw32/lib/libwsock32.a(libws2_32h.o): local symbol `(null)' has no section
collect2.exe: error: ld returned 1 exit status

Bojan Vukasovic

unread,
Dec 2, 2021, 9:51:47 AM12/2/21
to SpatiaLite Users
I managed to fix this one using:

gcc -static versioninfo/.libs/mod_spatialite_la-version.o  -Wl,--allow-multiple-definition,--whole-archive ./s.o ./gaiaaux/.libs/gaiaaux.a ./gaiaexif/.libs/gaiaexif.a ./gaiageo/.libs/gaiageo.a ./geopackage/.libs/geopackage.a ./spatialite/.libs/splite.a ./shapefiles/.libs/shapefiles.a ./dxf/.libs/dxf.a ./md5/.libs/md5.a ./control_points/.libs/control_points.a ./cutter/.libs/cutter.a ./topology/.libs/topology.a ./srsinit/.libs/srsinit.a ./stored_procedures/.libs/stored_procedures.a ./connection_cache/.libs/connection_cache.a ./virtualtext/.libs/virtualtext.a ./wfs/.libs/wfs.a /mingw64/x86_64-w64-mingw32/lib/libwinpthread.dll.a /mingw64/x86_64-w64-mingw32/lib/libws2_32.a /mingw64/lib/liblzma.dll.a /mingw64/local/lib/libexpat.dll.a /mingw64/local/lib/libcurl.dll.a /mingw64/local/lib/libgeos.dll.a /mingw64/local/lib/libminizip.dll.a /mingw64/local/lib/librttopo.dll.a /mingw64/local/lib/libfreexl.dll.a /mingw64/local/lib/libiconv.dll.a /mingw64/local/lib/libproj.dll.a /mingw64/local/lib/libz.dll.a /mingw64/local/lib/libgeos_c.dll.a -Wl,--no-whole-archive -L/mingw64/local/lib -L./../.. -L=C:/msys64/mingw64/bin/../lib -L=C:/msys64/mingw64/x86_64-w64-mingw32/lib -I/msys64/mingw64/x86_64-w64-mingw32/include -L==C:/msys64/mingw64/bin/../lib -ltiff -lcrypt32 -lssl -lcrypto -lgdi32 -lwldap32 -lzstd -LC:/msys64/mingw64/bin/../lib -lxml2 -lsqlite3 -O2   -o static_bin/mod_spatialite.dll

I guess the problem I have now is that "-module" parameter (which I cannot find to exist on gcc linker), and that I still have to use "./s.o" with WinMain - I guess this is why it is not working.

Bojan Vukasovic

unread,
Dec 2, 2021, 9:58:53 AM12/2/21
to SpatiaLite Users
This one is also working - producing DLL that is huge in size (30MB), but it seems that it still uses DLLs in the end

gcc -shared versioninfo/.libs/mod_spatialite_la-version.o  -Wl,--allow-multiple-definition,--whole-archive ./gaiaaux/.libs/gaiaaux.a ./gaiaexif/.libs/gaiaexif.a ./gaiageo/.libs/gaiageo.a ./geopackage/.libs/geopackage.a ./spatialite/.libs/splite.a ./shapefiles/.libs/shapefiles.a ./dxf/.libs/dxf.a ./md5/.libs/md5.a ./control_points/.libs/control_points.a ./cutter/.libs/cutter.a ./topology/.libs/topology.a ./srsinit/.libs/srsinit.a ./stored_procedures/.libs/stored_procedures.a ./connection_cache/.libs/connection_cache.a ./virtualtext/.libs/virtualtext.a ./wfs/.libs/wfs.a /mingw64/x86_64-w64-mingw32/lib/libwinpthread.dll.a /mingw64/x86_64-w64-mingw32/lib/libws2_32.a /mingw64/lib/liblzma.dll.a /mingw64/local/lib/libexpat.dll.a /mingw64/local/lib/libcurl.dll.a /mingw64/local/lib/libgeos.dll.a /mingw64/local/lib/libminizip.dll.a /mingw64/local/lib/librttopo.dll.a /mingw64/local/lib/libfreexl.dll.a /mingw64/local/lib/libiconv.dll.a /mingw64/local/lib/libproj.dll.a /mingw64/local/lib/libz.dll.a /mingw64/local/lib/libgeos_c.dll.a -Wl,--no-whole-archive -L/mingw64/local/lib -L./../.. -L=C:/msys64/mingw64/bin/../lib -L=C:/msys64/mingw64/x86_64-w64-mingw32/lib -I/msys64/mingw64/x86_64-w64-mingw32/include -L==C:/msys64/mingw64/bin/../lib -ltiff -lcrypt32 -lssl -lcrypto -lgdi32 -lwldap32 -lzstd -LC:/msys64/mingw64/bin/../lib -lxml2 -lsqlite3 -O2   -o static_bin/mod_spatialite.dll

Reply all
Reply to author
Forward
0 new messages