Cinterop with C++

In the new kotlinx.datetime library the documentation states:

Implementation

The implementation of date/time types, such as Instant, LocalDateTime, TimeZone and so on, relies on:

Indeed in the cinterop Gradle configuration code i noticed those lines:

    targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
        compilations["main"].cinterops {
            create("date") {
                val cinteropDir = "$projectDir/nativeMain/cinterop"
                val dateLibDir = "${project(":").projectDir}/thirdparty/date"
                headers("$cinteropDir/public/cdate.h")
                defFile("nativeMain/cinterop/date.def")
                // common options
                extraOpts("-Xsource-compiler-option", "-std=c++17")
                extraOpts("-Xsource-compiler-option", "-I$cinteropDir/public")
                extraOpts("-Xsource-compiler-option", "-include$cinteropDir/cpp/defines.hpp")
                // *nix support
                extraOpts("-Xcompile-source", "$dateLibDir/src/tz.cpp")
                extraOpts("-Xcompile-source", "$dateLibDir/src/ios.mm")
                extraOpts("-Xsource-compiler-option", "-I$dateLibDir/include")
                extraOpts("-Xcompile-source", "$cinteropDir/cpp/cdate.cpp")
                // iOS support
                extraOpts("-Xcompile-source", "$cinteropDir/cpp/apple.mm")
                // Windows support
                extraOpts("-Xcompile-source", "$cinteropDir/cpp/windows.cpp")
            }
        }

There are many .cpp file references and a manual cinterop compiler option to use C++17

Is there any documentation or roadmap for C++ cinterop-ing? :upside_down_face:

I remember reading that the headers for the time zone stuff is still plain C. So just the implementation part is C++. So you can use C++ as long as you only need access to C header files.

Yeah but what about

extraOpts("-Xsource-compiler-option", "-std=c++17")

My guess is that it compiles the code with a c++ compatible compiler but it exposes plain C functions so no interop between kotlin and c++ there.

2 Likes