Why does kotlin/native hello world not output anything inside IntelliJ IDEA on Apple M1?

Grz*_*icz 5 intellij-idea kotlin kotlin-native

I\'ve downloaded a fresh IntelliJ IDEA with the Kotlin multiplatform plugin and created a project using the Native application project template. This template creates a Main.kt file with the content:

\n
fun main() {\n    println("Hello, Kotlin/Native!")\n}\n
Run Code Online (Sandbox Code Playgroud)\n

As well as many other gradle files referencing kotlin("multiplatform") version "1.7.20". I can build and run the project from inside IntelliJ IDEA, but I see no hello world:

\n

没有你好世界

\n

我只能看到 gradle 输出和成功结果,但没有Hello Kotlin/Native! message anywhere. I\'ve tried changing the runDebugExecutableNative settings and checked the options related to show the console/output when stdout messages are printed:

\n

项目设置

\n

我看不到其他输出窗口/窗格,所以\xe2\x80\xa6 在哪里可以看到输出?项目构建调试或发布Kasha.kexe which I can run from the command line manually, but I\'m guessing an IDE shouldn\'t require me to run commands from the command line every time?

\n
$ ./build/bin/native/debugExecutable/Kasha.kexe\nHello, Kotlin/Native!\n
Run Code Online (Sandbox Code Playgroud)\n

我可以在运行窗格中看到一个灰色符号,它对应于输出> Task :runDebugExecutableNative SKIPPED。这是否意味着 IDE 只能构建但不能运行可执行文件?我正在使用 IntelliJ IDEA 2022.2.3(社区版)并且kotlin("multiplatform") version "1.7.20".

\n

更新bppleman correctly guesses the problem is I\'m trying to run this on an Apple M1 machine. The gradle that comes out from the template is:

\n
kotlin {\n    val hostOs = System.getProperty("os.name")\n    val isMingwX64 = hostOs.startsWith("Windows")\n    val nativeTarget = when {\n        hostOs == "Mac OS X" -> macosX64("native")\n        hostOs == "Linux" -> linuxX64("native")\n        isMingwX64 -> mingwX64("native")\n        else -> throw GradleException("Host OS is not supported in Kotlin/Native.")\n    }\n\xe2\x80\xa6\n
Run Code Online (Sandbox Code Playgroud)\n

macosX64呼叫替换为macosArm64 makes everything work as expected. But I guess now intel based apple machines will be on the wrong side of the fence?

\n

bpp*_*man 8

我看到问题有了新的编辑

用 macosArm64 替换 macosX64 调用使一切按预期工作。但我猜现在基于英特尔的苹果机器会站在栅栏的错误一边?

所以我将提交该问题的新答案

确实如你所说,换成intel mac后还是会出现问题,所以我的做法是同时判断当前的cpu平台,就像这样

val hostOs = System.getProperty("os.name")
val isMingw = hostOs.startsWith("Windows")
val nativeTarget = when {
    hostOs == "Mac OS X" -> {
        if (System.getProperty("os.arch").contains("aarch64")) {
            macosArm64("native")
        } else {
            macosX64("native")
        }
    }
    hostOs == "Linux" -> linuxX64("native")
    isMingw -> {
        if (System.getenv("ProgramFiles(x86)") != null) {
            mingwX86("native")
        } else {
            mingwX64("native")
        }
    }
    else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
Run Code Online (Sandbox Code Playgroud)

当然你也可以这样做,这取决于你的需求

kotlin {
    val hostOs = System.getProperty("os.name")
    val isMingwX64 = hostOs.startsWith("Windows")
    val nativeTargets = when {
        hostOs == "Mac OS X" -> listOf(macosArm64("native"), macosX64("macosX64"))
        hostOs == "Linux" -> listOf(linuxX64("native"))
        isMingwX64 -> listOf(mingwX64("native"))
        else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
    }

    nativeTargets.forEach { target ->
        target.apply {
            binaries {
                executable {
                    entryPoint = "main"
                }
            }
        }
    }

    sourceSets {
        val nativeMain by getting
        val nativeTest by getting
        val macosX64Main by getting {
            dependsOn(nativeMain)
        }
        val macosX64Test by getting {
            dependsOn(nativeTest)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用dependsOn来共享sourceSet。

需要注意的是,两个 macOS 源集之间只能有一个可执行条目

在此输入图像描述

你不应该为 macosX64 添加另一个条目,除非你真的想要,然后你必须删除 dependentOn 依赖项

你会得到:

在此输入图像描述

  • Intellij 默认情况下怎么可能不这样做???非常感谢! (2认同)