Native interoperability (cinterop)¶
Kotlin/Native provides the cinterop tool to facilitate interaction with C and Objective-C libraries.
The Kotlin Toolchain supports cinterop out of the box with zero configuration for the common use case.
Basic usage¶
To add a C or Objective-C interop to your multiplatform module:
- Create a
cinteropdirectory in your module root (alongsidesrcandtest). - Drop your
.deffiles into this directory.
The Kotlin Toolchain will automatically detect these files and configure the cinterop tool for all applicable native platforms.
No additional configuration in your module.yaml is required.
Platform-specific definitions¶
You can use the @<platform> qualifier
for the cinterop directory to limit interop definitions to specific platforms or platform families.
At the file level vs. directory level
The .def file format already supports platform-specific configuration for the library, for example:
compilerOpts.linux vs. compilerOpts.osx.
So it is possible to use a single .def file in the common cinterop directory
instead of having separate platform-specific files under, e.g., cinterop@linux and cinterop@macos.
Both approaches are currently valid - use the one you prefer.
Advanced usage¶
If you need the .def file generated or provisioned (for example, to implement custom library location or provisioning logic),
you can write a plugin for this.
See the relevant docs for more details on how to contribute cinteropDefinitions.