c

    Conrad Kramer

    6 months ago
    I have a few simple C files that I would like to compile and use with cinterop, and I have a few questions1. Is it possible to actually compile those C files as a part of my Gradle build? If so, what is the best way to go about it? 2. Assuming these C files compile to a static library and I include them in my Kotlin target, will they work on Android? Or do I need to write separate JNI bindings for Android, and then merge the interfaces into a cross-platform abstraction?
    Joakim Forslund

    Joakim Forslund

    6 months ago
    1. Yes, you create a cinterop setup as displayed here: https://kotlinlang.org/docs/native-app-with-c-and-libcurl.html#add-interoperability-to-the-build-process one thing should be noted, this will only work with native targets 2. They will work on android, but there is a lot of gotchas. You can't use the most heavy weight kotlin multiplatform libraries if you create a pure android native application (such as coroutines or atomicfu) or for obvious reasons, make use of the traditional android libraries. If you want to use jvm yes, your only choice here is to setup a separate JNI binding
    c

    Conrad Kramer

    6 months ago
    @Joakim Forslund in that example
    libcurl
    is not compiled as a part of the build, but rather the system
    libcurl
    is linked from Kotlin
    Joakim Forslund

    Joakim Forslund

    6 months ago
    It sure is, they get compiled as a part of the cinterop setup
    You will find the compiled files under build/cinterop, but maybe you mean something else.
    c

    Conrad Kramer

    6 months ago
    From the link:
    You need to have the
    curl
    library binaries on your system to make the sample work.
    It does not use Clang or GCC to compile
    libcurl
    as a part of the Gradle build
    Joakim Forslund

    Joakim Forslund

    6 months ago
    Yes? And you just want to compile some files, the setup is still the same. Put the c files into the cinterop folder, but without the need to setup the obvious curl linking or include
    Yeah, I'm not sure what you mean with Gradle build here.
    The cinterop binaries will be located in the build folder as a part of the normal gradle build process
    And the notion that it does not use clang to compile libcurl is just wrong
    c

    Conrad Kramer

    6 months ago
    Okay, put another way Inside of the
    def
    file,
    curl/curl.h
    is put into the
    headers
    field. If I have
    hello.c
    and
    hello.h
    , can I put a
    .c
    file into the
    headers
    field?
    Joakim Forslund

    Joakim Forslund

    6 months ago
    In that case your
    def
    file would contain
    package = hello
    headers = hello.h
    headerFilter = hello.h
    ---
    One way is to put your
    .c
    code under the
    ---
    in the def file
    If you want to keep your
    .c
    file intact, you simply pass the folder where the c files is located to compilerOpts
    c

    Conrad Kramer

    6 months ago
    I am able to get it working if the c code is below the
    ---
    , but I am unable to get it to work any other way
    It fails with
    CXError_ASTReadError
    if I put the
    .c
    file in the arguments
    I could use Gradle’s
    cpp-library
    plugin to do the compile, and then
    cinterop
    just for the headers
    As most examples I have seen online either a) bundle a static library (
    .a
    ) or b) link a system library
    Joakim Forslund

    Joakim Forslund

    6 months ago
    I have several projects there the folder of the c files gets passed in as compilerOpts. Try placing the
    .c
    and the
    .h
    file in the same folder i.e
    nativeInterop/cinterop/c
    . Keep the same def file
    c

    Conrad Kramer

    6 months ago
    actually, it worked if I just passed the
    .c
    file in the
    headers
    array
    Joakim Forslund

    Joakim Forslund

    6 months ago
    Sure, but interlinking issues might come from that
    At any case, what I wrote above works, that I know.
    c

    Conrad Kramer

    6 months ago
    Sweet, I will fiddle with it. Further question, if I want to use
    cinterop
    on Android, I assume I need to use
    androidNativeArm64
    and not just
    android
    ?
    Joakim Forslund

    Joakim Forslund

    6 months ago
    Yes
    c

    Conrad Kramer

    6 months ago
    Because the functions are not
    static
    (they are from an external library), I either get “symbol multiply defined” if I include the
    c
    file or “symbol not found” if I do not