User Tools

Site Tools


k5:k5.6:start

5.6 Gambas preprocessor

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:

  • A preprocessor Boolean constant
  • A preprocessor constant - compared to a string
  • The OR or AND keyword
  • parentheses to group sub expressions

These are valid preprocessor constants:

ConstantValueAllowed Comparison Operators
SystemOperating System =, <>
Architecture or ArchCPU-Architektur =, <>
Version or GambasCompiler-Version =, <>, <, ⇐, >, >=
Debugging-Information enabledDebugging-Information enabledNone
TrueValue 'true'none
FalseValue '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'.

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k5/k5.6/start.txt · Last modified: 30.01.2022 (external edit)

Page Tools