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://hxwzsj.com/article/pjhggg.html

其他资讯

Copyright © 2025 青羊区翔捷宏鑫字牌设计制作工作室(个体工商户) All Rights Reserved 蜀ICP备2025123194号-14
友情链接: 外贸网站建设 成都网站设计 成都网站设计 营销型网站建设 成都营销网站建设 重庆网站制作 手机网站设计 重庆网站建设 网站建设 成都网站设计公司 成都网站设计 重庆企业网站建设 网站设计 成都营销网站制作 定制网站设计 企业网站设计 企业网站建设 成都网站建设 定制网站制作 网站制作 成都定制网站建设 网站设计制作报价