MQL4 Conditional Compilation

Overview

Preprocessor directives that enable or disable code sections during compilation based on defined identifiers.

Directives

  • **#ifdef identifier**
  • * Compiles the enclosed code if identifier has been defined via #define.
  • **#ifndef identifier**
  • * Compiles the enclosed code if identifier has NOT been defined via #define.
  • **#else**
  • * Used within #ifdef or #ifndef blocks. * If the preceding condition is false, the code following #else is compiled.
  • **#endif**
  • * Marks the end of a conditional compilation block.

    Structure

    Directives can be chained with #else to create if-else like structures. The compiler ignores code blocks based on the evaluation of the conditions.

    #ifndef TestMode
       #define TestMode
    #endif

    void OnStart() { #ifdef TestMode Print("Test mode"); #else Print("Normal mode"); #endif }

    Standard Macros

    The following macros are automatically defined based on the compilation context:

  • __MQL4__: Defined when compiling .mq4 files.
  • __MQL5__: Defined when compiling .mq5 files.
  • _DEBUG: Defined when compiling in debug mode.
  • _RELEASE: Defined when compiling in release mode.
  • **Example Usage:**

    void OnStart()
    {
       #ifdef __MQL5__
          #ifdef _DEBUG
             Print("Hello from MQL5 compiler [DEBUG]");
          #else
             #ifdef _RELEASE
                Print("Hello from MQL5 compiler [RELEASE]");
             #endif
          #endif
       #else
          #ifdef __MQL4__
             #ifdef _DEBUG
                Print("Hello from MQL4 compiler [DEBUG]");
             #else
                #ifdef _RELEASE
                   Print("Hello from MQL4 compiler [RELEASE]");
                #endif
             #endif
          #endif
       #endif
    }
    

    Related Directives

  • #define (Macro substitution)
  • #property (Program properties)
  • #include (Including files)
  • #import (Importing functions)