我正在编写一个针对 Windows、Linux 和 Mac 的 Kotlin/Native 应用程序,我需要在 Windows 上调用
platform.posix._popen
,在另一台上调用 platform.posix.popen
。
如果我使用简单的条件(
if (Platform.osFamily == OsFamily.WINDOWS)
),我会收到编译错误。
Kotlin 中没有
#ifdef
这样的东西,但是你可以使用 expect
/actual
机制来实现你所需要的。
在通用代码中,定义一个没有主体的
expect fun
,然后在linux/windows/macOS源集中单独定义一个actual fun
实现。