8

I am looking for a predefined symbol to write a code like that:

{$IFDEF LAZARUS}
// code compiles by fpc/lazarus
{$ELSE}
// code compiles by delphi
{$ENDIF}
Arioch 'The
  • 15,799
  • 35
  • 62
kludg
  • 27,213
  • 5
  • 67
  • 118

2 Answers2

14

Use FPC             

{$IFDEF FPC}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    `FPC` is defined by the compiler rather than the IDE. So it will be correct for all IDEs that use FPC as their compiler. – David Heffernan Aug 30 '12 at 15:15
  • 1
    Yes I don't think there's a directive to detect the IDE at all. After all, the IDE is just an enriched Notepad to type your code in. – GolezTrol Aug 30 '12 at 15:19
  • IDE itself could possibly define a conditional symbol, and store it in compiler options. – kludg Aug 30 '12 at 15:22
  • What does it matter which IDE is invoking the compiler? – David Heffernan Aug 30 '12 at 15:29
  • Maybe he thinks IDE = widgets library, hence Lazarus = LCL. – Arioch 'The Aug 30 '12 at 15:43
  • I also think the code should check for **both FPC and Delphi**. There is GNU Pascal, there is Virtual Pascal, OpenSibil, MIDlet Pascal and who knows what else, even Oxygene is compatible to some point. I am certain that both FPC and DELPHI should be checked. Maybe they should be checked once in the header, and if none defined then abort compilation due to non-supported compiler. But at lease once Delphi presence should be ensured – Arioch 'The Aug 30 '12 at 15:45
  • 2
    I am porting a unit test project to Lazarus and need ex to use different unit names in `uses` clause. DUnit implementation is specific to IDE. – kludg Aug 30 '12 at 15:51
  • I am currently experimenting with built-in Lazarus GUI testrunner – kludg Aug 30 '12 at 15:57
  • 1
    @Arioch: starting in XE2 (or maybe XE, I forget which), the Delphi compiler started defining the `DCC` conditional for itself. Prior to that, there was no defining conditional to identify the Delphi compiler, except indirectly via the version-specific `VERxxx` conditionals. – Remy Lebeau Aug 30 '12 at 17:05
  • 1
    @GolezTrol: not so simple as in Delphi you may have some code related to the IDE itself, ie: when you use ToolsAPI. – az01 Sep 01 '12 at 20:08
  • @az01 That is still compiled using the Delphi compiler (for the Delphi IDE) or by FreePascal if you compile (for) Lazarus. – GolezTrol Sep 01 '12 at 22:27
  • Maybe in combination with a define for designtime parts of components – Marco van de Voort Sep 10 '12 at 11:27
1

For GUI applications afaik the "LCL" symbol is defined inside Lazarus projects. In this case it probably won't matter.

In general, for bigger codebases, I would avoid having too much ifdef FPC/LCL and ifdef in your sourcecode though. It makes adding an exception or other version harder.

Use a system like JCL and Zeos(7) are using, where you give most differences an own name (like "USE_FPCUNIT" or "USE_DUNIT") and link these to versions in a central includefile.

For a short treatise on the subject see http://www.stack.nl/~marcov/porting.pdf (chapter 2)

P.s. I would consider Pocketstudio, TP,GPC,VP and WDSybil (and whatever I forgot) dead for most practical purposes and the bytecode variants Canterbury Pascal/Component Pascal/Oxygene/Prism/Delphi.NET incompatible (most are more Oberon than Pascal anyway). That pretty much leaves Delphi, Kylix and FPC to worry about.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89