How do I use a shared library from Kotlin/Native? ...
# kotlin-native
m
How do I use a shared library from Kotlin/Native? All tutorials are for creating a shared library, not using one. I created a
.def
file, seems to work, I have autocompletion of library functions, project compiles, but fails when linking. I have a compiled
.so
file for the library, but cannot find how to make my project use it.
Got it, in the
.def
file I needed to add
linkerOpts = -Lsrc/nativeInterop/cinterop -l wiringPi
, having
libwiringPi.so
in that path. The filename of the
.so
file is related to the
-l
option, but
lib
is cut from the name. Also I had it as
libwiringPi.so.2.46
, the version number at the end of the filename was a problem.
Also adding
-R .
to
linkerOpts
actually made it find the
.so
file when the executable is run, when the
.so
file is distributed along the executable in the same directory.
n
The changes you have made to
linkerOpts
will work in development via Gradle, but will not work when the program is run in production via the terminal.
m
It works though, in fact I cannot run the program in any other way than "in production", because I run it on a remote Raspberry Pi, I cannot run locally on my dev machine because the architecture does not match, I cross-compile for the Pi.
a
Hello, @Marcin Wisniowski! Can you clarify, is this
.so
file a product of some Kotlin/Native code compilation or not?
m
@Artyom Degtyarev [JB] It's a C library I compiled on a Raspberry Pi, resulting in the
.so
file. I am dynamically linking to it from my Kotlin/Native project, which I am cross-compiling from a Linux host to the Raspberry Pi. It was easy to setup with making a
.def
file to get the Idea to recognize it and it was compiling, but failed linking, which I had a hard time understanding how to fix. It's all working now though, as I said above.
v
I've stumbled upon this thread while trying to understand why I can't get Kotlin Native to link to a Pi linux library (in this case,
libcurl
). IntelliJ understands the headers, I think compiling works, but linking fails, Ultimately I want to be doing something similar to you, linking to `wiringPi`or a similar library. How did you solve your linking problems?
m
I don't really remember what I did, it was a year ago, but I am happy to link you the repo of my project, where it all works.
https://gitlab.com/Nohus/football-table Might be outdated now, with the fast paced development of Kotlin, I haven't been updating it since the project is finished and does it's job. Should take you on the right path though.
v
Thank you for replying, I'll definitely check it out. Did you build in directly on the Pi, or from another machine?
m
I built wiringPi directly on the Pi to get the .so library. But the actual software I cross-compiled on a Linux machine.