I am experimenting with kotlin multiplatform in this chess app. I have c++ code for the chess engines in the app. For each engine, I want to create a kmm module. Since kotlin native does not yet interop with c++, I have to create wrappers around the c++ code. In each androidMain, I have the JNI class that interface with c++, and this works. ios is where I am not clear. I believe I have to create an obj-c++ wrapper (.mm file) around the c++ code, then do cinterop for the wrapper.
iosTarget("ios") {
compilations.getByName("main") {
val jwtc by cinterops.creating {
defFile(project.file("src/iosMain/cpp/jwtc.def"))
packageName("com.cinterop.jwtc")
}
}
}
Is this right? When I try to build it, gradle sync fails with this error:
Execution failed for task ':jwtc:cinteropJwtcIos'.
> Process 'command '/Applications/Android Studio Preview.app/Contents/jre/Contents/Home/bin/java'' finished with non-zero exit value 1
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':jwtc:cinteropJwtcIos'. <36 internal calls>
Caused by: org.gradle.process.internal.ExecException: Process 'command '/Applications/Android Studio Preview.app/Contents/jre/Contents/Home/bin/java'' finished with non-zero exit value 1
at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:414)
at org.gradle.process.internal.DefaultJavaExecAction.execute(DefaultJavaExecAction.java:52)
at org.gradle.process.internal.DefaultExecActionFactory.javaexec(DefaultExecActionFactory.java:198)
at org.gradle.api.internal.project.DefaultProject.javaexec(DefaultProject.java:1145)
at org.jetbrains.kotlin.compilerRunner.KotlinToolRunner.runViaExec(KotlinToolRunner.kt:98)
at org.jetbrains.kotlin.compilerRunner.KotlinToolRunner.run(KotlinToolRunner.kt:73)
at org.jetbrains.kotlin.gradle.tasks.CInteropProcess.processInterop(KotlinNativeTasks.kt:1035) <122 internal calls>
I have cleaned and restarted android studio severally, and also tried java 8 instead of 11.
I would also appreciate links to more info about the def file. There is not much about it in the kotlin docs.
My jwtc.def:
headers = GameWrapper.h
headerFilter = GameWrapper.h
GameWrapper.h:
@interface GameWrapper
- (void) getBestMove;
@end
GameWrapper.mm:
#include "GameWrapper.h"
#include "Game.h"
@implementation GameWrapper
- (void) getBestMove {
Game game;
game.getBestMove();
}
@end
I finally found that the real error message:
Exception in thread "main" java.lang.Error: /var/folders/17/14lmpq652zn5h_cy_h51z7yr0000gr/T/8492320185156128475.c:1:10: fatal error: 'GameWrapper.h' file not found
and fixed it by adding compilerOpts
to point to the location of my src files:
iosTarget("ios") {
compilations.getByName("main") {
val jwtc by cinterops.creating {
defFile(project.file("src/iosMain/cpp/jwtc.def"))
packageName("com.cinterop.jwtc")
compilerOpts ("-Isrc/iosMain/cpp/")
}
}
}