As part of my recent DelphiCon 2021 session, Multi-Platform Explorations using Delphi, FMX, Feeds, REST and More, my example code needed to use IFDEFs for some of the uses statements, variable definitions and code. This blog post contains an example of the use of defines when compiling for Windows (Win32/Win64), macOS, iOS and Android platforms. I also include screen shots of the sample output on each platform. You can download all of my talk’s sample projects using the session links above.
The simple Delphi FireMonkey application includes a TButton and a TMemo. In the Button’s OnClick event handler the code outputs information about the platform and the compiler defines for each platform.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
uses System.IOUtils; procedure TForm1.Button1Click(Sender: TObject); begin case TOSVersion.Platform of TOSVersion.TPlatform.pfWindows: PlatformPath := 'Windows'; TOSVersion.TPlatform.pfMacOS: PlatformPath := 'macOS'; TOSVersion.TPlatform.pfiOS: PlatformPath := 'iOS'; TOSVersion.TPlatform.pfAndroid: PlatformPath := 'Android'; TOSVersion.TPlatform.pfWinRT: PlatformPath := 'WinRT'; TOSVersion.TPlatform.pfLinux: PlatformPath := 'Linux' else PlatformPath := 'Unexpected platform' end; Memo1.Lines.Add('TPlatform: '+PlatformPath); Memo1.Lines.Add(TOSVersion.ToString); {$IF DEFINED(IOS)} Memo1.Lines.Add('Defined: IOS'); {$ENDIF} {$IF DEFINED(IOS32)} Memo1.Lines.Add('Defined: IOS32'); {$ENDIF} {$IF DEFINED(IOS64)} Memo1.Lines.Add('Defined: IOS64'); {$ENDIF} {$IF DEFINED(ANDROID)} Memo1.Lines.Add('Defined: ANDROID'); {$ENDIF} {$IF DEFINED(ANDROID32)} Memo1.Lines.Add('Defined: ANDROID32'); {$ENDIF} {$IF DEFINED(ANDROID64)} Memo1.Lines.Add('Defined: ANDROID64'); {$ENDIF} {$IF DEFINED(MACOS)} Memo1.Lines.Add('Defined: MACOS'); {$ENDIF} {$IF DEFINED(MACOS64)} Memo1.Lines.Add('Defined: MACOS64'); {$ENDIF} {$IF DEFINED(OSX)} Memo1.Lines.Add('Defined: OSX'); {$ENDIF} {$IF DEFINED(OSX64)} Memo1.Lines.Add('Defined: OSX64'); {$ENDIF} {$IF DEFINED(MSWINDOWS)} Memo1.Lines.Add('Defined: MSWINDOWS'); {$ENDIF} {$IF DEFINED(WIN32)} Memo1.Lines.Add('Defined: WIN32'); {$ENDIF} {$IF DEFINED(WIN64)} Memo1.Lines.Add('Defined: WIN64'); {$ENDIF} {$IF DEFINED(LINUX)} Memo1.Lines.Add('Defined: LINUX'); {$ENDIF} {$IF DEFINED(LINUX32)} Memo1.Lines.Add('Defined: LINUX32'); {$ENDIF} {$IF DEFINED(LINUX64)} Memo1.Lines.Add('Defined: LINUX64'); {$ENDIF} {$IF DEFINED(DCC)} Memo1.Lines.Add('Defined: DCC'); {$ENDIF} {$IF DEFINED(CONSOLE)} Memo1.Lines.Add('Defined: CONSOLE'); {$ENDIF} {$IF DEFINED(POSIX)} Memo1.Lines.Add('Defined: POSIX'); {$ENDIF} {$IF DEFINED(POSIX32)} Memo1.Lines.Add('Defined: POSIX32'); {$ENDIF} {$IF DEFINED(POSIX64)} Memo1.Lines.Add('Defined: POSIX64'); {$ENDIF} {$IF DEFINED(CPUX86)} Memo1.Lines.Add('Defined: CPUX86'); {$ENDIF} {$IF DEFINED(CPUX64)} Memo1.Lines.Add('Defined: CPUX64'); {$ENDIF} {$IF DEFINED(CPU32BITS)} Memo1.Lines.Add('Defined: CPU32BITS'); {$ENDIF} {$IF DEFINED(CPU64BITS)} Memo1.Lines.Add('Defined: CPU64BITS'); {$ENDIF} {$IF DEFINED(CPUARM)} Memo1.Lines.Add('Defined: CPUARM'); {$ENDIF} {$IF DEFINED(CPUARM32)} Memo1.Lines.Add('Defined: CPUARM32'); {$ENDIF} {$IF DEFINED(CPUARM64)} Memo1.Lines.Add('Defined: CPUARM64'); {$ENDIF} {$IFDEF DEBUG} Memo1.Lines.Add('DEBUG is Defined'); {$ELSE} Memo1.Lines.Add('DEBUG is Not Defined'); {$ENDIF} end; |
Screen Grabs for Each Platform
Additional information can be found on the Delphi Wiki:
https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Conditional_compilation_(Delphi)
Interested in building an app that supports Android and Windows? Try out the Windows IDE and make the most of it. Start the Free Trial here.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition