Linking C++ code to Haxe

136 views
Skip to first unread message

Sami Habib

unread,
Dec 18, 2014, 1:12:35 AM12/18/14
to haxe...@googlegroups.com
So there's this command-line program that one of my programs wraps using sys.io.Process, where it can run it with specific arguments in quick succession.

Anyway, the issue is that the compiled version of this program is only available in .exe form. I would like to also make it available for Mac and Linux users. Luckily, the program is open-source, so from what I can tell there are two ways I can do this:

1) Recompile it into binaries for Mac and Linux and continue using sys.io.Process, or
2) Link the code to Haxe so that it's contained in my program, without being a separate bit.

I would prefer to be able to do the second option, since I feel like it'd be a very helpful for using other libraries as well (and possibly be faster, even for the Windows build). So how would I go about doing this? My experience in C++ is pretty much negligible (I can understand basic code but can't write anything beyond "Hello World"), but I am willing to learn how to do this. Some sort of tutorial would be helpful, if possible.

I would be targeting Neko (and/or CPP I guess), and possibly Android.

Hugh

unread,
Dec 22, 2014, 12:03:06 AM12/22/14
to haxe...@googlegroups.com
Hi,
You should be able to build an "ndll".  You will need to write some cffi "glue", but from the sounds of it, you could get away with a single function that takes an array of strings (argc,argv).
Then from the haxe code, you use (nme/cpp).Lib.load("yourndll", "yourfunctionname"), and pass in haxe array of strings.
This will use shared libraries - which you need for neko, and also works for cpp.  Later, it is easy to convert this to static linking.
If you look at nme as a (big) example of an ndll, the main files are "ExternalInterface.cpp", which you will need your own version of, and the "Build.xml" with uses the hxcpp compiler settings to generate ndlls for platforms supported by hxcpp.

Hugh

Sami Habib

unread,
Dec 22, 2014, 6:02:58 AM12/22/14
to haxe...@googlegroups.com
Thanks! I looked a bit further into it and I think I get how it works. I'm pretty sure I know the glue code to write, I'm just unsure how to go about compiling it. I'll give it a go over the next couple of days.
Reply all
Reply to author
Forward
0 new messages