Hi guys, I’m a total newbie on kotlin native/multi...
# kotlin-native
n
Hi guys, I’m a total newbie on kotlin native/multiplatform projects. I tried to create a multi-platform project in idea community edition using the new project template. it created a build.gradle file like this.
Copy code
plugins {
    id 'kotlin-multiplatform' version '1.3.0-rc-146'
}

repositories {
    maven { url '<http://dl.bintray.com/kotlin/kotlin-eap>' }
    mavenCentral()
}
group 'com.example.sample'
version '0.0.1'

apply plugin: 'maven-publish'

kotlin {
    targets {
        fromPreset(presets.jvm, 'jvm')
        fromPreset(presets.js, 'js')
        // For ARM, preset should be changed to presets.iosArm32 or presets.iosArm64
        // For Linux, preset should be changed to e.g. presets.linuxX64
        // For MacOS, preset should be changed to e.g. presets.macosX64
        fromPreset(presets.macosX64, 'macos')
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
            }
        }
        commonTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test-common'
                implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
            }
        }
        jvmMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
            }
        }
        jvmTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test'
                implementation 'org.jetbrains.kotlin:kotlin-test-junit'
            }
        }
        jsMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-js'
            }
        }
        jsTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test-js'
            }
        }
        macosMain {
            dependencies {
            }
        }
        macosTest {
        }
    }
}
in the macos main module i want to include the ncurses library. So I changed the part as
Copy code
macosMain {
            dependencies {
             cinterop('ncurses') {
                    // something more
                }
            }
        }
I am getting error
Could not find method cinterop() for arguments [ncurses, build_1mx5jx84o9toi8bpdxyehixos$_run_closure2$_closure6$_closure13$_closure21$_closure22@836eb38] on object of type org.jetbrains.kotlin.gradle.plugin.mpp.DefaultKotlinDependencyHandler.
Also I’m a total newbie to c/cpp or native world, so i may be doing something really stupid. if anyone can help i would b really grateful.
o
It should look something like this:
Copy code
fromPreset(presets.macosX64, 'sdl2macos') {
                compilations.main {
                    cinterops {
                        sdl {
                            defFile = file("sdl2/interop/sdl2.def")
                        }
                    }
                }
            }
n
thanks @orangy. this worked. gradle build is working. the def file i have is
Copy code
headers = /usr/local/opt/ncurses/include/ncurses.h
headerFilter = /usr/local/opt/ncurses/include/ncursesw/*
linkerOpts.osx = -L/usr/local/opt/ncurses/lib -lncurses
The paths are all okay. the file
/usr/local/opt/ncurses/include/ncurses.h
is the header file from the installation of homebrew on mac mojave. I used the command
cinterop -def src/macosMain/ncurses.def -compilerOpts -I/usr/local/opt/ncurses/include -o src/macosMain/lib/ncurses
to generate the binding to look into the functions. On executing, a kotlin file is created at the path
src/macosMain/lib/ncurses-build/kotlin/ncurses/ncurses.kt
. The content od the file is empty. (apart from the package import and @file statements).content is below
Copy code
@file:kotlinx.cinterop.InteropStubs
@file:Suppress("UNUSED_VARIABLE", "UNUSED_EXPRESSION")
package ncurses

import kotlin.native.SymbolName
import kotlinx.cinterop.*

// NOTE THIS FILE IS AUTO-GENERATED
This doesn’t seem right. I’m not even sure if I’m doing the right thing here. 😞
o
You don’t need to run
cinterop
manually, gradle task should handle it for you. Please check https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md for more information about
.def
files
Also it’s better to ask Kotlin/Native related questions in #kotlin-native channel
🤔 2
n
@orangy: Yes, I understand that. I was trying the cinterop when I hadn’t seen ur reply. However the generated file was empty like above. On running the gradle build, the generated kotlin file is empty, which doesn’t seem right. Can u suggest what I might be doing wrong? We r in the #kotlin-native channel only. 🙈
This is the cinterop section in build.gradle
Copy code
fromPreset(presets.macosX64, 'macos') {
            compilations.main {
                cinterops {
                    ncurses {
                        defFile = file('src/macosMain/ncurses.def')
                        includeDirs '/usr/local/opt/ncurses/include'
                    }
                }
            }
        }