如何在Windows上构建libcurlKotlinNative应用程序?

zd287kbt  于 2023-02-05  发布在  Kotlin
关注(0)|答案(1)|浏览(118)

我对Kotlin Native比较陌生,为了了解更多,我正在学习这两个教程,一个来自JetBrains官方文档,另一个来自jonnyzzz博客,这两个教程都侧重于使用C Interop和libcurl创建应用程序:

  1. https://kotlinlang.org/docs/native-app-with-c-and-libcurl.html
  2. 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
    }
}

我错过了什么?
非常感谢您宝贵的时间。

bcs8qyzn

bcs8qyzn1#

在Ktor项目中有一个关于如何链接Curl的更完整的演示a-尽管它可能包含比您需要的更多的配置。

# libcurl.def 

linkerOpts.mingw_x64 =       -lcurl \
                             -L/usr/lib64 \
                             -L/usr/lib/x86_64-linux-gnu \
                             -L/opt/local/lib \
                             -L/usr/local/opt/curl/lib \
                             -L/opt/homebrew/opt/curl/lib \
                             -LC:/msys64/mingw64/lib \
                             -LC:/Tools/msys64/mingw64/lib \
                             -LC:/Tools/msys2/mingw64/lib

它说“这个程序需要libcurl.a库。”它将在哪里查找该文件?在-L提供的任何路径上,所以您还必须将-L/dir/that/contains/libraries添加到您的.def文件中。
或者,您可以静态链接Curl,然后Kotlin/Native编译器将自动链接该库。

# libcurl.def 

staticLibraries = libcurl.a

# -lcurl is not needed

linkerOpts.mingw_x64 =       -L/usr/lib64 \
                             -L/usr/lib/x86_64-linux-gnu \
                             -L/opt/local/lib \
                             -L/usr/local/opt/curl/lib \
                             -L/opt/homebrew/opt/curl/lib \
                             -LC:/msys64/mingw64/lib \
                             -LC:/Tools/msys64/mingw64/lib \
                             -LC:/Tools/msys2/mingw64/lib

请注意,如果libcurl.a是为错误的平台编译的,或者是使用不兼容的gcc或libc版本(because Kotlin/Native uses old versions of gcc and libc)编译的,也可能会出现链接错误。

相关问题