Search code examples
openglkotlinkotlin-native

Kotlin/Native OpenGL interop


I'm trying to setup kotlin/native project that utilizes OpenGL C libraries.

OS: ArchLabs, linux 5.1.15 (shares repositories with arch)

Packages installed: glu, glew, freeglut, glfw

In my main() there's only one function called (it's copied from samples):

glutInit(argc.ptr, null)

There was no out of the box support for OpenGL in my project, so I decided to make opengl.def:

package = platform.OpenGL
headers = GL/glut.h

compilerOpts = -I/usr/include
$ ls /usr/include/GL  
freeglut_ext.h  glcorearb.h  gl.h         glu_mangle.h  glxext.h  glx_mangle.h  glxtokens.h  wglew.h
freeglut.h      glew.h       gl_mangle.h  glut.h        glx.h     glxmd.h       internal
freeglut_std.h  glext.h      glu.h        glxew.h       glxint.h  glxproto.h    osmesa.h

And here's my gradle.build.kts:

plugins {
    id("org.jetbrains.kotlin.multiplatform") version "1.3.41"
}

repositories {
    mavenCentral()
}

kotlin {
    linuxX64("opengl") {
        val main by compilations.getting
        val opengl by main.cinterops.creating

        binaries {
            executable {
                entryPoint = "opengl.main"
            }
        }
    }
}

There is a .kt file generated: build/classes/.../OpenGL/OpenGL.kt which contains definition of glutInit function (well, more of a reference I guess)

And here's the output of runReleaseExecutableOpengl

> Configure project :
Kotlin Multiplatform Projects are an experimental feature.

> Task :cinteropOpenglOpengl

> Task :linkReleaseExecutableOpengl
/home/Opengl/.konan/dependencies/clang-llvm-6.0.1-linux-x86-64/bin/ld.lld: error: undefined symbol: glutInit
>>> referenced by ld-temp.o
>>>               /tmp/konan_temp5065866915785286367/combined.o:(platform_OpenGL_kniBridge520)
e: /home/Opengl/.konan/dependencies/clang-llvm-6.0.1-linux-x86-64/bin/ld.lld invocation reported errors

> Task :linkReleaseExecutableOpengl FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':linkReleaseExecutableOpengl'.

> Process 'command '/usr/lib/jvm/java-11-openjdk/bin/java'' finished with non-zero exit value 1

Is there a way to fix it? My best guess is that I have to have mingw-w64-* packages installed, for example mingw-w64-freeglut. Is that the case? It could also be that I'm pointing to the wrong headers (I'm not really into C yet and it's been a long time since I used C++) and it can't find the implementation of these headers.

Thanks in advance!


Solution

  • You need linkerOpts = -L/usr/lib -lglut in the def file, to dynamically link against freeglut.