kotlinnative调用C静态库

准备环境

创新互联公司成立于2013年,先为紫阳等服务建站,紫阳等地企业,进行企业商务咨询服务。为紫阳企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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

void sayHello();

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

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)
{
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

网页名称:kotlinnative调用C静态库
文章分享:http://bzwzjz.com/article/pjhggg.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都响应式网站建设 网站建设公司 成都网站设计 H5网站制作 重庆网站制作 成都网站制作 成都网站设计 移动网站建设 网站制作 app网站建设 公司网站建设 成都营销网站建设 网站设计 定制网站设计 网站制作公司 外贸网站设计方案 网站设计 网站制作公司 重庆网站建设 营销网站建设 成都网站建设推广 广安网站设计