Show: Delphi C++
Display Preferences

Delphi compiler directives

From RAD Studio
Jump to: navigation, search

Go Up to Delphi Compiler Directives (List) Index

Each Delphi compiler directive is classified as one of the following:

A compiler directive is a comment with a special syntax. The compiler directive starts with a $ (dollar-sign) as the first character after the opening comment delimiter, immediately followed by a name (one or more letters) that designates the particular directive. You can include comments after the directive and any necessary parameters.

Compiler directives can be placed wherever comments are allowed. Note that comments beginning with the double-slash // cannot be used for compiler directives. For example:

{$hints off}        // a Delphi compiler directive
(*$hints off*)      // a Delphi compiler directive
//$hints off        // just a commentary, ignored

All directives, except switch directives, must have at least one space between the directive name and the parameters.

The three types of directives are described in the following paragraphs.

Contents

Switch Directives

Switch directives turn particular compiler features on or off. You can specify the directive using either its single-letter equivalent or its long name:

  • For the single-letter version, you add either + or - immediately after the directive letter. For example: {$A+}
  • For the long version, you supply the word "on" or "off." For example: {$ALIGN ON}

Switch directives are either global or local:

  • Global directives affect the entire compilation and must appear before the declaration part of the program or the unit being compiled.
  • Local directives affect only the part of the compilation that extends from the directive until the next occurrence of the same directive. They can appear anywhere.

Switch directives can be grouped in a single compiler directive comment by separating them with commas with no intervening spaces. For example:

{$B+,R-,S-}

This grouping does not work for long-named directives, which must be entered individually. For example:

{$optimization on,hints off}        // everything starting with "," is treated like a comment here

Parameter Directives

Parameter directives specify parameters that affect the compilation, such as file names and memory sizes.

Conditional Directives

Conditional directives cause sections of code to be compiled or suppressed based on specified conditions, such as user-defined conditional symbols.

Examples

Here are some examples of compiler directives:

{$B+}
{$R- Turn off range checking}
{$I TYPES.INC}
{$M 32768,40960}
{$DEFINE Debug}
{$IFDEF Debug}
{$ENDIF}

Various Ways You Can Enter Compiler Directives

Note: Directives embedded in the source code have priority over directives specified on the command line or in Project > Options > Delphi Compiler > Compiling.

In source code

You can insert compiler directives directly into your source code.

Here is an example of conditional compilation in the Math.pas file (in your product \source directory):

 procedure DivMod(Dividend: Cardinal; Divisor: Word;
   var Result, Remainder: Word);
 {$IFDEF PUREPASCAL}
 begin
   Result := Dividend div Divisor;
   Remainder := Dividend mod Divisor;
 end;
 {$ELSE !PUREPASCAL}
 {$IFDEF X86ASM}
 asm // StackAlignSafe
         PUSH    EBX
         MOV     EBX,EDX
         MOV     EDX,EAX
         SHR     EDX,16
         DIV     BX
         MOV     EBX,Remainder
         MOV     [ECX],AX
         MOV     [EBX],DX
         POP     EBX
 end;
 {$ENDIF X86ASM}
 {$ENDIF !PUREPASCAL}

Using command-line switches

You can also change the default directives using the -$directive switch with the command-line compiler, dcc32, and for the IDE, bds.exe.

When using the command-line compiler, you can specify compiler directives on the command line; for example,

DCC32 -$R+ MYPROG

Using the IDE

You can set many of the compiler directives on the Project > Options > Delphi Compiler > Compiling dialog box. Any changes you make to the settings on the Compiling page affect all units whenever their source code is recompiled in subsequent compilations of that project. If you change a compiler switch and compile, all units for which you have source code are recompiled with the new settings.

If you are working in the Code Editor and want a quick way to see the compiler directives that are in effect, press Ctrl+O+O. The current directives settings are inserted in a horizontal line at the top of your file, along with other current settings, such as compiler warnings (the settings for the $WARN directive). These lists are added to the beginning of your source file unless you do an undo (Ctrl+Z) or manually delete the list:

Directives.png

See Also

Personal tools
In other languages