Gambas Documentation - /lang/.if

# #If

#If PreprocessorExpression ... [ #Else If PreprocessorExpression ... ] [ #Else ... ] #Endif

The #If ... #Endif preprocessor directive allows to conditionally compile code.

PreprocessorExpression is a rudimentary boolean expression that can include the following:

- A preprocessor boolean constant (see below).

- A preprocessor constant compared to a string (see below).

- The Or or the And keyword.

- Parenthesis to group sub-expressions.

The allowed preprocessor constants are:

Constant | Value | Comparison operators allowed
--- | --- | ---
`System` | The operating system. | `=`, `<>`
`Architecture` or `Arch` | The CPU architecture. | `=`, `<>`
`Version` or `Gambas` | The compiler version. | `=`, `<>`, `<`, `<=`, `>`, `>=`
`Debug` | If debugging information is enabled. | None
`True` | The True value. | None
`False` | The False value. | None

### Examples

    ' Only one 'Print' line will be actually compiled by the following code

    Public Sub Main()

      #If System = "Linux"
        #If Architecture = "x86_64"
          Print "Linux 64 bits"
        #Else
          Print "Linux 32 bits"
        #Endif
      #Else If System = "FreeBSD"
        Print "FreeBSD ?"
      #Else
        Print "Other !?"
      #Endif

    End

By using conditional compilation based on operating system or CPU architecture, you create an executable that will be specific to the operating system or CPU architecture used at compilation time.

This is usually not a good idea, so try as much as possible to detect operating system or CPU architecture at runtime, i.e. by testing the value of System.Family and System.Architecture in your code.