Search code examples
c++haxe

Neko Dlls in Haxe C++ target


I am trying to use Neko dlls (written in C++) with the C++ target of Haxe. I am able to call the functions in haxe but not able to pass values.

This is the C++ code -

value Hello(value h)
{
    cout << val_int(h);
    return val_int(1);
}DEFINE_PRIM(Hello, 1);

This is the Haxe code -

class Main
{
     var load = cpp.Lib.loadLazy( "ndll" , "Hello", 1 );
     static function main()
     {
          load(1);
     }
 }

It executes only if the function does not take parameters. Also, the value that is returned form the C++ function to Haxe is null.

This code actually works perfectly when I compile for the neko target, but it doesn't seem to work with the cpp target.

Any help is appreciated.


Solution

  • In order for this to work, you'll have to add to the header of your cpp file:

    #define IMPLEMENT_API
    #include <hx/CFFI.h>
    

    (instead of neko's headers) If you want the ndll to run on both neko and hxcpp, you should also add

    #define NEKO_COMPATIBLE
    

    before the hx/CFFI.h include.

    You can compile using whatever is best for you, but I recommend using a Build.xml to generate your ndll, since it will automatically add the include and lib paths correctly for hxcpp's headers. You can see an example of a very simple Build.xml here: http://pastebin.com/X9rFraYp

    You can see more documentation about hxcpp's CFFI here: http://haxe.org/doc/cpp/ffi