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

前端 未结 5 1335
半阙折子戏
半阙折子戏 2021-02-20 00:52

When I use Delphi directives in code, like:

{$IFDEF something}
.
.
.
{$ENDIF}

Where do I assign the word \'something\' in the project? I tried

相关标签:
5条回答
  • 2021-02-20 01:30

    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

    0 讨论(0)
  • 2021-02-20 01:34

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

    Delphi Project Options dialog

    0 讨论(0)
  • 2021-02-20 01:37

    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.

    0 讨论(0)
  • 2021-02-20 01:49

    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.

    0 讨论(0)
  • 2021-02-20 01:51

    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}
    
    0 讨论(0)
提交回复
热议问题