Create an app using C Interop and libcurl – tutorial
This tutorial demonstrates how to use IntelliJ IDEA to create a command-line application. You'll learn how to create a simple HTTP client that can run natively on specified platforms using Kotlin/Native and the
The output will be an executable command-line app that you can run on macOS and Linux and make simple HTTP GET requests.
To get started, install the latest version of IntelliJ IDEA. The tutorial is suitable for both IntelliJ IDEA Community Edition and IntelliJ IDEA Ultimate.
Create a Kotlin/Native project
In IntelliJ IDEA, select File | New | Project.
In the panel on the left, select Kotlin | Native Application.
Specify the name and select the folder where you'll save your application.
Click Next and then Finish.
IntelliJ IDEA will create a new project with the files and folders you need to get you started. It's important to understand that an application written in Kotlin/Native can target different platforms if the code does not have platform-specific requirements. Your code is placed in a folder named
NativeMain with its corresponding
NativeTest. For this tutorial, keep the folder structure as is.
Along with your new project, a
build.gradle(.kts) file is generated. Pay special attention to the following in the build file:
Targets are defined using
mingwX64for macOS, Linux, and Windows. For a complete list of supported platforms, see the Kotlin Native overview.
The entry itself defines a series of properties to indicate how the binary is generated and the entry point of the applications. These can be left as default values.
C interoperability is configured as an additional step in the build. By default, all the symbols from C are imported to the
interoppackage. You may want to import the whole package in
.ktfiles. Learn more about how to configure it.
Create a definition file
When writing native applications, you often need access to certain functionalities that are not included in the Kotlin standard library, such as making HTTP requests, reading and writing from disk, and so on.
Kotlin/Native helps consume standard C libraries, opening up an entire ecosystem of functionality that exists for pretty much anything you may need. Kotlin/Native is already shipped with a set of prebuilt platform libraries, which provide some additional common functionality to the standard library.
An ideal scenario for interop is to call C functions as if you are calling Kotlin functions, following the same signature and conventions. This is when the
cinterop tool comes in handy. It takes a C library and generates the corresponding Kotlin bindings, so that the library can be used as if it were Kotlin code.
To generate these bindings, create a library definition
.def file that contains some information about the necessary headers. In this app, you'll need the
libcurl library to make some HTTP calls. To create a definition file:
srcfolder and create a new directory with File | New | Directory.
Name new directory nativeInterop/cinterop. This is the default convention for header file locations, though it can be overridden in the
build.gradlefile if you use a different location.
Select this new subfolder and create a new
libcurl.deffile with File | New | File.
Update your file with the following code: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
headersis the list of header files to generate Kotlin stubs. You can add multiple files to this entry, separating each with a
\on a new line. In this case, it's only
curl.h. The referenced files need to be relative to the folder where the definition file is, or be available on the system path (in this case, it's
headerFiltershows what exactly is included. In C, all the headers are also included when one file references another one with the
#includedirective. Sometimes it's not necessary, and you can add this parameter using glob patterns to fine-tune things.
headerFilteris an optional argument and is mostly used when the library is installed as a system library. You don't want to fetch external dependencies (such as system
stdint.hheader) into the interop library. It may be important to optimize the library size and fix potential conflicts between the system and the provided Kotlin/Native compilation environment.
The next lines are about providing linker and compiler options, which can vary depending on different target platforms. In this case, they are macOS (the
.osxsuffix) and Linux (the
.linuxsuffix). Parameters without a suffix are also possible (for example,
linkerOpts=) and applied to all platforms.
The convention is that each library gets its definition file, usually with the same name as the library. For more information on all the options available to
cinterop, see the Interop section.
Add interoperability to the build process
To use header files, make sure they are generated as a part of the build process. For this, add the following entry to the
The new lines are marked with
// NL. First,
cinterops is added, and then an entry for each
def file. By default, the name of the file is used. You can override this with additional parameters:
See the Interoperability with C section for more details on the available options.
Write the application code
Now you have the library and the corresponding Kotlin stubs and can use them from your application. For this tutorial, convert the simple.c example to Kotlin.
src/nativeMain/kotlin/ folder, update your
Main.kt file with the following code:
As you can see, explicit variable declarations are eliminated in the Kotlin version, but everything else is pretty much the same as the C version. All the calls you'd expect in the
libcurl library are available in the Kotlin equivalent.
Compile and run the application
Compile the application. To do that, run the following command in the terminal:./gradlew runDebugExecutableNative
In this case, the
cinteropgenerated part is implicitly included in the build.
If there are no errors during compilation, click the green Run icon in the gutter beside the
main()method or use the Alt+Enter shortcut to invoke the launch menu in IntelliJ IDEA.
IntelliJ IDEA opens the Run tab and shows the output — the contents of
You can see the actual output because the call
curl_easy_perform prints the result to the standard output. You could hide this using
For a complete example of using the
libcurl, see the libcurl sample of the Kotlin/Native project that shows how to abstract the code into Kotlin classes as well as display headers. It also demonstrates how to make the steps easier by combining them into a shell script or a Gradle build.