我对Kotlin Native比较陌生,为了了解更多,我正在学习这两个教程,一个来自JetBrains官方文档,另一个来自jonnyzzz博客,这两个教程都侧重于使用C Interop和libcurl创建应用程序:
- https://kotlinlang.org/docs/native-app-with-c-and-libcurl.html
- https://jonnyzzz.com/blog/2018/10/29/kn-libcurl-windows/
我试图在Windows 11上构建上述应用程序。
经过一番努力之后,我终于设法将libcurl库导入到我的Kotlin Native项目中,从而可以调用相关的C函数。到目前为止,一切顺利,但当我试图构建整个项目时,我收到了以下错误:
e: C:\Users\Nicola\.konan\dependencies\llvm-11.1.0-windows-x64-essentials/bin/clang++ invocation reported errors
The C:\Users\Nicola\.konan\dependencies\llvm-11.1.0-windows-x64-essentials/bin/clang++ command returned non-zero exit code: 1.
output:
lld-link: error: undefined symbol: __declspec(dllimport) curl_easy_strerror
>>> referenced by C:\Users\Nicola\IdeaProjects\SimpleHttpClient\src\nativeMain\kotlin\Main.kt:15
>>> C:\Users\Nicola\AppData\Local\Temp\konan_temp9458678904101419787\result.o:(libcurl_curl_easy_strerror_wrapper33)
lld-link: error: undefined symbol: __declspec(dllimport) curl_easy_init
>>> referenced by C:\Users\Nicola\IdeaProjects\SimpleHttpClient\src\nativeMain\kotlin\Main.kt:15
>>> C:\Users\Nicola\AppData\Local\Temp\konan_temp9458678904101419787\result.o:(libcurl_curl_easy_init_wrapper36)
lld-link: error: undefined symbol: __declspec(dllimport) curl_easy_perform
>>> referenced by C:\Users\Nicola\IdeaProjects\SimpleHttpClient\src\nativeMain\kotlin\Main.kt:15
>>> C:\Users\Nicola\AppData\Local\Temp\konan_temp9458678904101419787\result.o:(libcurl_curl_easy_perform_wrapper37)
lld-link: error: undefined symbol: __declspec(dllimport) curl_easy_cleanup
>>> referenced by C:\Users\Nicola\IdeaProjects\SimpleHttpClient\src\nativeMain\kotlin\Main.kt:15
>>> C:\Users\Nicola\AppData\Local\Temp\konan_temp9458678904101419787\result.o:(libcurl_curl_easy_cleanup_wrapper38)
lld-link: error: undefined symbol: curl_easy_setopt
>>> referenced by C:\Users\Nicola\AppData\Local\Temp\konan_temp9458678904101419787\result.o:(knifunptr_libcurl39_curl_easy_setopt)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':linkDebugExecutableNative'.
> Compilation finished with errors
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 8s
这是我写的代码
- 主目录:**
import libcurl.*
import kotlinx.cinterop.*
fun main(args: Array<String>) {
val curl = curl_easy_init()
if (curl != null) {
curl_easy_setopt(curl, CURLOPT_URL, "http://jonnyzzz.com")
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L)
val res = curl_easy_perform(curl)
if (res != CURLE_OK) {
println("curl_easy_perform() failed ${curl_easy_strerror(res)?.toKString()}")
}
curl_easy_cleanup(curl)
}
}
- 库卷定义:**
headers = curl/curl.h
headerFilter = curl/*
- 版本.分级. kts:**
plugins {
kotlin("multiplatform") version "1.7.10"
}
group = "me.nicola"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
kotlin {
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
val nativeTarget = when {
hostOs == "Mac OS X" -> macosX64("native")
hostOs == "Linux" -> linuxX64("native")
isMingwX64 -> mingwX64("native")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
nativeTarget.apply {
compilations.getByName("main") {
cinterops {
val libcurl by creating {
val includePath = "C:\\Users\\Nicola\\Documents\\curl-7.87.0\\curl-7.87.0\\include"
defFile(project.file("src/nativeInterop/cinterop/libcurl.def"))
packageName("libcurl")
compilerOpts("-I/$includePath")
includeDirs.allHeaders(includePath)
}
}
}
binaries {
executable {
val buildPath = "C:\\Users\\Nicola\\Documents\\curl-7.87.0\\curl-7.87.0\\builds\\libcurl-vc-x86-release-dll-ipv6-sspi-schannel\\lib\\libcurl.lib"
entryPoint = "main"
linkerOpts(buildPath)
}
}
}
sourceSets {
val nativeMain by getting
val nativeTest by getting
}
}
我错过了什么?
非常感谢您宝贵的时间。
1条答案
按热度按时间bcs8qyzn1#
在Ktor项目中有一个关于如何链接Curl的更完整的演示a-尽管它可能包含比您需要的更多的配置。
它说“这个程序需要
libcurl.a
库。”它将在哪里查找该文件?在-L
提供的任何路径上,所以您还必须将-L/dir/that/contains/libraries
添加到您的.def
文件中。或者,您可以静态链接Curl,然后Kotlin/Native编译器将自动链接该库。
请注意,如果
libcurl.a
是为错误的平台编译的,或者是使用不兼容的gcc或libc版本(because Kotlin/Native uses old versions of gcc and libc)编译的,也可能会出现链接错误。