COMMUNITY

Haxe code visibility

Good morning guys!

I have a simple question (been searching the web for a solution for hours by now and can’t find an answer).

Is there a way to pack your haxe classes into a dll similar structure?
So basically I want someone to reuse my code but don’t want to expose what actually the functions do?

Hey @Bobi!

I’ve created haxe-c-bridge as a tool for making native haxe libraries that expose a C API. It uses hxcpp.

Firstly, set your target to hxcpp by adding --cpp bin to your hxml, next tell hxcpp you want a dll rather than an exe by adding -D dll_link

When you compile now you’ll get a dll :partying_face: but you still need some way to use it. There’s two things to think about here: first, we need to expose functions so you can call them from C/C++/Swift/whatever. Next, we need to consider how to play nicely with haxe’s garbage collection and its event-loop so haxe events (like haxe.Timer.delay()) and haxe threads all work correctly. Haxe-c-bridge exists to handle both these things.

Install haxe-c-bridge into your project (it’s a single file so you can just copy it if you want), then add @:build(HaxeCBridge.expose()) to any class you want to expose. It will generate a C header file alongside your .dll exposing all public methods on that class. All haxe code is executed in a separate thread called the haxe-thread so before you call any methods just call initializeHaxeThread(onHaxeException); and you’re good to go!

Because it’s plain-old-c you can use it from any language with a C interface, i.e. Swift, Go etc. Checkout the github page for the full details

3 Likes