Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi compiler Directive list, do we have to place directives before the unit name?

im working on application using delphi 7, and i just came across this

   {$A+,B-,C+,D+,E-,F-,G+,H+,I+,J+,K-,L+,M-,N+,O-,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1}
   {$MINSTACKSIZE $00004000}
   {$MAXSTACKSIZE $00100000}
   {$IMAGEBASE $00400000}
   {$APPTYPE GUI}

   unit fmMain; // this is the main form of the project

   interface

   uses
     //..all the code is here
   end.  

i know there are these Compiler Directive and Delphi Compiler Directives list

the author of the code has placed the so many compiler directives before the main unit name.

can any one tell me

  1. Whats the use of having Directives before unit name, does this make them global?
  2. And can we create our own directives in some particular situations?
  3. where are the compiler directives defined?
like image 252
PresleyDias Avatar asked Dec 22 '22 03:12

PresleyDias


2 Answers

  • The key topic explaining the principles is here: Delphi compiler directives.
  • The compiler directives are listed here: Delphi Compiler Directives (List).

As to whether or not the directive has effect over the whole project, an entire unit, an single function and a region with a function, that varies from directive to directive. You have to read the documentation for each directive to know what its scope is.

You ask if they need to be placed right at the start of the unit. You need to take account of the scope of the directive. Another part of the documentation has this to say regarding switch directives:

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.

However, consider the DENYPACKAGEUNIT directive (emphasis mine):

The {$DENYPACKAGEUNIT ON} directive prevents the Delphi unit in which it appears from being placed in a package.

If this directive has unit-wide scope then it merely needs to be present in the unit to take effect.

So it can matter where the directive is placed. The bottom line is that, for each directive, you need to know its scope and in order to do so you must consult the documentation for that directive.

Note that you only need to set directives in code if you wish to change the settings from those made in the project settings. It's perfectly reasonable to set the options in the project options and not set them in code.


What appears to have happened in the code you present is that the author typed CTRL+O O and the IDE inserted the various settings as defined in the project options at that instant in time.

like image 69
David Heffernan Avatar answered Dec 24 '22 00:12

David Heffernan


  1. Whats the use of having Directives before unit name, does this make them global?

If by global you mean project-wide, the answer is NO. Each unit is compiled on it's own.

  1. And can we create our own directives in some particular situations?

They're called compiler directives because the compiler interprets them. You can't create your own directives unless you're at Embarcadero and you change the compiler.

  1. where are the compiler directives defined?

They're defined in the compiler's sources. They're documented in the compiler documentation, and you found a grate link already (your second link)

like image 23
Cosmin Prund Avatar answered Dec 24 '22 02:12

Cosmin Prund