Haxe Target-Specific Getting-Started Tutorials

Getting started with Haxe targetting HashLink

Goal: You want to compile your Haxe code to HashLink bytecode, then run it on the HashLink VM (aka “using HL/JIT”).

Note: The Haxe HashLink target can also produce C code which you’d then compile with a C compiler like GCC. This use of HashLink is sometimes called “HL/C”, in contrast to the above “HL/JIT”, which we’re using in this doc.

Prerequisites

Haxe 4

We want to install the newest binary release of Haxe 4 into ~/opt and ~/bin for use with HashLink. Go to the Haxe Download page and click the “(View all versions)” link at the top. Then go to newest/topmost “4.0.0-rc.X” and download the Linux 64-bit binaries. Install the binaries like so:

Then, so haxe can find its std library:

cd ~/opt
ln -s haxe_201xxxxxxxxxxx_xxxxxxx haxe

and in your ~/.bashrc, add:

export HAXE_STD_PATH="$HOME/opt/haxe/std"

Finally, set up haxelib:

haxelib setup

accepting the default ~/haxelib directory name (where haxelib will store installed library files).

If you don’t already have it installed, you’ll need to apt install neko, as haxelib requires it.

Upgrading

To upgrade to a newer preview release:

  • As described above: download, unpack, and copy the newer haxe and haxelib binaries into your ~/bin.
  • rm the haxe symlink in ~/opt and make a new one pointing to the newly unpacked haxe_201xxxxxxxxxxx_xxxxxxx directory.

Try it out

$ which haxe haxelib
/home/YOU/bin/haxe
/home/YOU/bin/haxelib

$ haxe --version
4.0.0-rc.X+xxxxxxx

$ haxelib version
3.4.0

Other

We’ll build the latest HashLink release from source and install into /usr/local. But before that, per the HashLink README, make sure you have a handful of recommended libraries installed:

# apt install libpng-dev libturbojpeg0-dev libvorbis-dev
# apt install libopenal-dev libsdl2-dev
# apt install libmbedtls-dev libuv1-dev

Install HashLink

No longer as root (from the above use of apt), download the latest binary release of HashLink and build it like so:

To upgrade: first, from your previous hashlink version’s directory (in ~/opt/hashlink-1.X), as root, run make uninstall there.

As root, install HashLink:

# make install

HL will be installed into /usr/local. Then run:

# ldconfig

so the system can properly find the libraries just copied to /usr/local/lib (ldconfig searches the library directories and updates /etc/ld.so.cache).

If you want to use HL/C you’ll need to install via haxelib the “hashlink” library:

haxelib install hashlink

This provides the build tools to make HL/C work, analogous to how the hxcpp and hxjava libraries provide the build tools for the C++ and Java targets, respectively.

Try it out

You should now have hl installed:

$ which hl
/usr/local/bin/hl

$ hl
HL/JIT 1.9.0 (c)2015-2018 Haxe Foundation
  Usage : hl [--debug <port>] [--debug-wait] <file>

Try it out on some code:

cd path/to/my-proj
cat Main.hx

which is:

Then build it:

haxe -main Main -hl out.hl

and run it:

hl out.hl

to get the output:

Main.hx:3: Hello, World!

Next Step

Create a my-proj/src directory and move Main.hx into it.

Then create a my-proj/hl.hxml file containing:

-cp src
-main Main
-hl out.hl

and build and run in one shot from your top-level my-proj directory like so:

haxe hl.hxml && hl out.hl