Patrick
01/31/2020, 3:17 PMclass 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:
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:
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?Patrick
01/31/2020, 3:31 PMKavan
02/01/2020, 1:12 PMval input = 45:
Pass it like this didConnect(::input)
olonho
02/02/2020, 9:09 AMnapperley
02/03/2020, 3:18 AMPatrick
02/03/2020, 7:48 AMvar 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 CPatrick
02/03/2020, 7:50 AMolonho
02/03/2020, 2:14 PM-*Xexport*-library
flagPatrick
02/04/2020, 9:10 AMilya.matveev
02/04/2020, 3:56 PM1.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.:
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-562538118ilya.matveev
02/04/2020, 3:59 PMKavan
02/04/2020, 3:59 PMnapperley
02/04/2020, 6:21 PMHere 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.