Getting stuck with translating a C Macro (creates ...
# kotlin-native
n
Getting stuck with translating a C Macro (creates a Struct? - https://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/_m_q_t_t_client_8h.html#aefd7c865f2641c8155b763fdf3061c25) into a custom def declaration (https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md#adding-custom-declarations) that will create a instance of MQTTClient_connectOptions (generated class in the KLib). This is part of a Kotlin Native program which uses the Eclipse Paho MQTT Client library (https://www.eclipse.org/paho/clients/c/).
m
Something like this, maybe:
Copy code
static inline void MQTTClient_connectOptions_initialize(struct MQTTClient_connectOptions *value) {
    *value = MQTTClient_connectOptions_initializer;
}
o
yeah, Mike got the general pattern right - if you see no way to do smth in K/N, just add C function into .def file and do it there. However, we will investigate how to do structure initialization right
n
Especially tricky when it is not clear which order the properties in a struct are being initialised (when it comes to default values).
Is there a way to get more informative error messages from the cinterop tool?. End up with the following error after adding in the workaround in the def file:
Copy code
Exception in thread "main" java.lang.Error: /tmp/tmp18083280284659516124.c:3:14: error: expected expression
m
Well, yes - this was not valid C. But this should work:
Copy code
static inline void MQTTClient_connectOptions_initialize(MQTTClient_connectOptions *value) {
    MQTTClient_connectOptions initializer = MQTTClient_connectOptions_initializer;
    *value = initializer;
}
s
@napperley you may be interested in taking a look at this Kotlin/Native application using MQTT: https://github.com/madhead/seaowl For example,
.def
file: https://github.com/madhead/seaowl/blob/master/sensor/src/main/c_interop/mqtt.def
👍 2
By the way, this project is written by @madhead
n
This error is outputted by konanc when trying to get a pointer from a CVariable (MQTTClientVar):
Copy code
error: unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
public final val <T : CVariable> CValues<MQTTClientVar /* = CPointerVarOf<MQTTClient /* = CPointer<out CPointed> */> */>.ptr: CPointer<MQTTClientVar /* = CPointerVarOf<MQTTClient /* = CPointer<out CPointed> */> */> defined in kotlinx.cinterop.MemScope
	createMqttClient(client.ptr, brokerUrl, clientId, MQTTCLIENT_PERSISTENCE_NONE, null)
Here is the Klib definition:
Copy code
fun MQTTClient_create(handle: CValuesRef<MQTTClientVar /* = CPointerVarOf<MQTTClient /* = CPointer<out CPointed> */> */>?, serverURI: String?, clientId: String?, persistence_type: Int, persistence_context: CValuesRef<*>?): Int
m
How do you define
client
?
n
Like this:
Copy code
memScoped {
  val client = alloc<MQTTClientVar>()
  //...
}
o
could you provide self-containing non-compiling source, please?
n