Hello everyone, I have a problem with interop kotl...
# kotlin-native
a
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
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
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
I don’t know that you can dlopen a static lib
a
Copy code
I'm building a shared lib which is based on kotlin native, it includes a static library in C++
l
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
Right, but dlopen is specifically for dynamic libraries
a .a is a static lib
l
Did you try to change the linker path? Maybe it's not relative to the project folder.
a
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
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
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
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
Yes, I lost about 4 days on this error. And she decided so easily. @Landry Norris Thank you