JetBrains / kotlin-native

Kotlin/Native infrastructure
Apache License 2.0
7.02k stars 565 forks source link

Objective-C interoperability architecture issue Undefined symbols for architecture x86_64 #4737

Closed dargoz closed 3 years ago

dargoz commented 3 years ago

I just tried using native interop feature since I need native code written in Objective-C to be used in my library. So first I'm trying to test using simple hello to interop an Objective-C code

gradle :

kotlin {
     ...

     val iosX64 = iosX64("ios") {
          compilations.getByName("main) {
              val myInterop by cinterops.creating {
                 defFile(project.file("src/nativeInterop/cinterop/objective-c-file.def"))
              }
          }
     }

     val iosArm32 = iosArm32 ("iosArm32 ") {
          compilations.getByName("main) {
              val myInterop by cinterops.creating {
                 defFile(project.file("src/nativeInterop/cinterop/objective-c-file.def"))
              }
          }
     }

     val iosArm64 = iosArm64("iosArm64") {
          compilations.getByName("main) {
              val myInterop by cinterops.creating {
                 defFile(project.file("src/nativeInterop/cinterop/objective-c-file.def"))
              }
          }
     }

     ...
}

my .def configuration :

language = Objective-C
headers = Hello.h Foundation/Foundation.h
package = org.native

compilerOpts = -Isrc/nativeInterop/include

linkerOpts = -framework Foundation "-F/Applications/Xcode 11.3.1.app/Contents/Developer/Platforms/"

this is the project structure look like
enter image description here

my Hello.h

#import <Foundation/Foundation.h>

@interface Hello: NSObject

+ (id) init;
- (void) helloObjectiveC;

@end

Hello.m

#import "Hello.h"

@implementation Hello

+ (id) init {}
- (void) helloObjectiveC {
    printf("hello Objective c interop")
}

@end

after run cinterop task from gradle, the klib generated from that hello class and of course now I can import it to my kotlin project. for example in my iOS module:

package bca.lib

import org.native.Hello

actual object Platform {

    actual fun getPlatform() = "iOSMain"

    fun helloNativeInterop() {
        val hello = Hello()
        hello.helloObjectiveC()
    }

}

Then I'm trying to call it in my unit test to check if I can get the result or I aslo try to build it to framework but I got an error :

> Task :cleanIosTest UP-TO-DATE
> Task :cinteropMyInteropIos UP-TO-DATE
> Task :generateBuildKonfig UP-TO-DATE
> Task :generateIosMainAppDatabaseInterface UP-TO-DATE
> Task :compileKotlinIos UP-TO-DATE
> Task :iosProcessResources UP-TO-DATE
> Task :iosMainKlibrary UP-TO-DATE
> Task :compileTestKotlinIos UP-TO-DATE
> Task :linkDebugTestIos FAILED
e: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld invocation reported errors
Please try to disable compiler caches and rerun the build. To disable compiler caches, add the following line to the gradle.properties file in the project's root directory:

    kotlin.native.cacheKind=none

Also, consider filing an issue with full Gradle log here: https://kotl.in/issue
The /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld command returned non-zero exit code: 1.
output:
Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$_Hello", referenced from:
      objc-class-ref in result.o
ld: symbol(s) not found for architecture x86_64
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':linkDebugTestIos'.
> Compilation finished with errors
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.6.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 8s

Anyone know how to resolve this issue or maybe is it impossible to interop in that way? I just needed so it can be used in my iOS app later after I successfully build it to .framework

SvyatoslavScherbina commented 3 years ago

How did you create this issue?

"New issue" button on the kotlin-native issues page on GitHub website opens the following page for me: https://github.com/JetBrains/kotlin-native/issues/new/choose. It doesn't seem to have an option to create new issue on GitHub on my side.

So did you use a custom GitHub client or whatever?

dargoz commented 3 years ago

unfortunately, when I create this issue that page didn't show up.. When I try it again when you say it, it's already appear.. I don't use custom GitHub, I don't know why that happen

SvyatoslavScherbina commented 3 years ago

I see. Thank you for the explanation. Regarding your problem: you probably have to compile Hello.m and link it to your binary to fix it.