Is there any easy way to create the `.def` file fo...
# kotlin-native
m
Is there any easy way to create the
.def
file for cinterop? I've tried using
pkg-config --cflags <libname>
for
compilerOpts
and
pkg-config --libs <libname>
for
linkerOpts
but I end up with an undefined macro when I reload Gradle. Is there something I should do different?
It might also be caused by the fact I've experimented a lot with my setup and might have broken something 😅
l
Not really. You need to specify the library path directly. Apparently the current recommendation is to not link with system libraries, but build dedicated versions of the dependencies and specify the path explicitly in the def file.
At least that was what I was told when I reported a bug relating to using native libraries.
m
They wrote something really different in the docs, wondering why they told you that
Only that would make sense to me would be building the library and keeping it just as a resource for building the `.klib`s, not bundling it.
But from what you've sent here, they apparently didn't mean it like that
plus this statement
m
Also, what you've sent doesn't seem to be my problem too, both the issue you've sent and the one that is mentioned there end up with an error about shared libraries, while I do get a stacktrace about a header file:
Copy code
Exception in thread "main" java.lang.Error: /usr/include/x86_64-linux-gnu/sys/cdefs.h:146:34: error: function-like macro '__glibc_clang_prereq' is not defined
	at org.jetbrains.kotlin.native.interop.indexer.UtilsKt.ensureNoCompileErrors(Utils.kt:275)
	at org.jetbrains.kotlin.native.interop.indexer.IndexerKt.indexDeclarations(Indexer.kt:1246)
	at org.jetbrains.kotlin.native.interop.indexer.IndexerKt.buildNativeIndexImpl(Indexer.kt:1229)
	at org.jetbrains.kotlin.native.interop.indexer.IndexerKt.buildNativeIndexImpl(Indexer.kt:1225)
	at org.jetbrains.kotlin.native.interop.gen.jvm.DefaultPlugin.buildNativeIndex(Plugins.kt:33)
	at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.processCLib(main.kt:311)
	at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.processCLibSafe(main.kt:243)
	at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.access$processCLibSafe(main.kt:1)
	at org.jetbrains.kotlin.native.interop.gen.jvm.Interop.interop(main.kt:101)
	at org.jetbrains.kotlin.cli.utilities.InteropCompilerKt.invokeInterop(InteropCompiler.kt:47)
	at org.jetbrains.kotlin.cli.utilities.MainKt.mainImpl(main.kt:23)
	at org.jetbrains.kotlin.cli.utilities.MainKt.main(main.kt:45)
Any ideas?
l
Me? No. What does your def file look like?
Also the gradle file.
m
libgtk.def
Copy code
headers = gtk/gtk.h
package = libgtk

# pkg-config gtk4 --cflags
compilerOpts.linux = \
    -I/usr/include/gtk-4.0 \
    -I/usr/include/pango-1.0 \
    -I/usr/include/glib-2.0 \
    -I/usr/lib/x86_64-linux-gnu/glib-2.0/include \
    -I/usr/include/harfbuzz \
    -I/usr/include/freetype2 \
    -I/usr/include/libpng16 \
    -I/usr/include/libmount \
    -I/usr/include/blkid \
    -I/usr/include/fribidi \
    -I/usr/include/cairo \
    -I/usr/include/pixman-1 \
    -I/usr/include/gdk-pixbuf-2.0 \
    -I/usr/include/x86_64-linux-gnu \
    -I/usr/include/graphene-1.0 \
    -I/usr/lib/x86_64-linux-gnu/graphene-1.0/include \
    -mfpmath=sse \
    -msse \
    -msse2 \
    -pthread

# pkg-config gtk4 --libs
linkerOpts.linux = \
    -lgtk-4 \
    -lpangocairo-1.0 \
    -lpango-1.0 \
    -lharfbuzz \
    -lgdk_pixbuf-2.0 \
    -lcairo-gobject \
    -lcairo \
    -lgraphene-1.0 \
    -lgio-2.0 \
    -lgobject-2.0 \
    -lglib-2.0
build.gradle.kts
Copy code
plugins {
    kotlin("multiplatform") version "1.9.22"
}

group = "dev.matytyma"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

kotlin {
    val hostOs = System.getProperty("os.name")
    val isArm64 = System.getProperty("os.arch") == "aarch64"
    val nativeTarget = when {
        hostOs == "Linux" && isArm64 -> linuxArm64("native")
        hostOs == "Linux" && !isArm64 -> linuxX64("native")
        else -> throw GradleException("Host OS is not supported by Kotlin GTK.")
    }

    nativeTarget.apply {
        compilations.getByName("main") {
            cinterops {
                val libgtk by creating
            }
        }
        binaries {
            executable {
                entryPoint = "main"
            }
        }
    }
    sourceSets {
        val nativeMain by getting
        val nativeTest by getting
    }
}
@loke Could you please try on your machine and see if it throws the same error for you? Thanks!
l
What error does it throw for you?
l
Sorry, I'm not sure what the problem could be. It seems to be an issue of compatibility between the system header files and the ones shipped in the kotlin native compiler (it comes with its own compile environment)
m
If it is like you think, should I report that as an issue or is there actually nothing I can do about it?
l
Sorry, I don't use slack very often so I didn't see your message.
I have run out of ideas, so perhaps it is a bug. You could try reporting it.
m
No prob with not responding, I'm also much more active on other platforms than Slack. And ye... I was wondering about it yesterday 😄