How to use a newer linker and glibc in a Kotlin/Native project?

28 views Asked by At

I want to use GLFW in my Kotlin/Native project so I created a .def file at src/nativeInterop/cinterop/glfw3.def:

headers = \
    glfw3.h \
    glfw3native.h
headerFilter = /usr/include/GLFW/*

compilerOpts.linux = \
    -I/usr/include \
    -I/usr/include/GLFW
linkerOpts.linux = \
    -L/usr/lib \
    -lc \
    -l:libglfw.so.3

and updated my build.gradle.kts accordingly:

plugins {
    kotlin("multiplatform") version "1.9.23"
}

// -- snip --

kotlin {
    linuxX64("native").apply {
        val main by compilations.getting
        compilations.getByName("main") {

            cinterops {
                val glfw3 by creating {
                    defFile(project.file("src/nativeInterop/cinterop/glfw3.def"))
                    packageName("glfw3")
                }
            }
        }
        binaries {
            executable()
        }
    }

    sourceSets {
        val nativeMain by getting
        val nativeTest by getting
    }
}

But when I try to run this on my Arch Linux x86_64 system, I get the following error:

> Task :linkDebugExecutableNative
e: /home/simao/.konan/dependencies/x86_64-unknown-linux-gnu-gcc-8.3.0-glibc-2.19-kernel-4.9-2/x86_64-unknown-linux-gnu/bin/ld.gold invocation reported errors

The /home/simao/.konan/dependencies/x86_64-unknown-linux-gnu-gcc-8.3.0-glibc-2.19-kernel-4.9-2/x86_64-unknown-linux-gnu/bin/ld.gold command returned non-zero exit code: 1.
output:
/usr/lib/libglfw.so.3: error: undefined reference to 'memfd_create', version 'GLIBC_2.27'
/usr/lib/libglfw.so.3: error: undefined reference to 'dlopen', version 'GLIBC_2.34'
/usr/lib/libglfw.so.3: error: undefined reference to 'pthread_key_delete', version 'GLIBC_2.34'
/usr/lib/libglfw.so.3: error: undefined reference to 'dlclose', version 'GLIBC_2.34'
/usr/lib/libglfw.so.3: error: undefined reference to 'powf', version 'GLIBC_2.27'
/usr/lib/libglfw.so.3: error: undefined reference to 'dlsym', version 'GLIBC_2.34'
/usr/lib/libglfw.so.3: error: undefined reference to 'pthread_setspecific', version 'GLIBC_2.34'
/usr/lib/libglfw.so.3: error: undefined reference to 'pthread_getspecific', version 'GLIBC_2.34'
/usr/lib/libglfw.so.3: error: undefined reference to 'pthread_key_create', version 'GLIBC_2.34'

> Task :linkDebugExecutableNative FAILED
4 actionable tasks: 2 executed, 2 up-to-date

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':linkDebugExecutableNative'.
> Compilation finished with errors

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 3s
4:47:17 PM: Execution finished 'runDebugExecutableNative'.

More logs:

/usr/lib/libmvec.so.1: error: undefined reference to 'exp10f', version 'GLIBC_2.32'
/usr/lib/libmvec.so.1: error: undefined reference to 'exp10', version 'GLIBC_2.39'
/usr/lib/libmvec.so.1: error: undefined reference to 'pow', version 'GLIBC_2.29'
/usr/lib/libmvec.so.1: error: undefined reference to 'powf', version 'GLIBC_2.27'
/usr/lib/libmvec.so.1: error: undefined reference to 'log2', version 'GLIBC_2.29'
/usr/lib/libmvec.so.1: error: undefined reference to 'log2f', version 'GLIBC_2.27'
/usr/lib/libmvec.so.1: error: undefined reference to 'logf', version 'GLIBC_2.27'
/usr/lib/libmvec.so.1: error: undefined reference to 'log', version 'GLIBC_2.29'
/usr/lib/libmvec.so.1: error: undefined reference to 'expf', version 'GLIBC_2.27'
/usr/lib/libmvec.so.1: error: undefined reference to 'exp', version 'GLIBC_2.29'
/usr/lib/libmvec.so.1: error: undefined reference to 'exp2f', version 'GLIBC_2.27'
/usr/lib/libmvec.so.1: error: undefined reference to 'exp2', version 'GLIBC_2.29'
/usr/lib/libmvec.so.1: error: undefined reference to 'hypot', version 'GLIBC_2.35'
/usr/lib/libmvec.so.1: error: undefined reference to 'hypotf', version 'GLIBC_2.35'

My code only contains this, so far:

fun main() {

}

Looking at this, it leads me to believe that the toolchain used (/home/simao/.konan/dependencies/x86_64-unknown-linux-gnu-gcc-8.3.0-glibc-2.19-kernel-4.9-2/x86_64-unknown-linux-gnu/) is too old because it says glibc-2.19 in the name but the library GLFW is using symbols from Glibc 2.27 and Glibc 3.34). I would also be fine with a solution that involves statically linking musl as long as it works fine with GLFW. I also don't understand why Kotlin/Native (or konan) uses GCC when Kotlin/Native is said to be using LLVM. So why not use Clang instead?

What I would like to do here is either update the entire toolchain, use a different one or update just the glibc but without having to use something like -l:/usr/lib/libc.so.6. How do I do that?

0

There are 0 answers