Kotlin/Native Windows cinterop 使用内置库 dll

Kotlin/Native Windows cinterop using built library dll

我正在使用 Windows 来遵循此处的 Kotlin-Native libcurl 示例 https://kotlinlang.org/docs/tutorials/native/curl.html

不幸的是,这是一个比我想象的更大的挑战。我一直在学习如何使用 CMake 等工具从源代码在 windows 上编译 C 二进制文件,并按照本教程在 Windows 上构建 libcurl:https://jonnyzzz.com/blog/2018/10/29/kn-libcurl-windows/

在 Windows 上对 cinterop 的 .def 文件进行了几天的反复试验,并使用 compilerOpts 指向我构建库中 curl 的头文件后,我遇到了麻烦.

我似乎无法 Gradle 识别 dll 文件。任务 ':linkDebugExecutableMingw' 一直失败。我不知道我应该把 dll 文件放在哪里。我试过把它放在exe旁边的构建输出文件夹中,但它仍然无法编译,并出现以下错误:

C:\Users\yous\.konan\dependencies\msys2-mingw-w64-x86_64-clang-llvm-lld-compiler_rt-8.0.1\bin\ld: C:\Users\yous\AppData\Local\Temp\konan_temp6326583690522756621\result.o:out:(.rdata$.refptr.knifunptr_sample35_curl_easy_strerror[.refptr.knifunptr_sample35_curl_easy_strerror]+0x0): undefined reference to `knifunptr_sample35_curl_easy_strerror'

和头文件错误列表类似,那么最后的信息是:

e: C:\Users\yabde\.konan\dependencies\msys2-mingw-w64-x86_64-clang-llvm-lld-compiler_rt-8.0.1/bin/clang++ invocation reported errors

这是我成功找到头文件后的结果。我认为这是未包含 dll 文件的问题。

这是我的相关 Gradle 构建脚本:

mingwX64("mingw") {
        compilations.main {
            cinterops {
                libcurl {
                    // Def-file describing the native API.
                    // The default path is src/nativeInterop/cinterop/<interop-name>.def
                    defFile project.file("src/nativeInterop/cinterop/libcurl.def")

                    // Package to place the Kotlin API generated.
                    packageName 'libcurl'

                    // Options to be passed to compiler by cinterop tool.
                    compilerOpts '-Isrc\nativeInterop\cinterop\libcurl-vc-x64-release-dll-ipv6-sspi-winssl\include -o libcurl'
                    //linkerOpts '-Isrc\nativeInterop\cinterop\libcurl-vc-x64-release-dll-ipv6-sspi-winssl\bin'

                    // 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("src/nativeInterop/cinterop/libcurl-vc-x86-release-dll-ipv6-sspi-winssl/include")

                    // A shortcut for includeDirs.allHeaders.
                    //includeDirs("include/directory", "another/directory")
                }
            }
        }
        binaries {
            executable {
                // Change to specify fully qualified name of your application's entry point:
                entryPoint = 'sample.main'
                // Specify command-line arguments, if necessary:
                //runTask?.args('-Lsrc/nativeInterop/cinterop/libcurl-vc-x86-release-dll-ipv6-sspi-winssl/bin')
                runTask?.args('')
            }
        }
    }

我认为这个问题可能与定位 dll 的链接器选项有关,但取消注释 linkerOpts 行没有帮助。

我想我已经放弃 Kotlin/Native 并且会坚持使用 C 来进行 data-oriented 本机编程...

编辑: 另外,请注意,任何被注释掉的文字都是我尝试过的 好的,这是我的 def 文件:

headers = curl/curl.h
headerFilter = curl/*

compilerOpts.linux = -I/usr/include -I/usr/include/x86_64-linux-gnu

linkerOpts.osx = -L/opt/local/lib -L/usr/local/opt/curl/lib -lcurl
linkerOpts.linux = -L/usr/lib/x86_64-linux-gnu -lcurl
linkerOpts.mingw = -Llibcurl-vc-x86-release-dll-ipv6-sspi-winssl/lib

好的,这是我的项目结构:

在寻找将 libcurl 与我的二进制文件静态链接的解决方案时,我多次遇到这个问题。

最后,我在 Windows 上使用 WinInet 进行 HTTP 通信。 Linux 和 macOS 二进制文件仍然依赖于带有 libcurl 的 ktor 客户端。

请参阅我关于如何将 WinInet API 与 Kotlin MPP 结合使用的示例:https://github.com/localazy/kotlin-mpp-wininet

对于简单的 HTTP 通信,WinInet API 就足够了,生成的二进制文件要小得多,在我的例子中,它没有额外的依赖项。

我遵循了相同的步骤(jonyzzz 博客)。我为 Kotlin-Native 创建了一个 Libcurl 静态示例。支持 gzip 和 SSL。编译为独立的 exe,不需要 dll

https://github.com/carlosrafp/Libcurl-Kotlin-Native-standalone

我使用 mingw64(gzip 和 libcurl)和 msys2/mingw (openssl)

构建了存储库中提供的静态库