How to build Haxe program (and run on HL) with only module-level functions & variables?

I’d like to try out using module-level variables and functions, running them with Hashlink.

Given my-prog.hx:

var nm = "World";

function greet(s:String) {
    trace("Hello, " + s + "!");
}

function main() {
    greet(nm);
}

How can I compile that to run on Hashlink?

I created an hl.hxml file containing:

--hl out.hl

And trying haxe hl.hxml fails, of course, since I didn’t tell it the filename, but also trying

haxe -hl out.hl my-prog.hx

fails as well.

What needs to go into my hl.hxml file?

Note, this is using:

$ haxe --version
4.2.1+bf9ff69

You need to tell haxe the classpath and main Class. Haxe doesn’t really support “Single File” programs like your example (at least to my knowledge), so I’d recommend putting your file into a directory, usually called src. You should also rename your file to be PascalCase, as the File’s name defines the class name, and class names in haxe must be PascalCase. If you don’t want to use a src folder, that should work too. This build.hxml file should do the trick:

-cp . # Set the classpath to the current working directory
-main MyProg # Should use a File called MyProg.hx in the classpath

-hl out.hl # output to a file called out.hl

Note that you need to remove the comments I made for this to be a valid hxml file.
In case you wanna put your code into a directory src instead, you can set the classpath to src instead of . (the current working directory).
The same can also be achieved via these command line arguments

haxe \
  -p . \
  --main MyProg \
  --hl out.hl

No need for that. Current working directory is added to the classpath by default.

  • rename file to MyProg.hx (use PascalCasing)
  • then run haxe -hl out.hl -main MyProg (no need to add “.hx”)

Nice. I changed the filename to “MyProg.hx”, then in my hl.hxml file put:

--hl my-prog.hl
--main MyProg

And built+ran like so:

haxe hl.hxml
hl my-prog.hl

Thanks all!