Compiler Directives

Tutorial on compiler directives.

In this tutorial I'm going to talk a little bit about compiler directives.

First of all, what are compiler directives?

Compiler directives are the red lines you see at the very top of most scripts. They tell Simba to do something before or while compiling a script before it's run.

When you press the start button in Simba first the script is compiled and once compiled it runs.

Some of the most common compiler directives you will see while using Simba are:

{$INCLUDE} //can be shortened to {$I}

All of this except {$ELSE} and {$ENDIF} need some other parameter before the closing curly bracket.

This will be the most common you will see in Simba but for a more complete list check

There's 2 main types of compiler directives in this list. Directives for conditional compiling and directives to include stuff in in our script.

Let's talking about the second type which is the most straight forward one.

This are:

{$INCLUDE SRL/osr.simba} //used to import a file into our script.
{$LOADLIB ../plugins/libremoteinput} //used to load a plugin

The first one, can be shortened to {$I SRL/osr.simba} and it simply is telling Simba to import the file specified. Any code you have automatically running in that file, will run the moment the file is imported. For example, with SRL/osr.simba imported, SRL.Setup is automatically run.

The second one you probably won't use often but it's used to load plugins. which are usually .dll files in windows. And works the similarly as INCLUDE.

Now let's talk about the second type of directives, conditional compilation.

It's really what it sounds like...

if THIS then
  compile X
  compile Y

We are going to start with {$DEFINE} and {$UNDEF} and I'm going to attempt explaining things with my own words. If you need more information please check the FPC Manual.

Anyway, when you use DEFINE you are defining a symbol that will be recognized from then on by the compiler. If at any moment you want to remove that symbol you use UNDEF.

The syntax is the following:


When a symbol is defined you can use if/else instructions with it for the compiler. Example:


WriteLn 'MY_SYMBOL is defined';
WriteLn 'MY_SYMBOL is not defined';

This little script will only ever run the first WriteLn. If you were to remove the DEFINE or undefine your symbol with UNDEF before the if statement then only the second part would run.

Okay, so you might be asking yourself, but why is this any good? What can this do that you can't just regularly do in lape?

Well you can do a conditional import for example!

Let's say you have a library that has a different version for windows and for linux. You can do this:

{$IFDEF WINDOWS} //WINDOWS is auto declared in windows.
{$I windows.simba}
{$I linux.simba}

You can get pretty creative with this and you can also try using IFHASFILE to check if a file exists.

Keep in mind that any IF statement will always need a closing ENDIF.

And this is it for this tutorial, I hope you got the basics of the compiler directives 😄