https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
a

Adam S

12/09/2023, 10:38 PM
I'm writing bindings for 2 C libraries,
lib-alpha
and
lib-beta
. To keep things separate, I've got two Gradle subprojects.
lib-beta
has
#import lib-alpha.h
, which means that CInterop generates duplicate classes. For example,
lib-alpha
contains a Rectangle struct.
lib-beta
is supposed to use the same Rectangle, but because the CInterop bindings are generated in a separate subproject, the binding for the same Rectangle is duplicated, and so the Rectangle is incompatible. Is there a way to tell the CInterop class to re-use an existing generated binding?
t

Timofey Solonin

12/10/2023, 11:29 AM
If you first create CInterop klib from
lib-alpha
and pass this klib as a
-library
dependency to CInterop for
lib-beta
, then
lib-beta
will reuse the declarations from
lib-alpha.klib
instead of duplicating them.
a

Adam S

12/10/2023, 11:34 AM
thanks, that sounds like what I want! It's tough figuring out how to do this with Gradle though, do you have an example?
t

Timofey Solonin

12/10/2023, 12:02 PM
Something like this? Run with:
Copy code
./gradlew :b:compileKotlinMA64
🙏 1
loading 1
a

Adam S

12/10/2023, 12:23 PM
thanks! I can see it working now
🎉 1
I wonder why my project doesn't work though... I've got a similar set up, although it is more complicated
maybe the include dir needs to be exactly the same? At the moment I copy the file into the two separate subprojects to try to prevent Gradle complaining about overlapping tasks
t

Timofey Solonin

12/10/2023, 12:42 PM
the include dir needs to be exactly the same
This could be the reason, but most importantly you want the header files to be exactly the same. In the example you could duplicate the
include
directory to be
includeA
and
includeB
with equal contents and the declaration reuse would still work. But as soon as you would add a newline to
a.h
in
includeA
the declaration reuse would stop working.
a

Adam S

12/10/2023, 1:15 PM
ah thanks for checking, yes, the header files are the same
I think actually it's working, but IntelliJ is very slow to recognise the generated bindings
yesssss, got it working! Thanks for your help @Timofey Solonin!
🔥 1