Skip to content

konan 编译器选项

源:Kotlin/Native Compiler Options

konan 是 Kotlin/Native 的核心编译器。深入理解编译器选项能够优化构建性能和产物质量。

基础命令

编译单文件

bash
# 最简单的编译
kotlinc-native hello.kt -o hello

# 运行
./hello.kexe

指定输出类型

bash
# 可执行文件 (默认)
kotlinc-native main.kt -o program

# 动态库
kotlinc-native -produce dynamic mylib.kt -o mylib

# 静态库
kotlinc-native -produce static mylib.kt -o mylib

# Framework (iOS/macOS)
kotlinc-native -produce framework MyFramework.kt -o MyFramework

优化选项

优化级别

bash
# -opt=none: 禁用所有优化
kotlinc-native -opt=none main.kt -o debug_build

# 编译时间: ~5s
# 运行性能: 基准线
# 二进制大小: 较大
bash
# -opt: 启用基础优化
kotlinc-native -opt main.kt -o release_build

# 编译时间: ~15s (3x)
# 运行性能: 2-3x 提升
# 二进制大小: 减少 20%
bash
# -opt + -Xlto: 链接时优化
kotlinc-native -opt -Xlto main.kt -o optimized_build

# 编译时间: ~30s (6x)
# 运行性能: 3-5x 提升
# 二进制大小: 减少 40%

特定优化

bash
# 启用内联优化
kotlinc-native -Xopt-in=kotlin.experimental.OptIn main.kt

# 禁用特定编译阶段
kotlinc-native -Xdisable-phases=Devirtualization main.kt

# 使用特定分配器
kotlinc-native -Xallocator=mimalloc main.kt

# 代码大小优化(而非速度)
kotlinc-native -Xoptimize-for-size main.kt

调试选项

调试信息

bash
# 生成调试符号
kotlinc-native -g main.kt -o debug_app

# 保留所有调试信息
kotlinc-native -g -Xg0 main.kt -o full_debug

# dSYM 文件 (macOS/iOS)
kotlinc-native -g -Xembed-bitcode main.kt -o app_with_dsym

调试器支持

bash
# LLDB 调试
kotlinc-native -g main.kt -o myapp
lldb ./myapp.kexe

# GDB 调试
kotlinc-native -g main.kt -o myapp
gdb ./myapp.kexe

平台配置

目标平台

bash
# 查看支持的目标
kotlinc-native -list-targets

# 输出:
# linuxX64
# linuxArm64
# macosX64
# macosArm64
# iosArm64
# iosX64
# iosSimulatorArm64
# mingwX64
# androidNativeArm64
# ...

# 指定目标平台
kotlinc-native -target linuxArm64 main.kt -o app_arm64

交叉编译

bash
# 在 x64 macOS 上编译 ARM64 macOS 程序
kotlinc-native -target macosArm64 main.kt -o app_arm64

# 在 Linux 上编译 Windows 程序
kotlinc-native -target mingwX64 main.kt -o app.exe

# 在 macOS 上编译 iOS 程序
kotlinc-native -target iosArm64 main.kt -o iOSApp

库管理

链接库

bash
# 链接系统库
kotlinc-native main.kt -o app \
    -linker-option -lm \
    -linker-option -lpthread

# 链接自定义库
kotlinc-native main.kt -o app \
    -linker-option -L/usr/local/lib \
    -linker-option -lmylib

# 链接 Framework (macOS/iOS)
kotlinc-native main.kt -o app \
    -linker-option "-framework CoreFoundation"

包含路径

bash
# 添加头文件搜索路径
kotlinc-native main.kt -o app \
    -compiler-option -I/usr/include \
    -compiler-option -I/usr/local/include

内存管理

GC 配置

bash
# GC 触发阈值 (8MB)
kotlinc-native -Xgc-threshold=8388608 main.kt

# 目标堆大小 (32MB)
kotlinc-native -Xgc-target-heap-bytes=33554432 main.kt

# 最小堆增长 (1MB)
kotlinc-native -Xgc-min-heap-bytes=1048576 main.kt

# 启用 GC 调度器
kotlinc-native -Xgc-scheduler=aggressive main.kt

内存模型

bash
# 新内存模型 (默认,Kotlin 1.7.20+)
kotlinc-native -Xbinary=memoryModel=experimental main.kt

# 禁用对象冻结检查(新 MM)
kotlinc-native -Xbinary=freezing=disabled main.kt

高级选项

并发编译

bash
# 启用并行编译(加快大项目编译)
kotlinc-native -Xmulti-threaded main.kt

# 指定线程数
kotlinc-native -Xworkers=4 main.kt

Bitcode

bash
# 嵌入 LLVM Bitcode (iOS 需要)
kotlinc-native -Xembed-bitcode main.kt -o app

# 仅标记 Bitcode
kotlinc-native -Xembed-bitcode-marker main.kt

导出符号

bash
# 导出所有符号
kotlinc-native -Xexport-kdoc main.kt

# 生成 Objective-C 头文件
kotlinc-native -produce framework \
    -Xobjc-generics \
    MyFramework.kt -o MyFramework

Gradle 集成

应用编译器选项

kotlin
// build.gradle.kts
kotlin {
    targets.withType<KotlinNativeTarget> {
        binaries.all {
            // Release 优化
            if (buildType == NativeBuildType.RELEASE) {
                freeCompilerArgs += listOf(
                    "-opt",
                    "-Xlto",
                    "-Xallocator=mimalloc"
                )
            }
            
            // Debug 配置
            if (buildType == NativeBuildType.DEBUG) {
                freeCompilerArgs += listOf(
                    "-g",
                    "-Xg0",
                    "-opt=none"
                )
            }
            
            // GC 调优
            freeCompilerArgs += listOf(
                "-Xgc-threshold=8388608",
                "-Xgc-target-heap-bytes=33554432"
            )
            
            // 链接器选项
            linkerOpts += listOf(
                "-L/usr/local/lib",
                "-lmylib"
            )
        }
    }
}

平台特定配置

kotlin
kotlin {
    linuxX64 {
        binaries.executable {
            freeCompilerArgs += listOf(
                "-opt",
                "-linker-option", "-L/usr/lib/x86_64-linux-gnu"
            )
        }
    }
    
    macosArm64 {
        binaries.executable {
            freeCompilerArgs += listOf(
                "-opt",
                "-Xallocator=mimalloc",
                "-linker-option", "-framework", "-linker-option", "CoreFoundation"
            )
        }
    }
}

性能对比

优化级别影响

配置编译时间运行速度二进制大小适用场景
-opt=none5s1x2.5MB开发调试
-opt15s2.5x2.0MB日常使用
-opt -Xlto30s4x1.5MB生产发布

内存分配器对比

bash
# 测试:100万次对象分配

# 默认分配器
kotlinc-native -opt main.kt
# 耗时: 450ms

# mimalloc
kotlinc-native -opt -Xallocator=mimalloc main.kt
# 耗时: 320ms (1.4x 提升)

# 自定义分配器
kotlinc-native -opt -Xallocator=custom main.kt
# 耗时: 取决于实现

故障排查

启用详细输出

bash
# 详细编译日志
kotlinc-native -verbose main.kt -o app

# 显示所有警告
kotlinc-native -Werror main.kt

# 显示 LLVM 信息
kotlinc-native -Xllvm-variant=default -verbose main.kt

常见问题

bash
# 错误
kotlinc-native main.kt -o app
# undefined reference to `my_function`

# 解决:添加库路径
kotlinc-native main.kt -o app \
    -linker-option -L/path/to/lib \
    -linker-option -lmylib
bash
# 问题:-opt 后程序崩溃

# 调试:逐步启用优化
kotlinc-native -opt=none main.kt  # OK
kotlinc-native -opt main.kt       # 崩溃

# 禁用特定优化阶段
kotlinc-native -opt \
    -Xdisable-phases=Devirtualization \
    main.kt
bash
# 错误:Cannot find toolchain for target

# 解决:安装目标工具链
# Linux → mingw
sudo apt install mingw-w64

# 重新编译
kotlinc-native -target mingwX64 main.kt

掌握 konan 编译器选项能够在开发效率和产物性能之间找到最佳平衡。Production 构建建议使用 -opt -Xlto 获得最佳性能。