1

I'm learning haxe with the intent of writing code to be used in cpp, java, and cs products. I've found plenty of documentation for writing haxe source code, but not so much for calling the resulting code from the target platform.

To illustrate, say my haxe code creates a Gizmo object:

class Gizmo {
  var data: String;
  public function new() {}
  public function load(s: String) {
    this.data = s;
  }
}

Compile using

  haxe -D static_link --cpp cpp Gizmo.hx

Which completes successfully. Now I'd like to instantiate a Gizmo in my c++ project. Here's where I'm not finding much guidance available. I'm trying this:

(file: gizmo.cxx)

#include <Gizmo.h>
#include <string>

using namespace std;

int main(int argc, char** args) {
  Gizmo *g = new Gizmo();
  string s = "xyz";
  g->load(s);
  return 0;
}

Compile using

g++ -I ~/.haxe/lib/hxcpp/4,2,1/include -I cpp/include -loutput -Lcpp gizmo.cxx 

I get a few warnings about 'offsetof' within non-standard-layout type hx::ArrayBase, followed by

gizmo.cxx: In function ‘int main(int, char**)’:
gizmo.cxx:9:6: error: ‘Gizmo’ {aka ‘class hx::ObjectPtr<Gizmo_obj>’} has no member named ‘load’
    9 |   g->load(s);
      |      ^~~~
make: *** [Makefile:3: gizmo] Error 1

Any ideas on the issue here, and where can I get examples for calling haxe code from c++? Running into similar problems calling from java and .NET (although with some success).

1 Answer 1

1

Thanks to good advise from the haxe forum, I studied the cpp code generated by haxe, stepping through with gdb to get the details. Here's the working code:

#include <hxcpp.h>
#include <Gizmo.h>
#include <Std.h>

int main(int argc, char** argv) {
  HX_TOP_OF_STACK
  hx::Boot();
  __boot_all();
  Gizmo_obj * g = new Gizmo_obj();
  g->__construct();
  String s = HX_STRINGI("abc", 3);
  g->load(s);
  return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

It'd probably be good to link to the forum post from which you derived your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.