Skip to content

Instantly share code, notes, and snippets.

@ibilon
Last active January 2, 2018 12:06
Show Gist options
  • Save ibilon/02e3e4e7f2c825444501a2e878e0ec7f to your computer and use it in GitHub Desktop.
Save ibilon/02e3e4e7f2c825444501a2e878e0ec7f to your computer and use it in GitHub Desktop.
Calling hxcpp app from c++ app
// haxe -main Host -cpp host
// ./host/Host ./lib/Main.dso
class Host {
public static function main () {
var lib = Dl.open(Sys.args()[0], Dl.RTLD_NOW);
if (lib == null) {
trace("can't find library");
Sys.exit(1);
}
var fn = Dl.sym(lib, "hxRunLibrary");
if (fn == null) {
trace("can't find entry point");
Sys.exit(1);
}
var err:cpp.ConstCharStar = untyped __cpp__("reinterpret_cast< const char*(*)() > ({0})()", fn);
if (err != null) {
trace("Error: " + err);
}
}
}
@:include("dlfcn.h")
extern class Dl {
public static inline var RTLD_NOW:Int = 2;
@:native("dlopen")
public static function open (lib:cpp.ConstCharStar, flags:Int) : cpp.RawPointer<cpp.Void>;
@:native("dlsym")
public static function sym (lib:cpp.RawPointer<cpp.Void>, sym:cpp.ConstCharStar) : cpp.RawPointer<cpp.Void>;
}
// haxe -main Main -cpp lib -D dll_link
class Main {
public static function main () {
trace("main");
var a = new Main();
a.t();
throw "not caught";
}
public function new () {}
function t() {
trace("ok!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment