Haxe Code Cookbook
Haxe programming cookbookBeginnerConditional compilation

Conditional compilation

Reading time: 1 minute

This snippet demonstrates use of conditional compilation with custom compiler flags.

Conditional compilation is a tool commonly used to alter the flow of the compilation process. It relies on the use of compiler flags (also known as defines), which are configurable values that exist only during compilation.

Compiler flags are set with -D key or -D key=value from the command-line or build file. The values of Float, Int, and String constants are used directly when evaluating conditionals.

Note that those conditional compilation branches that the compiler doesn't enter are discarded while parsing the source file.

To get a list of supported Haxe compiler flags, use haxe --help-defines.


class Main {
  static function main() {
    #if introduce
    trace("Hello! This is an example of conditional compilation.");

    #if (level > 4)
    trace("Welcome, administrator!");
    #elseif (level > 2)
    trace("Welcome, super user!");
    trace("Welcome, user!");


Assume the following build file:

-main Main
-neko main.n
-D introduce
-D level=3

Running neko main.n from the compiler output directory will result in:

Hello, this is an example of conditional compilation.
Welcome, super user!

Last modified:
Category:  Beginner