Hi. I'm trying to use the FMOD low level libraries under Kotlin/Native. In the core fmod.h header for their library they are defining a struct called FMOD_SYSTEM, which is not available in source form, and therefore the header only includes a forward declaration:
typedef struct FMOD_SYSTEM FMOD_SYSTEM;
So I'm assuming that Kotlin/Native also made a forward declaration:
import cnames.structs.FMOD_SYSTEM
The following initialization call was translated from c:
FMOD_RESULT F_API FMOD_System_Create(FMOD_SYSTEM **system);
Into Kotlin as:
fun FMOD_System_Create(system: CValuesRef<CPointerVar<FMOD_SYSTEM>>?): FMOD_RESULT {
memScoped {
return kniBridge5(system?.getPointer(memScope).rawValue)
}
}
My question is, within Kotlin, is it currently possible to invoke that api call? And if so, how? I can only think of this:
memScoped {
val ptr = alloc<FMOD_SYSTEM::class>()
FMOD_System_Create(cValuesOf(ptr))
}
But I'm getting this error:
src/main/kotlin/hello.kt:8:21: error: type argument is not within its bounds: should be subtype of 'CVariable'
val ptr = alloc<FMOD_SYSTEM>()
I apologize in advance, as I still do not understand how to properly create and pass C pointers and references back and forth between C code and Kotlin code. Any help is appreciated.
Thanks for such an awesome compiler!
IIRC, there is a method called allocPointerTo(), so you should be able to do the following:
memScoped {
val ptr = allocPointerTo<FMOD_SYSTEM>()
FMOD_System_Create(ptr.ptr)
}
(under the hood that's just alloc<CPointer<FMOD_SYSTEM>>())
Yep, that worked. I really need to re-read the documentation to try and find out how to leverage KN's native memory allocation semantics. Much appreciated!
Most useful places to skim through in search for assist with C interoperability are INTEROP.md file, samples and kotlinx.cinterop package sources (don't forget to switch branch if using prebuilt compiler instead of master). I consult the latter one quite often both for available functions and for type hierarchy reference, as it saves you big times considering the current state of tooling.
Good luck and have fun! :)
IIRC, there is a method called
allocPointerTo(), so you should be able to do the following:memScoped { val ptr = allocPointerTo<FMOD_SYSTEM>() FMOD_System_Create(ptr.ptr) }(under the hood that's just
alloc<CPointer<FMOD_SYSTEM>>())
I am attempting to use this solution from the 'iosMain' source set of a Kotlin multiplatform project, and haven't been able to figure out any way to get it to recognize kotlinx.cinterop imports, or any declarations from these packages. I've tried adding several different plugins and dependencies to the build.gradle, but nothing has worked. What needs to be added to be able to reference this function?
It should work as soon as you declare native target. kotlinx.cinterop package is part of stdlib, which is added as a dependency automatically, without any additional references in build script. Make sure you are using latest IDE, Kotlin plugin for your IDE, and kotlin-multiplatform Gradle plugin versions. If it still doesn't work for you, you should probably visit #multiplatform Slack channel for additional assistance, as much more people are following it than Issues section here.
Most helpful comment
IIRC, there is a method called
allocPointerTo(), so you should be able to do the following:(under the hood that's just
alloc<CPointer<FMOD_SYSTEM>>())