Where do I define symbols tested with {$IFDEF}?

末鹿安然 提交于 2019-12-04 00:26:52

It's in the Conditional Defines slot under Project | Options, which looks like this on D2010:

Other answers have pointed you at the places to define symbols and the scope implications of the different approaches.

However, what no-one has yet mentioned is that if you change the DEFINE symbols you MUST do a FULL BUILD of your project for them to have any effect on your code.

When you "Compile" the Delphi compiler will only compile units which have themselves changed since the previous compile. If you change DEFINE symbols this doesn't change any project units, so if the units are not re-compiled then the change in DEFINE symbols will not have ANY effect in those units.

To FORCE changes in DEFINE symbols to be applied in ALL units, you MUST "build", not compile.

This may explain why your attempt to set defines did not appear to work previously

You can also define them in {$DEFINE <symbol>} directives. What changes is the scope. When you define a <symbol> under conditional defines in the project options, the scope is global to the whole project. $DEFINE directives are valid only from the point they are declared to the end of the current module, or until an $UNDEF directive using the same <symbol> is encountered. What to use depends on your needs, and what the IFDEF does.

There are two places where you can put conditional defines that are used in all units of a project:

  1. in the project options (as David Heffernan already said)
  2. in an include file that is included in all of these units

Why do I mention the second option? Because it allows specialized processing based on the VERxxx conditional define and other conditional defines given in 1. See jedi.inc (from the Jedi JCL) for an example.

Also, as Deltics said: When it determines which units to recompile, the compiler only checks whether the unit itself has changed, not whether the conditional defines or any include files have changed. So if you change conditional defines, you must do a rebuild, not just a recompile. Since the Delphi compiler is very fast, this fortunately does not make much of a difference for compile times.

You can define global symbols in external file with .inc extension. Create a new text file, put in it all you defines and save it as for instance Predefines.inc:

--- Content of the file Predefines.inc ---

{$DEFINE Symbol}
{$IFDEF Symbol}
  {$DEFINE AnotherSymbol}
{$ENDIF}

In you Delphi modules, where you need to check are symbols defined, put this code in interface section:

interface

{$I Predefines.inc}

uses ...

// Check you defines

{$IFDEF Symbol}
...
{$ENDIF}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!