kotlin native 调用 C 动态库
准备环境
安装fedora31编译kotlin native创建 hello.h 头文件,在其中输入如下代码#ifndef HELLO_H #define HELLO_H void sayHello();#endif
创建hello.c文件,在其中输入如下代码
#include "hello.h"#include <stdio.h>void sayHello(){ printf("Hello Kotlin Native\n");}
编译hello.c,生成动态链接库
mkdir libgcc -shared -fPIC -o lib/libmyhello.so hello.c
创建hello.def文件
headers=hello.hheaderFilter=hello.hpackage=hello
linkerOpts = -L/tmp/kotlin/lib -lmyhello #如果不加这一行,使用kotlinc编译main.kt则需要加上 -linker-options '-L./lib -lmyhello'
```
执行如下命令用以分析hello.h文件,并自动生成kotlin定义
cinterop -def hello.def -compiler-option -I. -o hello
命令执行后的结果
创建main.kt文件
import hello.*fun main(args: Array<String>){sayHello()}
编译main.kt
kotlinc main.kt -library hello -o main
执行文件
https://github.com/plter/SimpleKotlinNativeCallCDemo
https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。