溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

kotlin native 調(diào)用C靜態(tài)庫

發(fā)布時(shí)間:2020-07-22 19:03:20 來源:網(wǎng)絡(luò) 閱讀:458 作者:757781091 欄目:編程語言

準(zhǔn)備環(huán)境

  1. 安裝fedora31
  2. 編譯kotlin native
  3. 創(chuàng)建 hello.h 頭文件,在其中輸入如下代碼
#ifndef HELLO_H 
#define HELLO_H 

void sayHello();

#endif
  1. 創(chuàng)建hello.c文件,在其中輸入如下代碼
#include "hello.h"
#include <stdio.h>

void sayHello()
{
    printf("Hello Kotlin Native\n");
}
  1. 編譯hello.c,生成靜態(tài)鏈接庫
mkdir lib

gcc -c hello.c
ar rcs lib/libmyhello.a hello.o
  1. 創(chuàng)建hello.def文件

    headers=hello.h
    headerFilter=hello.h
    package=hello
    staticLibraries = libmyhello.a #靜態(tài)庫的名稱
    libraryPaths= /tmp/kotlin/lib #靜態(tài)庫的搜索路徑

    libmyhello.a會(huì)包含進(jìn)同cinterop生成的hello.klib文件中

  2. 執(zhí)行如下命令用以分析hello.h文件,并自動(dòng)生成kotlin定義
cinterop -def hello.def -compiler-option -I. -o hello
  1. 命令執(zhí)行后的結(jié)果
    kotlin native 調(diào)用C靜態(tài)庫
  2. 創(chuàng)建main.kt文件
import hello.*

fun main(args: Array<String>)
{
sayHello()
}
  1. 編譯main.kt
    kotlinc main.kt -library hello  -o main
  2. 執(zhí)行文件
    kotlin native 調(diào)用C靜態(tài)庫

  3. 參考鏈接
    https://github.com/plter/SimpleKotlinNativeCallCDemo
    https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI