【问题标题】:How do I add MinGW Kotlin Native dependencies如何添加 MinGW Kotlin Native 依赖项
【发布时间】:2019-01-09 07:42:13
【问题描述】:

我正在尝试创建一个能够与 AWS IoT 服务通信的小型应用程序。因为我希望它相当小并且我想尝试一些新的东西,所以我决定选择 Kotlin Native。我很快注意到 AWS 发布了他们的 C++ 库,可以让你轻松连接到 AWS IoT 服务 (https://github.com/aws/aws-iot-device-sdk-cpp/tree/release) 我下载了它,甚至设法用 MinGW 编译(是的,我在 Windows 上)。我注意到它生成了一堆 *.o 文件。我认为现在是将其导入我的 Kotlin Native 项目的合适时机。我的 build.gradle 文件现在看起来完全标准

plugins {
    id 'kotlin-multiplatform' version '1.3.11'
}
repositories {
    mavenCentral()
}
kotlin {
    targets {
        // For ARM, preset should be changed to presets.iosArm32 or presets.iosArm64
        // For Linux, preset should be changed to e.g. presets.linuxX64
        // For MacOS, preset should be changed to e.g. presets.macosX64
        fromPreset(presets.mingwX64, 'mingw')

        configure([mingw]) {
            // Comment to generate Kotlin/Native library (KLIB) instead of executable file:
            compilations.main.outputKinds('EXECUTABLE')
            // Change to specify fully qualified name of your application's entry point:
            compilations.main.entryPoint = 'sample.main'
        }
    }
    sourceSets {
        // Note: To enable common source sets please comment out 'kotlin.import.noCommonSourceSets' property
        // in gradle.properties file and re-import your project in IDE.
        mingwMain {
        }
        mingwTest {
        }
    }
}

task runProgram {
    def buildType = 'release' // Change to 'debug' to run application with debug symbols.
    dependsOn "link${buildType.capitalize()}ExecutableMingw"
    doLast {
        def programFile = kotlin.targets.mingw.compilations.main.getBinary('EXECUTABLE', buildType)
        exec {
            executable programFile
            args ''
        }
    }
}

由于某种原因,我找不到任何关于如何添加我新编译的依赖项的示例。通常,当您编写 C++ 代码时,您必须分别指定 Include 目录和 Lib 目录的路径。 AFAIK 这不是 gradle 开箱即用的东西。那我该如何导入这个依赖呢?或者也许有一些集中的存储库我可以简单地从中提取这种依赖关系,就像现在仍在使用的几乎所有其他编程语言一样?至少这个特定的库似乎在 NuGet 上不可用:/

【问题讨论】:

    标签: c++ gradle kotlin-native


    【解决方案1】:

    Kotlin/Native 目前无法[1] 与 C++ 互操作。但是,您可以为任何 C++ 库创建 C 包装器,它的函数来自 Kotlin/Native[2]。

    当使用multiplatform gradle plugin时,您可以使用以下语法定义本机互操作:

    kotlin {
        linuxX64 { // Replace with a target you need.
            compilations.main {
                cinterops {
                    myInterop {
                        // Def-file describing the native API.
                        // The default path is src/nativeInterop/cinterop/<interop-name>.def
                        defFile project.file("def-file.def")
    
                        // Package to place the Kotlin API generated.
                        packageName 'org.sample'
    
                        // Options to be passed to compiler by cinterop tool.
                        compilerOpts '-Ipath/to/headers'
    
                        // Directories for header search (an analogue of the -I<path> compiler option).
                        includeDirs.allHeaders("path1", "path2")
    
                        // Additional directories to search headers listed in the 'headerFilter' def-file option.
                        // -headerFilterAdditionalSearchPrefix command line option analogue.
                        includeDirs.headerFilterOnly("path1", "path2")
    
                        // A shortcut for includeDirs.allHeaders.
                        includeDirs("include/directory", "another/directory")
                    }
    
                    anotherInterop { /* ... */ }
                }
            }
        }
    }
    

    如果您只定义互操作名称,插件将在src/nativeInterop/cinterop/ 目录中查找.def 文件[3] 并使用它(在本例中为src/nativeInterop/cinterop/myInterop.def)。

    kotlin {
        linuxX64 { 
            compilations.main {
                cinterops {
                    myInterop {
                    }
                }
            }
         }
    }
    

    .def 文件[3] 包含有关您尝试使用的库的信息,通常如下所示。

    headers = png.h
    headerFilter = png.h
    package = png
    

    更多关于cinterop的信息:https://kotlinlang.org/docs/reference/native/c_interop.html

    更多关于多平台项目的信息:https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      • 2019-04-28
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      相关资源
      最近更新 更多