We need an entry point and Kotlin sources. lib.klib – the compiled Kotlin/Native library to access API from lib.h.lib.def – the definitions for cinterop, reference to lib.h.To deal with memory management (not needed for our example). It bridges C types and Kotlin/Native types (trivial Int in our case) and helps Not it is the time to call the cinterop with the following options: cinterop -def lib.def -compilerOpts "-I $( pwd ) " -o lib.klib The file helps to specify all necessary options for bigger libraries. The cinterop tool uses the definition file for my library lib.def: headers = lib.h We need to import the C library to be used with Kotlin/Native. It will be a bit more complicated if you have several. c sources into a C static library: gcc -c "-I $( pwd ) " lib.c -o lib.o For that we call gcc to compile (and link) Now we compile the C sources into a C library. Let’s create a lib.c for implementation for the foo function in C: #include "lib.h" With Kotlin/Native cinterop tool import the function into Kotlin/Native. The code above declares one function for export int foo(). Thus you may shrink the file to lib.h: #ifndef LIB2_H_INCLUDED The extern "C" block is not needed (unless you use C++) and left here The lib.h file looks as follows #ifndef LIB2_H_INCLUDED On macOS, it is enough to install and run Xcode. You shall have C/C++ toolchain installed too. It should just work on Linux, but I did not check it. The rest of the post is created and tested on macOS. The konanc and cinterop tools should be in the PATH of your terminal or console. Or merely download the compiler from GitHub Releases page.īeware, the compiler will download the toolchain on the very first run. Let’s focus on the most trivial library example. I will writeĪ dedicated post with the solution for _isOSVersionAtLeast is undefined. Some time ago I stuck linking a C binary into an executable withĪnd thus I decided to simplify things to localize the problem.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |