Problem with running Haxe CFFI example

188 views
Skip to first unread message

Alexey Kolonitsky

unread,
Mar 6, 2017, 4:26:20 AM3/6/17
to Haxe
Hi, there

I'm trying to run example of CFFI from old haxe documentaion and faced with some problems. 

I'm working on 
  • Win10, 
  • Haxe 3.3.0, 
  • hxcpp: 3.3.49 [3.4.43]

If I compile example "as is" 
#define IMPLEMENT_API
#include <hx/CFFI.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

extern "C" {
 value sum
(value a, value b)
 
{
 
if( !val_is_int(a) || !val_is_int(b) ) return val_null;
 
return alloc_int(val_int(a) + val_int(b));
 
}
}
DEFINE_PRIM
( sum, 2 );

g++ can't find `hx/CFFI.h`

$ g++ ExternalMath.c -shared -o ../bin/ExternalMath.dll
ExternalMath.c:2:21: fatal error: hx/CFFI.h: No such file or directory
#include <hx/CFFI.h>
^
compilation terminated.

If a add path to hxcpp/include folder and replace angle brackets by quotes, g++ find "hx/CFFI.h" header, but cont find other dependency inside of it.

g++ ExternalMath.c -shared -o ../bin/ExternalMath.dll -I c:\alexey\apps\haxe\lib\hxcpp\3,3,49\include
In file included from hx/CFFI.h:124:0,
                 from ExternalMath.c:2:
hx/CFFILoader.h:58:41: fatal error: mach-o/dyld.h: No such file or directory
                 #include <mach-o/dyld.h>
                                         ^
compilation terminated.

If I remove definition of IMPLEMENT_API, this bug disappeared 
But campilation stoped with error:

$ g++ ExternalMath.c -shared -o ../bin/ExternalMath.dll -I c:\alexey\apps\haxe\lib\hxcpp\3,3,49\include
/tmp/ccHeQvjk.o:ExternalMath.c:(.rdata$.refptr.val_int[.refptr.val_int]+0x0): undefined reference to `val_int'
/tmp/ccHeQvjk.o:ExternalMath.c:(.rdata$.refptr.alloc_int[.refptr.alloc_int]+0x0): undefined reference to `alloc_int'
/tmp/ccHeQvjk.o:ExternalMath.c:(.rdata$.refptr.alloc_null[.refptr.alloc_null]+0x0): undefined reference to `alloc_null'
/tmp/ccHeQvjk.o:ExternalMath.c:(.rdata$.refptr.val_type[.refptr.val_type]+0x0): undefined reference to `val_type'
collect2: error: ld returned 1 exit status

What's wrong with this exmple? 
It is from old haxe documentaion. Is it still actual? 

Hugh

unread,
Mar 6, 2017, 11:45:58 PM3/6/17
to Haxe
Yes, you will need some kind of include path of hxcpp, like "-Ipath/to/hxcpp/include"

The mach-o error - I assume you are on mac?  If not, then there looks like some define is missing.  If so, then there is something wrong with the toolchain.

Either way, it is going to be easiest to use hxcpp build system to build your ndll, since the flags will be setup on all supported compilers.

All you need to do is setup a build.xml script, like: https://github.com/HaxeFoundation/hxcpp/blob/master/test/cffi/project/build.xml

then "haxelib run hxcpp build.xml" will build for the default architecture.

Hugh

Hugh

unread,
Mar 6, 2017, 11:48:16 PM3/6/17
to Haxe
Oh, I see you are on windows.
I may not have tested g++ on windows.  But the build script method should still work with 
haxelib run hxcpp build.xml -Dmingw

Hugh

Alexey Kolonitsky

unread,
Mar 7, 2017, 4:10:27 PM3/7/17
to Haxe
Thank you Hugh! for the link to "build.xml". It works for me
  1. renamed "ExternalMath.c" to "ExternalMath.cpp", it's important for VS compiller.
  2. write build.xml 
  3. сopy-paste header of Project.cpp file from "cffi/project" with all #ifdef and #define
Finally it works!

So my build.xml 
<xml>
 
<include name="${HXCPP}\build-tool\BuildCommon.xml"/>

 
<files id="prime">
   
<file name="include/ExternalMath.cpp" />
 
</files>

 
<target id="default" output="ExternalMath" tool="linker" toolid="dll">
   
<outdir name="../bin/" />
   
<files id="prime"/>
 
</target>
</xml>

My minimal DLL file
#ifndef STATIC_LINK
#define IMPLEMENT_API
#endif

#if defined(HX_WINDOWS) || defined(HX_MACOS) || defined(HX_LINUX)
// Include neko glue....
#define NEKO_COMPATIBLE
#endif

#include <hx/CFFIPrime.h>

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

Single HAXE class which use it:
class ExternalLibrary {
 
static var sumFunc:Int->Int->Int = cpp.Lib.load("ExternalMath.dll","sum",2);

 
public static function main() {
 trace
("Sum 2 + 3 = " + sumFunc(2, 3));
 
}
}

And command line output:
$ ExternalLibrary-debug.exe
ExternalLibrary.hx:15: Sum 2 + 3 = 5
Reply all
Reply to author
Forward
0 new messages