https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
a

Andrey

03/25/2024, 4:46 PM
Hello everyone, I have a problem with interop kotlin, I'm trying to add a static library to my project. An error occurs at startup when the linker is looking for symbols.
Copy code
java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "hello" referenced by "/data/app/~~5wdEQm0yie2eojravmIojg==/kotlin.test-AITs0CaDTdEGYj6PdueKLA==/lib/arm64/libtest.so"...
                                                                                                    	at java.lang.Runtime.loadLibrary0(Runtime.java:1077)
                                                                                                    	at java.lang.Runtime.loadLibrary0(Runtime.java:998)
                                                                                                    	at java.lang.System.loadLibrary(System.java:1656)
My def file looks like this:
Copy code
package = api
headers = api.h
headerFilter = api.h
staticLibraries = libapi.a
libraryPaths = staticlib

compilerOpts = -Istaticlib
linkerOpts = -Lstaticlib
libraryPaths = staticlib
Any ideas please
l

Landry Norris

03/25/2024, 6:43 PM
Run
nm -C libapi.a
and see if the symbol shows up. The paths tend to be relative to the build folder, not the project directory, so add ../ before paths. I usually specify both '../' and '../../' since I remember seeing a certain way of running tests require a different directory.
Since you're statically linking libapi.a, you should also run nm on the so file, and the symbol should show up. Expect it to be listed with type 'T' or 't', not 'U'.
a

Andrey

03/25/2024, 6:45 PM
Thank you very much for your answer.
nm -C libapi.a | grep hello
-
0000000000000000 T hello()
nm -C libtest.so | grep hello
-
U hello
This is strange because in the build folder I find KLIB files that contain libapi.a
p

Paddy O'Brien

03/26/2024, 2:46 PM
I don’t know that you can dlopen a static lib
a

Andrey

03/26/2024, 2:47 PM
Copy code
I'm building a shared lib which is based on kotlin native, it includes a static library in C++
l

Landry Norris

03/26/2024, 2:47 PM
The dlopen trace is trying to open the shared object. It's odd that the symbol isn't present in the shared object. It should be, since it statically links in the library.
p

Paddy O'Brien

03/26/2024, 2:48 PM
Right, but dlopen is specifically for dynamic libraries
a .a is a static lib
l

Landry Norris

03/26/2024, 2:48 PM
Did you try to change the linker path? Maybe it's not relative to the project folder.
a

Andrey

03/26/2024, 2:49 PM
I tried to do a lot, it seems to me that my klib simply does not fall into the dependency, connecting it manually did not give any result. I also updated the entire environment to the latest versions
l

Landry Norris

03/26/2024, 2:50 PM
Is the 'hello' function declared in C++? If so, is it in an 'extern "C"' block? I think C++ links differently than C, and leads to weird errors with symbol resolution.
a

Andrey

03/26/2024, 2:51 PM
Copy code
Yes, I thought so too, so I did the same thing in the usual project for android. And it just works there
image.png,image.png
l

Landry Norris

03/26/2024, 2:53 PM
Kotlin/Native does not officially support linking against C++ symbols, only C. If you're using C++, the header should have an extern "C" block around the hello function prototype.
a

Andrey

03/26/2024, 3:46 PM
Yes, I lost about 4 days on this error. And she decided so easily. @Landry Norris Thank you
2 Views