Kotlin Native libcurl example on Windows
Asked Answered
P

5

6

I'm trying to learn how Kotlin Native works with this tutorial from JetBrains: https://kotlinlang.org/docs/tutorials/native/interop-with-c.html

But these tutorials are only fro macOS and Linux. Sometimes i use a windows computer and i want to know how it works there.

How can i add the libcurl library in windows and how can i add it to the libcurl.def file?

It isn't explained anywhere.

Popple answered 18/10, 2018 at 9:31 Comment(0)
S
3

You need to install MSYS2, then in MSYS2 console install libcurl:

$ pacman -S mingw-w64-x86_64-curl

See https://github.com/JetBrains/kotlin-native/pull/1499

Style answered 18/10, 2018 at 11:30 Comment(2)
github.com/JetBrains/kotlin-native/pull/… Here my actual issue.Reverberatory
Any suggestion for changes required for targeting x32?Boron
I
3

A more detailed step-by-step explanation on how to use dynamically built libcurl (aka DLL) with Kotlin/Native.

https://jonnyzzz.com/blog/2018/10/29/kn-libcurl-windows/

Indole answered 29/10, 2018 at 21:22 Comment(0)
I
2

I do not think it is covered somewhere right now. The idea is still the same, and you should be able to make it work. The first step is to download and build libcurl locally from https://curl.haxx.se/libcurl/. There are instructions and documentation on how to build it. I do recommend to pick the Windows own implementation for SSL.

Once you are done with building you will have both the includes and the library. Note, on Windows, you will always have a static library. Even if you build curl as a dynamic library (DLL), you will have a generated static library that you use for linking. DLL is easier to build and use. A true static library is better because you will have no dependencies in your Kotlin/Native program binary. At that point, the tutorial instructions should work.

Indole answered 18/10, 2018 at 10:56 Comment(2)
Where should i place the static library file? In linux it is on the compiler libs path i think. Am i right? In addition, for linux and mac there are special linkerOpts arguments. What should i do for windows here?Reverberatory
just include the path to the linkerOptsIndole
M
2

For Windows, if you need only basic HTTP communication, you can also try WinINet API which is a part of Windows and so no dependency is necessary. No hassle with the libcurl library.

See my example of how to use WinInet API with Kotlin MPP: https://github.com/localazy/kotlin-mpp-wininet

For macOS and Linux, instead of going through the hassle with libcurl manually, just use ktor clients: https://ktor.io/clients/index.html

Mauer answered 4/9, 2020 at 22:24 Comment(0)
N
-1

I'm studying Kotlin too and it took many hours until I realize how to handle def file, includes and static library.

I made an example of how to use static library (curl with gzip and SSL support compiled with mingw) on kotlin-native. This way you dont need to dll files to be supplied with your app

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

On libcurl.def file you can see:

headers = curl/curl.h   // path to curl header
libraryPaths = src/nativeInterop/cinterop  // path to your static library
staticLibraries = libcurl.a // the static library
linkerOpts.mingw = -lws2_32 -lwldap32 // linking dependences

I based on the nice post of jonnyzzz:

https://jonnyzzz.com/blog/2018/10/29/kn-libcurl-windows/

Nature answered 27/11, 2020 at 19:47 Comment(1)
You should've write it as a comment.Pungy

© 2022 - 2024 — McMap. All rights reserved.