Kotlin/Native 的“#ifdef”编译条件

问题描述 投票:0回答:1

我正在编写一个针对 Windows、Linux 和 Mac 的 Kotlin/Native 应用程序,我需要在 Windows 上调用

platform.posix._popen
,在另一台上调用
platform.posix.popen

如果我使用简单的条件(

if (Platform.osFamily == OsFamily.WINDOWS)
),我会收到编译错误。

kotlin kotlin-multiplatform kotlin-native
1个回答
0
投票

Kotlin 中没有

#ifdef
这样的东西,但是你可以使用
expect
/
actual
机制来实现你所需要的。

在通用代码中,定义一个没有主体的

expect fun
,然后在linux/windows/macOS源集中单独定义一个
actual fun
实现。

© www.soinside.com 2019 - 2024. All rights reserved.