Navigate to a desired project directory and create a Test.hx
source file with the following content:
class Test {
static function main() {
trace("Hello world");
}
}
Haxe source files are called modules. A module should define a type (abstract
, class
, enum
, interface
, or typedef
) with the same identifier as the module name - in this case the Test
class. Once that requirement is met, a module can define an arbitrary number of different types.
Haxe programs require an entry point, as denoted by the static main
function. The class implementing the entry point is the startup class or main class. Again, in this case the main class is the Test
class.
The trace()
function is a general purpose logging function exposed to the global namespace for the sake of convenience. It outputs to the target language's standard output handle (e.g. browser console for JavaScript, command line for C++). See the API documentation for more information.
Navigate to the project folder from your command line. Test to see if Haxe is configured in your environment by calling:
haxe --help
The Haxe interpreter can be used to test code that does not rely on any specific target language API. Use the interpreter by calling:
haxe -main Test --interp
Remember, the Test
module contains the Test
startup class, which is why -main Test
is passed to the compiler.
Haxe sources can compile (transpile) to sources / bytecodes of several different languages. The following table displays the target language, compiler flag, argument type, and compilation result. Use it by calling:
haxe -main Test [flag] [argument]
.
Language | Flag | Argument | Result |
---|---|---|---|
ActionScript 3 | -as3 | Directory | Source |
C# | -cs | Directory | Source + optional bytecode (.exe) |
C++ | -cpp | Directory | Source + optional binary (native) |
Flash | -swf | File | Bytecode (.swf) |
HL | -hl | File | Source |
Lua | -lua | File | Source |
Java | -java | Directory | Source + optional bytecode (.jar) |
JavaScript | -js | File | Source |
Neko | -neko | File | Bytecode (.n) |
PHP | -php | Directory | Source |
Python | -python | File | Source |
HashLink | -hl | File | Bytecode (.hl) |
Note that the path arguments here are relative to the path haxe
was called from. The optional bytecode/binary outputs can be opt-outed by adding the -D no-compilation
flags, in order to avoid an additional compilation step involving calling the target language's compiler.