ctripcorp / SQLlin

A DSL ORM library for Kotlin Multiplatform.
Apache License 2.0
219 stars 10 forks source link

Unable to link under Linux #48

Closed teras closed 9 months ago

teras commented 9 months ago

Hello I am trying to use SQLlin in Kotlin/Native Linux x64 platform. Unfortunately it breaks with an error:


The /home/teras/.konan/dependencies/x86_64-unknown-linux-gnu-gcc-8.3.0-glibc-2.19-kernel-4.9-2/x86_64-unknown-linux-gnu/bin/ld.gold command returned non-zero exit code: 1.
output:
/home/teras/.konan/dependencies/x86_64-unknown-linux-gnu-gcc-8.3.0-glibc-2.19-kernel-4.9-2/x86_64-unknown-linux-gnu/bin/ld.gold: error: cannot find -lsqlite3
/tmp/konan_temp4860865497063593644/result.o:out:function com_ctrip_sqllin_sqlite3_sqlite3_close_v2_wrapper7: error: undefined reference to 'sqlite3_close_v2'
/tmp/konan_temp4860865497063593644/result.o:out:function com_ctrip_sqllin_sqlite3_sqlite3_exec_wrapper8: error: undefined reference to 'sqlite3_exec'
...

plus a huge list of unresolved references to sqlite functions.

Of course, I do have sqlite installed

$ ls -l /usr/lib/libsqlite3.so 
lrwxrwxrwx root root 19 B Mon Sep 11 20:45:28 2023  /usr/lib/libsqlite3.so ⇒ libsqlite3.so.0.8.6

so the problem must be something else. Any idea?

qiaoyuang commented 9 months ago

Did your Linux executable program project add the linkerOpts into your build.gradle.kts correctly?

teras commented 9 months ago

I was following the documentation to the letter. Are there any options I should put? The interesting part is that the library itself added the -lsqlite3 option, not me.

qiaoyuang commented 9 months ago

Try adding this line to your build.gradle.kts executable program project:

linkerOpts += listOf("-lsqlite3", "-L/usr/lib")

If that doesn't work, try using the libsqlite3.a to replace your libsqlite3.so file.

teras commented 9 months ago

This improved the situation, but I have another problem now: (The error list is exhaustive)

The /home/teras/.konan/dependencies/x86_64-unknown-linux-gnu-gcc-8.3.0-glibc-2.19-kernel-4.9-2/x86_64-unknown-linux-gnu/bin/ld.gold command returned non-zero exit code: 1.
output:
/usr/lib/libsqlite3.so: error: undefined reference to 'fmod', version 'GLIBC_2.38'
/usr/lib/libsqlite3.so: error: undefined reference to 'fcntl64', version 'GLIBC_2.28'
/usr/lib/libsqlite3.so: error: undefined reference to 'fstat64', version 'GLIBC_2.33'
/usr/lib/libsqlite3.so: error: undefined reference to 'log', version 'GLIBC_2.29'
/usr/lib/libsqlite3.so: error: undefined reference to 'dlerror', version 'GLIBC_2.34'
/usr/lib/libsqlite3.so: error: undefined reference to 'pthread_mutexattr_destroy', version 'GLIBC_2.34'
/usr/lib/libsqlite3.so: error: undefined reference to 'stat64', version 'GLIBC_2.33'
/usr/lib/libsqlite3.so: error: undefined reference to 'dlopen', version 'GLIBC_2.34'
/usr/lib/libsqlite3.so: error: undefined reference to 'exp', version 'GLIBC_2.29'
/usr/lib/libsqlite3.so: error: undefined reference to 'pthread_join', version 'GLIBC_2.34'
/usr/lib/libsqlite3.so: error: undefined reference to 'pthread_mutexattr_init', version 'GLIBC_2.34'
/usr/lib/libsqlite3.so: error: undefined reference to 'pthread_mutexattr_settype', version 'GLIBC_2.34'
/usr/lib/libsqlite3.so: error: undefined reference to 'pthread_create', version 'GLIBC_2.34'
/usr/lib/libsqlite3.so: error: undefined reference to 'pow', version 'GLIBC_2.29'
/usr/lib/libsqlite3.so: error: undefined reference to 'pthread_mutex_trylock', version 'GLIBC_2.34'
/usr/lib/libsqlite3.so: error: undefined reference to 'dlclose', version 'GLIBC_2.34'
/usr/lib/libsqlite3.so: error: undefined reference to 'dlsym', version 'GLIBC_2.34'
/usr/lib/libsqlite3.so: error: undefined reference to 'log2', version 'GLIBC_2.29'
/usr/lib/libsqlite3.so: error: undefined reference to 'lstat64', version 'GLIBC_2.33'
qiaoyuang commented 9 months ago

Try to use the libsqlite3.a to replace your libsqlite3.so under the same directory.

teras commented 9 months ago

Hello

First of all, asking to link statically shouldn't be a proper solution. There are reasons why dynamic libraries are present. Especially if we override system files, like /usr/lib/*.so.

Having said that, I found a solution which probably should be documented somewhere. In order for linking to work, linker should be more relaxed. So instead of only linkerOpts += listOf("-lsqlite3", "-L/usr/lib") as parameters, the parameter --allow-shlib-undefined is also required.

Final list of parameters is:

 linkerOpts += mutableListOf("-lsqlite3", "-L/usr/lib", "--allow-shlib-undefined")
qiaoyuang commented 9 months ago

OK, thank you. That's good. I will modify related documents.