C++ code wrapper issue

75 views
Skip to first unread message

Alexander Cherepanov

unread,
Jun 10, 2014, 6:59:29 AM6/10/14
to haxe...@googlegroups.com
Hello!

I try to develop haxe hxcpp application which will interacts with native API.
At this link: http://permalink.gmane.org/gmane.comp.lang.haxe.general/41010 located example of my issue.

For example, let C++ API looks like this:

int sum(int a, int b) { return (a + b); }
int random() { ... }
....

I'd like to use it in my application without compiling to library, just in code.
Something like this (or another notation, it doesn't matter):

class ExternalAPI {
  public static sum(a:Int, b:Int);
}

trace(ExternalAPI.sum(10, 15)); // 25

I know about CFFI, but I really can't understand how to include it to my project. Where have to be located the *.h files?, how to tell compiler to use it? etc..

Thanks for any help... )

Hugh

unread,
Jun 11, 2014, 12:53:31 AM6/11/14
to haxe...@googlegroups.com

Hi,

So you have native code (sum,random), that you want to call from haxe c++ code?

What you have written is almost correct, but like this:

@:include("../../ExternAPI.h")
extern class ExternalAPI {
     @:native("::sum")
     public static function sum(a:Int, b:Int) : Int;
}

Note the "@:native" to give the actual name of the function, otherwise it assumes it will be an an ExternalAPI_obj namespace.  And assuming the sum proto-type is in "ExternAPI.h" (or whatever you want to call it)

You will also probably want to make the include path simpler, but add a "-I" include to the build system, and add a ".lib" to it too.  This is done with the build.xml injection:

@:include("ExternAPI.h")
extern class ExternalAPI {
     @:native("::sum")
    public static function sum(a:Int, b:Int) : Int;
}

@:buildXml("
   <files id='haxe'>,
      <compilerflag value='-I..'/>
   </files>
   <target id='haxe'>,
     <lib name='../mylib.a'/>
   </target>
")
class Test
{
     public static function main()
    {
      trace(ExternalAPI.sum(10,15));
    }
}

You can use variables in the Build.xml section. like

 <compilerflag value='-I${haxelib:myhaxelib}/include'/> 

I talked about this at wwx, see:

http://gamehaxe.com/wwx/wwx2014.swf

Hugh

Michael Bickel

unread,
Jun 11, 2014, 8:18:02 AM6/11/14
to haxe...@googlegroups.com
omg, my head explodes! How insane/crazy/awesome is that?!? I completely missed this.

Thanks Hugh!
Reply all
Reply to author
Forward
0 new messages