Compile-time flags

    There are several default flags provided by the compiler with information about compiler options and the target platform. User-provided flags are passed to the compiler, which allow them to be used as feature flags.

    A flag is just a named identifier which is either set or not. The status can be queried from code via the macro method flag?. It receives the name of a flag as a string or symbol literal and returns a bool literal indicating the flag’s state.

    The following program shows the use of compile-time flags by printing the target OS family.

    There’s also the macro method which returns whether a flag is set for the host platform, which can differ from the target platform (queried by ) during cross-compilation.

    Platform-specific flags derive from the target triple. See for a list of supported target platforms.

    crystal --version shows the default target triple of the compiler. It can be changed with the --target option.

    The flags in each of the following tables are mutually exclusive, except for those marked as (derived).

    Architecture

    The target architecture is the first component of the target triple.

    Vendor

    Flag nameDescription
    macosxApple
    portbldFreeBSD variant
    unknownUnknown vendor

    Operating System

    The operating system is derived from the third component of a the target triple.

    ABI

    The ABI is derived from the last component of the target triple.

    Flag nameDescription
    armhf (derived)ARM EABI with hard float
    gnuGNU
    gnueabihfGNU EABI with hard float
    msvcMicrosoft Visual C++
    muslmusl
    win32 (derived)Windows API

    The compiler sets these flags based on compiler configuration.

    User-provided flags are not defined automatically. They can be passed to the compiler via the --define or -D command line options.

    1. $ crystal eval -Dfoo 'puts {{ flag?(:foo) }}'
    2. true
    Flag nameDescription
    gc_noneDisables garbage collection (#5314)
    debug_raiseDebugging flag for raise logic. Prints the backtrace before raising.
    preview_mtEnables multithreading preview. Introduced in 0.28.0 ()
    strict_multi_assignEnable strict semantics for one-to-many assignment. Introduced in 1.3.0 (, #11545)
    skip_crystal_compiler_rtExclude Crystal’s native compiler-rt implementation.

    Custom flags can be freely used in user code as long as they don’t collide with compiler-provided flags or other user-defined flags. When using a flag specific to a shard, it’s recommended to use the shard name as a prefix.