55 Votes

Lazarus: Detect Operating System (Compiler Switch)

Tutorial by Stefan Trost | Last update on 2023-01-07 | Created on 2013-05-17

When writing applications for multiple operating systems using Lazarus, it might be necessary to use different code or to include different units at some points depending on the current operating system. Nevertheless, of course, you do not want to adjust the code again and again to the current system you are compiling on. Instead, we would like to have a uniform solution for all systems that automatically adapts to the system on which the compilation is currently taking place - although the code is containing some system depending parts.

So, in this tutorial, I would like to show you a way of how to show the compiler that a certain code or a certain part of a unit should only apply to a specific system. You can use the so-called compiler directives {$IFDEF}, {$ENDIF} as well as {$ELSE} for this.


First, just have a look at a small example:

   {$IFDEF WIN32}
      ShowMessage('32 Bit Windows');

   {$IFDEF WIN64}
      ShowMessage('64 Bit Windows');
   ShowMessage('No Windows');

In this example, first we are using an IF condition to check whether we are on a Windows system. If so, we look up whether it is a 32 bit or 64 bit Windows and we alert this with the help of ShowMessage. If not, we alert a message that the current system is not Windows.

Areas of Application

You can combine the directives as you want and you can use them at any place you want. So you can not only make parts of the code only available for specific systems, but, for example, you can also limit entire procedures or functions including their declarations to certain systems or, for example, add or skip units depending on the system.

A typical example would be system-related units such as the Windows Unit or the Units ShellApi, CocoaAll or BaseUnix, which, depending on the unit, can only be used on Windows, MacOS or Linux. If we want to use functions from one or more of these units and we also want to use the same code for other systems, we could write the following, as an example:

  {$IFDEF WINDOWS} Windows, ShellApi, {$ENDIF}
  {$IFDEF DARWIN} CocoaAll, {$ENDIF}
  {$IFDEF LINUX} BaseUnix, {$ENDIF}
  Classes, SysUtils, FileUtil;

This would only include the units Windows and ShellApi when compiled on Windows, CocoaAll only on MacOS, BaseUnix only on Linux and the Units Classes, SysUtils as well as FileUtil on all systems.

It is important to be careful here that the code makes sense for all of the system combinations. For example, the commas are set here so that the list of the units is always correctly separated with commas, regardless of which condition applies.

List of Operating System Directives

In this table, I have listed the most important directives for each system.

Alle Windows Systeme{$IFDEF WINDOWS}
Windows 64 Bit{$IFDEF WIN64}
Windows 32 Bit{$IFDEF WIN32}
Windows 16 Bit{$IFDEF WIN16}
Classic Macintosh{$IFDEF MAC}
Nintendo DS{$IFDEF NDS}

In addition to these specific directives, you can use {$IFDEF UNIX} to address any unix system. This directive applies to macOS, Mac OS X, FreeBSD, NetBSD, SunOS, OpenBSD as well as other Linux and Unix systems. With {$IFDEF BSD} you can address FreeBSD as well as NetBSD and OpenBSD.

For more directives, please visit Compiler Defines During Compilation on freepascal.org. With those directives, you can for example detect the CPU in use, with which Free Pascal version it is compiled, whether there are certain features available and many other things.


About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile


Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.