Logo Passei Direto

A maior rede de estudos do Brasil

7 pág.
Compiler Directives

Pré-visualização | Página 1 de 3

Compiler Directives
Controlling the Delphi Compiler
They look similar to comments, but they aren't. And nearly every project has at least one, even the default project Delphi creates when you first load it. They are compiler directives, and they are the focus of this article. 
You use compiler directives to control how Delphi's compiler performs its task. For example, the {$R} compiler directive instructs the compiler to link a specified .res file into the resulting executable. By default, every unit associated with a form includes at least one {$R} compiler directive, which tells Delphi to include a binary version of the form's .dfm file in the compiled .dcu file as a Windows resource. 
You can control compiler directives in one of two ways. The first way is to use the checkboxes on the Compiler page of the Project Options dialog box, as shown in Figure 1. To see this page, select Project | Options from Delphi's main menu, and then select the Compiler tab. 
The second way is to embed compiler directives directly in your code. Embedded compiler directives look a lot like braced comments. The only difference is that compiler directives have a dollar sign ($) immediately following the opening brace. Like all other statements interpreted by the compiler, however, if the {$ combination appears within a comment, it's ignored. 
There are three types of compiler directives: switch, parameter, and conditional. Switch directives turn a particular feature on or off. The {$H+} directive is a switch directive. Switch directives are turned on when the directive is followed by a plus sign (+) or the word ON, and turned off when it is followed by a minus sign (-) or the word OFF. In the case of the {$H} compiler directive, it instructs the compiler to treat all string declarations as ANSIStrings when turned on, and as Pascal-style strings when turned off. 
Parameter directives identify a file name, file extension, or setting required by the compiler. The ($R *.RES} resource compiler directive is one example of a parameter directive. Conditional directives permit you to instruct the compiler to conditionally compile a segment of code. The {$IFDEF} directive is a conditional directive. 
Compiler directives can also be categorized by their scope, which is either local or global. Global scope directives apply to the compilation of the entire project. For example, the {$APPTYPE CONSOLE} directive instructs the compiler to build a console application. Local scope directives, by comparison, apply to only part of the code being compiled. For instance, a specific section of code can be compiled with the {$H-} directive, instructing the compiler to treat all declared string variables as short strings in that section, and with {$H+} for the remainder of the project. 
Every project has a default set of compiler directives, defined by the values that appear on the Compiler page of the Project Options dialog box. If you like, you can instruct Delphi to explicitly insert the current compiler directives into a unit. To do this, press [Ctrl][O][O] (oh-oh, not zero-zero). An example of a unit where [Ctrl][O][O] has been pressed is shown in Figure 2. 
Figure 2: Press [Ctrl][O][O] in the editor to insert your project's default compiler directives at the top of the current unit. 
As you inspect Figure 2, you will notice that the first compiler directive includes a large number of switches. When you insert switch compiler directives manually, you can enter each separately, each contained in its own set of braces, or include a comma-separated list of the switch directives in a single set of braces. 
Selected Compiler Directives
The following sections take a look at a sample of compiler directives, but because many of the compiler directives are self-explanatory, I will make no attempt to be comprehensive. Instead, I'll focus on those compiler directives that I find most interesting, fun, or useful. For information on compiler directives not discussed here, you can press [F1] on the Compiler page of the Project Options dialog box, or select "compiler directives" from Delphi's online help index. 
Suppressing Hints and Warnings
Delphi's compiler will generate hints and warnings that can help you write better code. For example, if you declare a local variable in a method, but never use it, the compiler will display a hint following compilation. It will note potentially more troublesome problems with warnings. 
Sometimes these generated hints and warnings are annoying. For example, you may have a function that really does return a value in every case, but the compiler cannot detect this. In these cases, you can turn hints and/or warnings off. 
The {$HINTS} and {$WARNINGS} compiler directives are local switch directives. To turn hints off use: 
To turn them back on use: 
For warnings use: 
Consider the function shown in the code segment in Figure 3. It simplifies the launching of an executable. Even though the first line of the function sets the Result variable to False, the compiler will generate a hint saying that the return value of the function may not be set. Since you know that the function always returns a value, you can turn off this annoying hint using the {$HINTS} compiler directive. 
function LaunchApp(const AppName: string): Boolean; 
  ProcessInfo: TProcessInformation; 
  StartUpInfo: TStartupInfo; 
  Result := False; 
    FillMemory(@StartupInfo, SizeOf(StartupInfo), 0); 
    StartupInfo.cb := SizeOf(StartUpInfo); 
    Result := CreateProcess(nil, PChar(AppName), nil, nil,
      False, NORMAL_PRIORITY_CLASS, nil, nil, StartUpInfo, 
Figure 3: Turning compiler hints off for one method. 
Automatically Compiling Resource Files
Until Delphi 4, the use of resource scripts to compile Windows resource files required that you compile the resource scripts manually using the Borland Resource Command-line Compiler (BRCC32.EXE). Unfortunately, the manual nature of this compilation made it possible for you to change a resource script, forget to recompile it, and then link the old resource file into your application using the {$R} resource compiler directive. 
A new version of the resource compiler directive was introduced in Delphi 4. This version is shown in the following example, which must be placed in your project's .dpr file: 
{$R 'filename.res' 'filename.rc'}
When the Delphi compiler encounters this compiler directive, it performs a number of valuable tasks. First it checks to see if the named .res file exists. If it doesn't, it automatically compiles the named .rc file to create the .res file. If the .res file already exists, the compiler compares the timestamps of the .res and .rc files. If the .rc file is more recent, it recompiles the .rc file into a new .res file. As a result, when you link resources using this version of the resource compiler directive, you're assured of always using the latest version of your resource script. 
Instead of manually adding this version of the resource compiler directive to your project source, you can use the Project Manager to generate it. With your project open in the Project Manager, right-click the node associated with your project and select Add. The Project Manager responds by displaying the Add to project dialog box shown in Figure 4. 
Figure 4: Adding a resource script to a project in the Project Manager inserts the enhanced compiler directive into your .dpr file. 
Set the File of type to Resource File (*.rc). Then select your resource file and click the Open button. In response,