I have a Kotlin Class that looks like this: ```cla...
# kotlin-native
p
I have a Kotlin Class that looks like this:
Copy code
class Device {
    var didConnectFunc: CFunction<() -> Unit>? = null

    fun didConnect() {
        didConnectFunc?.ptr?.invoke()
    }
}
If I export this as a dynamic C library, I get can set the function to be executed like this:
Copy code
void *didConnect() {
    printf("DID CONNECT\n");
}

lib->kotlin.root.Device.set_didConnectFunc(device, didConnect);
This works fine. What do I have to do however if I want to pass an Argument to my C function like this:
Copy code
class Device {
    var didConnectFunc: CFunction<(barcode: Barcode) -> Unit>? = null

    fun didConnect(barcode: Barcode) {
        didConnectFunc?.ptr?.invoke(barcode)
    }
}
This code does not compile with the following error message:
type [@kotlin.ParameterName] Barcode is not supported here: doesn't correspond to any C type
How would I best solve this problem? Is it possible to create a pointer to the Barcode class and pass that to the function?
It also works if I pass a standard type like Int instead of an object like Barcode. But how would I make it work with the object?
k
Declare value outside main function like
val input = 45:
Pass it like this
didConnect(::input)
o
note that C function is smth that could be called from C, so only C types could be parts of the signature
n
Does that mean Kotlin value types (Inline Classes) aren't accessible from C?
p
I figured it out:
Copy code
var didConnectFunc: CFunction<(barcode: COpaquePointer) -> Unit>? = null

didConnectFunc?.ptr?.invoke(StableRef.create(barcode).asCPointer())
This works, now I just have to cast the barcode to the correct struct when calling it from C
Is there a way to tell KN to specifically export a type in the header file? Seems like I have to do the definitions myself since they are not exported by default.
o
yes
-*Xexport*-library
flag
p
Where do I have to put that flag?
i
If you use a 1.3.70-EAP version of Kotlin Gradle plugin (e.g.
1.3.70-eap-184
), you can export declarations from one of your dependencies to the C library in the same way as it is done for a framework now, e.g.:
Copy code
kotlin.linuxX64 {
    binaries.staticLib {
        export(project(":some-dependency-project"))
    }
}
In 1.3.60, there is no DSL for such an export but you can use a workaround described in this issue: https://github.com/JetBrains/kotlin-native/issues/3571#issuecomment-562538118
Here is a doc about exporting dependencies to frameworks: https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#exporting-dependencies-in-frameworks. Exporting to libraries in the EAP works in the same way.
k
Here in Arguments
n
Copy code
Here is a doc about exporting dependencies to frameworks: <https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#exporting-dependencies-in-frameworks>. Exporting to libraries in the EAP works in the same way.
There is a need for more Kotlin Native documentation that isn't tied into MacOS or Mobile development, especially with Linux development which is lacking.