如何添加 MinGW Kotlin Native 依赖项
How do I add MinGW Kotlin Native dependencies
我正在尝试创建一个能够与 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 上不可用:/
Kotlin/Native 不是[1] interoperable with C++ at this moment. You can however create C wrapper for any C++ library and it's functions from Kotlin/Native[2].
使用多平台gradle插件时,您可以使用以下语法定义本机互操作:
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
我正在尝试创建一个能够与 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 上不可用:/
Kotlin/Native 不是[1] interoperable with C++ at this moment. You can however create C wrapper for any C++ library and it's functions from Kotlin/Native[2].
使用多平台gradle插件时,您可以使用以下语法定义本机互操作:
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