A closer look at this source code reveals preprocessor directives that begin with a # character in the source code:
#If Debug If Box Then ' Draw the rectangle in which the text is to be placed. Cairo.Rectangle(Box.X, Box.Y, Box.Width, Box.Height) Cairo.Dash = [5] Cairo.Stroke() Endif #Endif
Gambas has a rudimentary preprocessor. This is a unit that precedes the lexical analysis - the first step in compiling. You can find out what the Gambas preprocessor does in the Gambas documentation at http://gambaswiki.org/wiki/lang/.if.
A preprocessor directive has this syntax:
#If Preprocessor-Expression ... [ #Else If Preprocessor-Expression ... ] [ #Else ... ] #Endif
A #IF…. EndIf preprocessor directive allows conditional compilation of source code. A preprocessor expression is a rudimentary Boolean expression that can include the following:
These are valid preprocessor constants:
Constant | Value | Allowed Comparison Operators |
---|---|---|
System | Operating System | =, <> |
Architecture or Arch | CPU-Architektur | =, <> |
Version or Gambas | Compiler-Version | =, <>, <, ⇐, >, >= |
Debugging-Information enabled | Debugging-Information enabled | None |
True | Value 'true' | none |
False | Value 'false' | none |
Table 5.6.1: Overview of preprocessor constants
In the sample source code below, only one of the four' Print' statements is compiled:
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 ' Main()
Observe this note:
By using the conditional compilation based on the' operating system' or' CPU architecture', an executable file specific to the operating system or CPU architecture is generated during compilation. That's usually not a good idea. Instead, try to detect as much of the operating system or CPU architecture in your code as possible at runtime by checking the values' System. Family' and' System. Architecture'.