What is required to get code completion to work in...
# kotlin-native
n
What is required to get code completion to work in IntelliJ for a C library?
g
You cannot, you need Clion
Or Android Studio, but it works only for Android projects
n
Have the following in the def file (for ncurses lib):
Copy code
# Linking the dynamic library doesn't work (curses.h cannot find ncurses/ncurses_dll.h 
# even though ncurses_dll.h is in the same directory).
# Using the static library instead.
staticLibraries = libncurses.a
libraryPaths = /home/napperley/ncurses-6.1/lib
IntelliJ can do code completion for a C lib. Extracted this build logic from a different project (KPad - https://gitlab.com/napperley/kpad ), which causes code completion to work:
Copy code
fromPreset(presets.linuxX64, 'linux') {
            compilations.main.cinterops {
                gtk3 {
                    ['/usr/include'].each {
                        includeDirs "$it/atk-1.0", "$it/gdk-pixbuf-2.0", "$it/cairo", "$it/pango-1.0", "$it/gtk-3.0", "$it/glib-2.0"
                    }
                    includeDirs '/usr/lib/x86_64-linux-gnu/glib-2.0/include'
                }
            }
        }
g
yeah, some support of def files is there
you right
n
Question is would the same trick work with Kotlin 1.3.31 in a build.gradle.kts file (the equivalent Gradle Kotlin DSL version)?
g
in build.gradle?
n
In build.gradle.kts
As I recall I only applied the trick with a dynamic C library, not a static one in the KPad project.
In theory something similar to this in build.gradle.kts should work (would this work for a static lib?):
Copy code
compilations.getByName("main") {
            @Suppress("UNUSED_VARIABLE") val ncurses by cinterops.creating {
                includeDirs("-L${System.getProperty("user.home")}/ncurses-6.1/include/ncurses")
                defFile(project.file("cinterop/ncurses.def"))
            }
        }
After doing a Gradle refresh there is a new klib entry under External Libraries in the Project window. Unfortunately the code completion only does the package name. The knm file is bare bones apart from the package name 🤣 .
g
sorry, I maybe misunderstood you, do you need code completion for C library in Kotlin/Native code?
or are you talking about C code in def files?
n
Talking about code completion for a klib that "wraps" a C lib.
g
in Kotlin code, right?
👌 1
this works
if you generated interop correctly
Unfortunately the code completion only does the package name
I have this behavior when C interop was configured in a wrong way, you do not include correct headers, paths etc
n
Looks as though I will need to find a copy of ncurses (for Linux Mint) that works since the include files are a bit wonky.
g
Yes, I’m not expert in C, I don’t understand logic what should be included, it depends on OS and on library itself
also content of your .def file is very important
n
Managed to get code completion working only for the dynamic library version (via the snippet in build.gradle.kts) simple smile. Unfortunately code completion doesn't work for the static library version 🙁 .
Turns out the source version of ncurses needs to be compiled with the --enable-overwrite option passed to configure, otherwise the include files point to a location that doesn't exist which is very strange.
g
All this header configuration for C/C++ always just super messy, at least for me
n
Much cleaner to have a C lib wrapped into a klib which is significantly easier to deal with.
g
Agree, but someone should configure it for you 😄
n
There was some discussion a while back on having a third party Kotlin Native klib repo that covered some of the major C libraries, that would be very nice. Close to the experience of including a JVM lib via Gradle.
g
yeah, it would be nice
especially for people who are not familiar with C/C++ development
2
m
I thought IntelliJ Ultimate was superset of all Jetbrains's IDEs (with proper plugins installed?
Is my premise wrong or is CLIon exception?
g
Not all, Ultimate doesn't include native development (CLion, App Code, GoLand) and C# development (Resharper, Rider)
👍 1
Also MPS is not a part of Ultimate, as I know