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 . 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 flag?) 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 name Description
    macosx Apple
    portbld FreeBSD variant
    unknown Unknown 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 name Description
    armhf (derived) ARM EABI with hard float
    gnu GNU
    gnueabihf GNU EABI with hard float
    msvc Microsoft Visual C++
    musl musl
    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 name Description
    gc_none Disables garbage collection (#5314)
    debug_raise Debugging flag for raise logic. Prints the backtrace before raising.
    preview_mt Enables multithreading preview. Introduced in 0.28.0 ()
    skip_crystal_compiler_rt Exclude 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.