haxiomic / haxe-c-bridge

Easily interact with haxe classes from C with an automatically generated C header
MIT License
51 stars 5 forks source link

C++ and Objective-C dependencies into C? #33

Open Lelelo1 opened 2 years ago

Lelelo1 commented 2 years ago

Hey I got great instructions in the Haxe community forums, and it worked out well for Haxe code to compile and be used in a watchOS project in swift (via an Objective-C bridging header file). I wonder though when it come to dependencies - is it restricted strictly C - or can Cpp be used like LucenePlusPlus in the watchOS projects as well? My understanding so far about haxe in general is that the project becomes tied and dependent to for instance Java - if you use a Java dependency in Haxe. But I am inexperienced in the realm of C, C++ and Objective-C, and HaxeCBridge for that matter. So can I pick out C++ and Objective C dependencies, and have it compiled into C still?

haxiomic commented 2 years ago

Hey @Lelelo1!

You can use both C++ and Objective-C libraries from haxe – the haxe code in the watchOS project from earlier is translated into C++ through hxcpp so you can access anything you could normally use from C++, this includes objective-c things

There's several ways of calling native code, one is you could simple emit C++ inline with your haxe

untyped __cpp__('// this will be copied into the C++ file as-is, referencing this variable {0}', someVariable);
untyped __cpp__('printf("hello world %d", {0})', 1234); 

Or using the __global__ object like this, which lets you stay in haxe-syntax

untyped __global__.printf("hello world %d", 1234);

Of course you'll need to include header files to actually call the functions, so generally the best way is to make externs, where you can specify the required headers with @:include('example.h')

here's an example for opengl https://github.com/haxiomic/gluon/blob/master/gluon/webgl/native/ES2Context.hx

hxcpp has some built-in externs for some common Objective-C types: https://api.haxe.org/cpp/objc

I think when working with objective-c, it can help to add -D objc so hxcpp emits .mm files instead of .cpp

There's not much in the way of guides so the best way to learn right now is looking at the example code in hxcpp and asking in the forum https://github.com/HaxeFoundation/hxcpp/tree/master/test

At present HaxeCBridge doesn't help with calling native code from haxe, rather it helps with the inverse calling haxe code from native (more to come in the future tho!)

Good luck, let me know how it goes!

Lelelo1 commented 2 years ago

Is it possible to use a Objective-C library in haxe, output it in C++ and then used in a C# project, for instance? Or is that ability to use both C++ and Objective-c limited to the clock project? Thanks for the samples!

haxiomic commented 2 years ago

Yes, it's possible to use Objective-C from haxe and then use the result from a C# project but you'll need to take care to understand how everything interconnects. Objective-C is largely syntax sugar over C++ if you rename your .cpp files to .mm, you can still compile them with C++ code but now you can also write and interact with Objective-C. Haxe will do this for you if you add -D objc.

Once you've got that working and you've figured out externs so you can use the library you want in haxe, the next step is using the result from C#.

C# can call native code compiled into a dynamic library, which is like an exe but you don't run it, instead it contains native code for other programs to use (extension is .dll on windows, .so on linux and .dylib on macOS). You could use haxe-c-bridge for this step – it will give you a dynamic library and a header file with methods you've exposed from haxe. Here's some notes on calling native code from C#: https://stackoverflow.com/questions/11425202/is-it-possible-to-call-a-c-function-from-c-net The header from haxe-c-bridge should work out of the box with C#

However, it depends on what platform you're using – if it's on mac it'll work, linux and window probably not because I've not yet tested those platforms. The changes requires to haxe-c-bridge will be minor, I just can't justify the time investment right now

Lelelo1 commented 2 years ago

My use case is to embed a search engine that can be used in a mobile app, watch projects, potentially existing/upcoming ARGlasses as well. I have always strived towards cross platform solutions. I am still learning and understanding the limits of haxe. The mobile app is a Xamarin.Forms project, and I have read that embedding c++ libraries should be possible in the official docs. The watchOS project worked out with the dummy code as you already know My ambition is to have this search engine and some custom (haxe) code available for as many platforms as possible. If you mean the compile step into dynamic library is currently only tested on mac, that is fine for me - since I am using mac.