I'm writing bindings for 2 C libraries, `lib-alpha...
# kotlin-native
a
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
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
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
Something like this? Run with:
Copy code
./gradlew :b:compileKotlinMA64
🙏 1
loading 1
a
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
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
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