I am trying to migrate C++ code by compiling Kotli...
# kotlin-native
r
I am trying to migrate C++ code by compiling Kotlin to a dynamic library and replacing step by step. I load this lib by including the header and the symbols:
Copy code
#include "libmy_api.h"
auto mylib = libmy_symbols();
So far so good. Now I would love to replace the orginal classes, for example
CMyClass
with the one from the Kotlin library. So I remove
CMyClass
from the C-source code, and I add a typedef:
typedef libmy_kref_MyClass MyClass;
Now I can use the regular
MyClass
as a type:
std::vector<MyClass> classes
So I don't have to touch that code, as it was already referencing "MyClass". Still good. Now the problem is when I try to create an instance. The current code is like this:
classes.push_back(MyClass())
But to initiate the class in the KN dl way, I have to replace all the code with:
classes.push_back(mylib->kotlin.root.MyClass.MyClass())
Is there a way, like typedef or using does, to keep this code intact while it does actually reference the kotlin generated dynamic library?
o
Not sure if C API can be drop in replacement for C++ classes, and we got no C++ interop yet
r
But C++ can load a C library afaik, so I think that will work. Also the first example works, I just don't know how to type-alias the creation of the instance. I tried with
->
,
::
, 'struct`,
*
and all that in the typedef, but it seems it is not allowed. I'll try to create a small example later today.
y
It seems that the "classes" in the generated dll aren't intended to be instantiated from C++/Rust/whatever. If you use StableRef you can pass objects between Kotlin and C, but they can't be modified by C code like structs can.
r
I don't understand, I thought this was the whole purpose of it?
So something like this would never work?
Copy code
#include <iostream>
#include "libmyn_api.h"

auto mylib = libmy_symbols();

using CPerson = libmy_kref_Person;

int main() {
    CPerson person = mylib->kotlin.root.Person.Person("firstname", "lastname");
    std::cout << "Hello, " << person.lastname << std::endl; // error on lastname, only "pinned" available"
    return 0;
}
This works
mylib->kotlin.root.Person.get_lastname(person)
. Now I only need a way to shorten it to something like
Person.get_lastname(person)
and a simple way to create a new instance, preferable like it was before (
Person("first", "last")
)
I figured to get it this good:
Copy code
auto personlib = mylib->kotlin.root.Person;
 auto person = personlib.Person("first", "last")
 std std::cout << "Hello, " << personlib.get_lastname(person) << std::endl;
But as i'm very unexperienced with C++ there probably is a cleaner and more generic way.
Too bad it results in
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
https://kotlinlang.org/docs/tutorials/native/dynamic-libraries.html
Copy code
There is a constructor in the Clazz field (aka kotlin.root.example.Clazz.Clazz), which is the constructor function to create an instance of the Clazz.
Should be possibe to create an instance..
Anyone knows how to resolve this error when using a compiled dynamic library?
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
It happens when i try to create an instance.
Anyone? 😞
I commited the project to github. Maybe someone can spot what I did wrong? Where should I look - the cmakelist? Or the library itself? https://github.com/RdeWilde/kotlin-dl-test
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
on this line https://github.com/RdeWilde/kotlin-dl-test/blob/master/main.cpp#L9