Fail to run "linkDebugFrameworkIos" in Kotlin Multiplatform when linking iOS Framework

Viewed 1176

I am trying to link an iOS .framework to Kotlin Multiplatform/Native so that I can use it in Kotlin. I successfully link the Framework to the Kotlin Multiplatform/Native successfully and the proper packages contains the headers that I linked up.

However Undefined symbols for architecture happens whenever I tried to build the project.

I have tried compiling to different architecture for iPhone Simulator on iosX64 preset and for iPhone iosArm64 preset.

And I tried adding the .framework itself to a vanilla XCode project and it runs fine on both simulator and iPhone.

Here is the .def file

// engine.def
language = Objective-C

package = org.djinnihello
headers = DjinniHelloLib2.h
headerFilter = **

And here is a section of the build.gradle file

//build.gradle
targets {
    fromPreset(presets.android, 'android')
    // This preset is for iPhone emulator
    // Switch here to presets.iosArm64 (or iosArm32) to build library for iPhone device
    fromPreset(presets.iosArm64, 'ios') {
        compilations.main {
            outputKinds('FRAMEWORK')
            cinterops {
                def productsDir = rootProject.file("iosApp/iosApp/Framework").absolutePath
                println(productsDir)
                hello {
                    defFile project.file("src/engine.def")
                    compilerOpts "-F${productsDir} -framework DjinniHelloLib2"
                    linkerOpts "-F${productsDir} -framework DjinniHelloLib2"
                    includeDirs "$productsDir/DjinniHelloLib2.framework/Headers"
                }
            }
        }
    }
}

Here's the error message that popped up when I tried to build it in XCode or running ./gradlew linkDebugFrameworkIos

> Task :app:cinteropHelloIos UP-TO-DATE

> Task :app:linkDebugFrameworkIos FAILED
Undefined symbols for architecture arm64:
  "_OBJC_CLASS_$_HWHelloWorld", referenced from:
     objc-class-ref in combined.o
       ld: symbol(s) not found for architecture arm64
1 Answers

Found the answer:

As of Kotlin Multiplatform 1.1.1 linkerOpts and compilerOpts in build.gradle do nothing. They have to be in the .def file in order to compile and run.

language = Objective-C

package = org.djinnihello
headers = DjinniHelloLib2.h
headerFilter = **

compilerOpts = -F/absolute/path/to/Framework -framework DjinniHelloLib2
linkerOpts = -F/absolute/path/to/Framework -framework DjinniHelloLib2
Related