Compiler Directives
In the $IFDEF example above, the TEMPOUT identifier is not defined, the $IFDEF evaluates to false and skips the code during compile. The code is never compiled.
Common Examples:
The following are some commonly used compiler directives you should be familiar with:
{$APPTYPE CONSOLE}
The $APPTYPE compiler directive is used in the project source file (.DPR) to tell the compiler to create a Win32 console application or a GUI application. When left out, the default is {$APPTYPE GUI}.
The following is a simple console application:
program Hello;
{$APPTYPE CONSOLE}
uses
SysUtils;
begin
WriteLn('Hello, World!')
end.
{$R *.res} and {$R *.DFM}
The $R*.res compiler directive is frequently found in .DPR source files and the $R *.dfm is found in form source units. The $R compiler directive specifies the name of a resource file to be included. The * indicates to include the base name of the file, no extension, it does not mean to include all files (it is not a wild card character).
{$E cpl}
The $E directive sets the extension of the executable.The following is the project source code from a basic Control Panel Applet. Notice the extension is changed to .cpl. Also note the header starts with library instead of with program. There are three types of headings: program, library, and package.
library Project3;
uses
CtlPanel,
AppletModule1 in 'AppletModule1.pas' {AppletModule1AppletModule: TAppletModule};
exports CPlApplet;
{$R *.RES}
{$E cpl}
begin
Application.Initialize;
Application.CreateForm(TAppletModule1AppletModule, AppletModule1AppletModule);
Application.Run;
end.
Custom Compiler Directives
In addition to using the many built-in compiler directives, you can define your own. Here is a simple example.
//Switch on debug mode.
{$Define MYDEBUGMODE}
{$IfDef MYDEBUGMODE}
ShowMessage('In debug mode.');
{$Else}
ShowMessage('Out of debug mode.');
{$EndIf}
//Switch off debug mode.
{$UnDef MYDEBUGMODE}
{$IfDef MYDEBUGMODE}
ShowMessage('In debug mode.');
{$Else}
ShowMessage('Out of debug mode.');
{$EndIf}
Notice the $IFDEF above does NOT use a Then. If you put one, it will work because Delphi for Win32 sees the Then as a comment. However, Delphi Prism does not and you will get an error.