How can I import c++ code to commonMain (Kotlin/Multiplatform project) - kotlin-multiplatform

I want to implement a few use cases before migrating my business logic code from JAVA to Kotlin/Multiplatform.
One of them is using c++ code.
There is an option to use c++ code in Kotlin/Multiplatform project on commonMain level?
Or I need to write a wrapper layer (like JNI) for each platform?
Can I get some example code?
Many thanks!

Calling to C++ can be tricky. You'll need to have JNI on the JVM, and on native you'll need to wrap the C++ with C (or Objective-C), then use cinterop to make that available to the native Kotlin compiler. Then, in common, you'll need to write a common version of the logic, and have that implemented on each platform.
Kotlin(common)
|
/ \
/ \
Kt(jvm) Kt(native)
/ \
JNI C/ObjC
/ \
C++ C++
There may eventually be direct C++ interop on native, but not sure when.

Related

Is there a way to use sqrt when using clang and web assembly target

I'm compiling c++ to web assembly using clang --target=wasm32 --no-standard-libraries. Is there a way to convince clang to generate sqrt? It's not finding <math.h> with this target.
Do you already tried to compile without the flag --no-standard-libraries? If you remove it, the clang probably will find math.h library (because its a standard library).
This is because wasm32-unknown-unknown is a completely barebones targets in Clang, and doesn't have any standard library - that is, no math.h, no I/O functions, not even memcpy.
However, you can usually get away with using --target wasm32-wasi + WASI SDK instead: https://github.com/WebAssembly/wasi-sdk
It includes the whole standard library, including even functions for interacting with the filesystem via the WASI standard in compatible environments.
If your code doesn't depend on filesystem / clock / other I/O, then you can safely use WASI-SDK to get math.h, memcpy, malloc and other standard functions, and the resulting WebAssembly will be compatible with any non-WASI environments as well.

How can you link against (non-standard) libraries from Terra?

I'd like to use raylib with Terra/Lua (better metaprogramming) but the official examples only show how to interface with the C standard library, which I assume Terra is linked against already.
But raylib has a header (raylib.h, I know how to include it in terra) and a so/dll file (the actual code of the library).
How can I link my Terra cide against raylib?
P.S. I'm interested in writing mostly Terra and just doing macros in Lua which is the third use case listed on the Terra website:
A stand-alone low-level language. Terra was designed so that it can
run independently from Lua. In fact, if your final program doesn’t
need Lua, you can save Terra code into a .o file or executable. In
addition to ensuring a clean separation between high- and low-level
code, this design lets you use Terra as a stand-alone low-level
language. In this use-case, Lua serves as a powerful meta-programming
language. Here it serves as a replacement for C++ template
metaprogramming or C preprocessor X-Macros with better syntax and
nicer properties such as hygiene. Since Terra exists only as code
embedded in a Lua meta-program, features that are normally built into
low-level languages can be implemented as Lua libraries. This design
keeps the core of Terra simple, while enabling powerful behavior such
as conditional compilation, namespaces, templating, and even class
systems implemented as libraries.
Nevermind, I think I found what I need.
terralib.linklibrary(filename)
Load the dynamic library in file
filename. If header files imported with includec contain declarations
whose definitions are not linked into the executable in which Terra is
run, then it is necessary to dynamically load the definitions with
linklibrary. This situation arises when using external libraries with
the terra REPL/driver application.
Source: Terra docs

Obtain compiler flags in skylark

I'd like to convert a CMake-based C++ library to bazel.
As part of the current CMake project, I'm using a libclang-based code generator that parses C++ headers and generates C++ code from the parsed AST. In order to do that, I need the actual compiler flags used to build the cc_library the header is part of. The flags are passed to the code generation tool so it can use clang's preprocessor.
Is there any way I could access the compiler flags used to build a dependency from a skylark- or gen_rule rule? I'm particularly interested in the include paths and defines.
We're working on it. Well, not right now, but will soon. You might want to subscribe to the corresponding issue, and maybe describe your requirements there so we take them into account when designing the API.

using an ios framework for native c++ code for ios

I have written a c++ library that needs opencv which is an image processing library. I want to now use this c++ library on ios. To do that I am going to copy my code to a mac and build to produce a cocoa touch static library.
Since, this has a dependency on opencv, I downloaded its ios framework. But now I am confused whether a framework can be used from c++ code or just from objective c/c++ ? Do I have to recompile this library so that i get c++ libraries or I can use the framework in my c++ code?
Yes, it can be used with c++. You will have to make sure you Type is set to "Objective C++ Source" for where you are making the framework calls.
I mix my C++ code with frameworks all the time.
Note this goes both ways. If you have Obj-C interacting with C++, you'll need to either have the file be a .mm or be of the "Objective C++ Source" Type.
The Type selection is in the File Inspector for files.

How to organize Android NDK / iOS project

I wrote a game for Android using Android NDK. Core of game logic written in pure C++. So, I want to port it to iOS. I know how to port Java (JNI) wrapping to Objective-C.
My question is how to organize this project on FS? I want to keep a single code base, and I use git (link).
First, note that there should be no need at all do wrap your C++ code: the objective-C extension of the C language is also compatible with C++. So, you can compile your code as "Objective-C++" and directly use C++ classes with Objective-C code.
Second, you can put your code anywhere you wish on the disk. Both eclipse for Android and xcode for iOS will let you include source code into your project from any relative directory.
If you ever need platform-specific native code you can use #ifdef __ANDROID__ for android-specific code inside your C++.

Resources