Hello! I'm trying to set up a library and applicat...
# kotlin-native
n
Hello! I'm trying to set up a library and application project with kotlin/native using the multiplatform project kotlin gradle dsl. Two questions: 1. Is it possible to have an expect function defined in a shared library that is then defined by the application that uses that library (something akin to C++
extern
function)? 2. Is it possible to have a
fun main()
, ie an entry point, in a shared library like you can in C++?
d
The first is not possible. Expect/actual only works within modules.
The second one is possible.
How it can be done depends on what you mean by shared library. Klib, dll, a, dylib?
If it is a klib, then you can set the entry point in your application project to be the main declared in your library project. (I haven't actually tried this).
n
alright, that makes sense on point 2.
So there's no mechanism for externally defined functions? I know that JNI definitions on android using NDK use the
external
keyword. nothing like that for Kotlin/Native?
d
It can be done but it's not native to kotlin to my knowledge. You could do your own dynamic loading?
I'm not too sure tbh. Someone more knowledgeable will see your question. 😅
👍 1
n
so I've figured out how to export a function to be used by C++ (
@SymbolName("function") external fun kotlinFunction()
) which seems to have the same effect as
extern void function()
in C, but now I'm not sure how to implement and link the right function in the application project
I'm getting "ld.lld: error: undefined symbol: function" on the application linkDebugExecutableLinux step
d
Try
@CName(externName = "function", shortName = "lolol") fun someFun() {}
In the application project btw.
n
That doesn't seem to do anything different. Maybe it has something to do with linking order and I have to set some linkerOpts?
d
You shouldn't have to. The symbol is in the application binary.
@CName("function") fun function() {}
should work.
d
As far as I know,
extern
symbols must be defined elsewhere in the same compilation in order for them to be resolved. In that, it's a bit like
expect
in kotlin if you consider the pre processor.
n
I think I see the problem... the extern function in C would be defined the header in order to use it like I'm trying to. I'm thinking this might just not be possible.
s
@CName
annotation is unrelated. What do you mean by “shared library”? Is it
.klib
or `.so`/`.dylib`/`.dll`?