Kotlin/Native开发环境搭建

Kotlin/Native is a technology for compiling Kotlin code to native binaries, which can run without a virtual machine. It is an LLVM based backend for the Kotlin compiler and native implementation of the Kotlin standard library.

Kotlin/Native目前虽然是Beta版,但是已经比较稳定,是目前最靠谱的跨平台技术之一。
KN的安装过程非常简单,本文将基于Linux/MacOS环境安装KN的过程做可一个简单的整理,详细步骤如下:


安装JVM


Kotlin的编译离不开JVM,所以安装JVM是前提条件。JVM的安装想必大家都很清楚,这里就不具体介绍了。

安装Kotlin/Native


https://github.com/JetBrains/kotlin/releases/latest
从官方下载KN最新版本,下载需要使用wget

mac

$ brew install wget

linux(ubuntu)

$ sudo apt install wget

然后使用wget下载KN

mac

$ wget https://github.com/JetBrains/kotlin/releases/download/v1.3.61/kotlin-native-macos-1.3.61.tar.gz

linux(ubuntu)

$ wget https://github.com/JetBrains/kotlin/releases/download/v1.3.61/kotlin-native-linux-1.3.61.tar.gz

下载完成后,解压并移动到谁当位置,我们选择路径/usr/local/kotlin-native

$ tar xzvf kotlin-native-macos-1.3.61.tar.gz
$ sudo mkdir -p /usr/local/kotlin-native
$ sudo mv kotlin-native-macos-1.3.61/* /usr/local/kotlin-native

配置环境变量,~/.bash_profile中添加如下

$ export PATH=$PATH:/usr/local/kotlin-native/bin/

安装 IntelliJ IDEA

虽然也可以使用别的IDE进行开发,但是同属Jetbrains家族的产品,对KN的支持自然最到位。天然集成gradle也会为我们在开发中对于各种依赖库的管理提供方便。
请到IntelliJ IDEA官网自行下载安装即可,过程非常简单
在这里插入图片描述


Hello world


打开IDEA,写下第一行KN代码

fun main(args: Array<String>) {
    println("Hello, World!")
}

编译


初次运行时需要下载和安装LLVM所依赖的各个package,会稍微花点时间

$ kotlinc-native hello.kt -o hello

运行


$ ./hello.kexe
Hello, World!

程序顺利执行!


编译Library


通过-p参数,可以编译成任意形式的Library

$ kotlinc-native hello.kt -p dynamic
-p 参数lib类型
program可执行二进制
static.a文件
dynamicLinux下生成.so, macOS下生成.dylib
frameworkiOS/macOS所需的framework
libraryklib(Kotlin库)
bitcodebc文件(LLVM的Bitcodebc)

可以通过-h查询KN更多参数的使用方法

$ kotlinc-native -h
Usage: kotlinc-native <options> <source files>
where possible options include:
  -g                         Enable emitting debug information
  -enable-assertions (-ea)   Enable runtime assertions in generated code
  -friend-modules <path>     Paths to friend modules
  -generate-no-exit-test-runner (-trn)
                             Produce a runner for unit tests not forcing exit
  -generate-test-runner (-tr) Produce a runner for unit tests
  -generate-worker-test-runner (-trw)
                             Produce a worker runner for unit tests
  -include-binary (-ib) <path> Pack external binary within the klib
  -library (-l) <path>       Link with the library
  -library-version (-lv) <version>
                             Set library version
  -linker-options <arg>      Pass arguments to linker
  -list-targets              List available hardware targets
  -entry (-e) <name>         Qualified entry point name
  -manifest <path>           Provide a maniferst addend file
  -memory-model <model>      Memory model to use, 'strict' and 'relaxed' are currently supported
  -module-name <name>        Specify a name for the compilation module
  -native-library (-nl) <path> Include the native bitcode library
  -no-default-libs           Don't link the libraries from dist/klib automatically
  -no-endorsed-libs          Don't link the endorsed libraries from dist automatically
  -nomain                    Assume 'main' entry point to be provided by external libraries
  -nopack                    Don't pack the library into a klib file
  -nostdlib                  Don't link with stdlib
  -opt                       Enable optimizations during compilation
  -output (-o) <name>        Output name
  -produce (-p) {program|static|dynamic|framework|library|bitcode}
                             Specify output file kind
  -repo (-r) <path>          Library search path
  -linker-option <arg>       Pass argument to linker
  -target <target>           Set hardware target
  -Werror                    Report an error if there are any warnings
  -api-version <version>     Allow to use declarations only from the specified version of bundled libraries
  -X                         Print a synopsis of advanced options
  -help (-h)                 Print a synopsis of standard options
  -kotlin-home <path>        Path to Kotlin compiler home directory, used for runtime libraries discovery
  -language-version <version> Provide source compatibility with specified language version
  -P plugin:<pluginId>:<optionName>=<value>
                             Pass an option to a plugin
  -progressive               Enable progressive compiler mode.
                             In this mode, deprecations and bug fixes for unstable code take effect immediately,
                             instead of going through a graceful migration cycle.
                             Code written in the progressive mode is backward compatible; however, code written in
                             non-progressive mode may cause compilation errors in the progressive mode.
  -nowarn                    Generate no warnings
  -verbose                   Enable verbose logging output
  -version                   Display compiler version
  @<argfile>                 Read compiler arguments and file paths from the given file

最后


编译过程感觉有些慢,据说Kotlin1.4对KN的编译有很多优化,值得期待。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用Kotlin/Native中的CInterop来调用C动态库。首先需要定义C函数的原型,并使用Kotlin的CName注解来指定C函数的名称。然后使用external关键字在Kotlin函数中声明C函数,并使用CName注解来指定C函数的名称。最后可以直接调用该Kotlin函数来调用C动态库中的函数。 以下是一个示例代码: ```kotlin import kotlinx.cinterop.* fun main() { // Load C dynamic library val lib = dlopen("libfoo.dylib", RTLD_LAZY) // Define C function prototype typealias my_c_function_t = CFunction<IntVar.(CPointer<ByteVar>) -> Unit> // Declare C function using external and CName annotations @CName("my_c_function") external fun myCFunction(ptr: CPointer<ByteVar>): Int // Call C function memScoped { val buffer = allocArray<ByteVar>(1024) val result = myCFunction(buffer) println("C function returned: $result") } // Unload C dynamic library dlclose(lib) } ``` 在上面的示例代码中,我们首先使用dlopen函数加载了一个C动态库,并获得了一个句柄。然后我们使用typealias定义了一个C函数的原型类型my_c_function_t,并使用CName注解指定了该C函数在动态库中的名称。接着我们使用external关键字和CName注解声明了一个Kotlin函数myCFunction,并指定了该函数的原型类型。最后我们可以直接调用该函数,以便调用C动态库中的函数。在函数调用结束后,我们使用dlclose函数关闭了动态库。 当然,这只是一个简单的示例。在实际应用中,可能需要更多的CInterop技巧来访问C结构体、使用C指针、处理C回调函数等等。但是,Kotlin/Native提供了丰富的工具和库来方便地与C代码交互,让我们可以更轻松地完成这些任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fundroid

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值