温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

kotlin native 调用C静态库

发布时间:2020-07-22 19:03:20 来源:网络 阅读:377 作者:757781091 栏目:编程语言

准备环境

  1. 安装fedora31
  2. 编译kotlin native
  3. 创建 hello.h 头文件,在其中输入如下代码
#ifndef HELLO_H 
#define HELLO_H 

void sayHello();

#endif
  1. 创建hello.c文件,在其中输入如下代码
#include "hello.h"
#include <stdio.h>

void sayHello()
{
    printf("Hello Kotlin Native\n");
}
  1. 编译hello.c,生成静态链接库
mkdir lib

gcc -c hello.c
ar rcs lib/libmyhello.a hello.o
  1. 创建hello.def文件

    headers=hello.h
    headerFilter=hello.h
    package=hello
    staticLibraries = libmyhello.a #静态库的名称
    libraryPaths= /tmp/kotlin/lib #静态库的搜索路径

    libmyhello.a会包含进同cinterop生成的hello.klib文件中

  2. 执行如下命令用以分析hello.h文件,并自动生成kotlin定义
cinterop -def hello.def -compiler-option -I. -o hello
  1. 命令执行后的结果
    kotlin native 调用C静态库
  2. 创建main.kt文件
import hello.*

fun main(args: Array<String>)
{
sayHello()
}
  1. 编译main.kt
    kotlinc main.kt -library hello  -o main
  2. 执行文件
    kotlin native 调用C静态库

  3. 参考链接
    https://github.com/plter/SimpleKotlinNativeCallCDemo
    https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI