kotlin-multiplatformkotlin-nativecinterop

Kotlin/Native cinterop: compilerOpts inside .def file vs compilerOpts inside gradle


In documentation of cinterop for Kotlin/Native, you have to define a .def file such as:

language = Objective-C
modules = FirebaseCore
compilerOpts = -framework FirebaseCore
linkerOpts = -framework FirebaseCore

Now you could also specify a compilerOpts within the cinterop block such as:

compilations["main"].cinterops {
    firebasecore {
        packageName 'cocoapods.FirebaseCore'
        defFile = file("$projectDir/src/iosMain/c_interop/FirebaseCore.def")
        includeDirs ("$projectDir/../iosApp/Pods/FirebaseCore/Firebase/Core/Public")
        compilerOpts ("-F$projectDir/src/iosMain/c_interop/modules/FirebaseCore-${versions.firebaseCoreIos}")
    }
}

I only took this example by Kevin Galligan here: KMPFirestore.

I'm really confuse whats the difference between compilerOpts inside gradle vs compilerOpts inside .def file?

I have no strong C background and I'm just trying to learn Kotlin Multiplatform (and for now just trying to understand how to interop with c or objectiveC libraries) but I can't properly set them up.

I would really appreciate some help as I don't even fully understand how .def file work.

Any reading material related to understanding these things would be of great help.


Solution

  • AFAIK, the two compilerOpts will be merged when generating the interops. Sadly the linkerOpts does not work this way. But for me to make the interop work correctly and link, I had to specify the frameworks' absolute path for the linkerOpts too in the .def file.