Conditional Compiling - #If, #Else, #Define

Conditional compiling lets one program build for several targets. Code inside an #If block is only compiled when its condition is true. When it's false, the code is left out completely, as if it wasn't there, so it can use commands, Imports and Includes the current target doesn't have.



Basic Syntax
#If DESKTOP
    Print "desktop"
#ElseIf WEB
    Print "web"
#Else
    Print "something else"
#EndIf

#ElseIf and #Else are optional. #End If works as well as #EndIf. The # lines go at the start of a line (spaces in front are fine), and an #If has to end in the same file it starts in.



The Target Names

The target you build for sets these names:

NameSet when building for
DESKTOP_WINDesktop on Windows
DESKTOP_VKDesktop (Vulkan) on Windows
DESKTOP_DEBDesktop on Linux (Debian, Ubuntu, Mint)
DESKTOP_ARCDesktop on Linux (Arch, CachyOS)
DESKTOPAny of the four desktops
WEB_GPUWeb (WebGPU)
WEB_GL2Web (WebGL2)
WEBEither web target
TARGET_PS1PlayStation
TARGET_USBUSB (ISO). It isn't one of the DESKTOP names

A name that isn't set counts as false. Names aren't case sensitive.



Conditions

A condition can use Not, And, Or and brackets, as well as True and False:

#If WEB And Not DEBUG_ON
#If (DESKTOP Or TARGET_USB) And SOUND
#If False

Names are only on or off: there are no values to compare.



Your Own Names: #Define
#Define SHOW_DETAILS

#If SHOW_DETAILS
    Print "score: " & ToString(score)
#EndIf

A #Define counts from its line onwards, including in the files included after it. Put your #Defines near the top of the main file, before its Includes. A #Define inside an #If block that's left out doesn't count. The target names can't be #Defined.



One Program, Every Target

Each target has its own runtime to import, and #If lets one file pick the right one:

#If DESKTOP Or TARGET_USB
Import "BBRuntimeLinux.decls"
#ElseIf WEB_GPU
Import "BBRuntimeWeb.decls"
#ElseIf WEB_GL2
Import "BBRuntimeWebGL2.decls"
#EndIf


Switching Code Off

#If False switches a block off without commenting out every line. The code in it is never read, so it doesn't even have to be finished:

#If False
    OldDrawRoutine()
    half-written idea, not BambooBasic yet
#EndIf


In the Editor

The # lines are shown in the keyword colour, and the code the selected target leaves out is greyed out. Change the Target menu and it updates. In a file that's included by another, the greying only knows about that file's own #Defines.



Key Points

See Also

Examples

See the Conditional Compiling Example: one program that builds for every target.


BambooBasic © 2026 Michael Denathorn