Bookstore

Recent

Author Topic: $ifdef question  (Read 551 times)

fcu

  • New Member
  • *
  • Posts: 46
$ifdef question
« on: December 18, 2019, 08:27:19 pm »
Hi
does the ifdef inside the main module affect the $ifdefs in the included unit  ?
for example
unit1
Code: Pascal  [Select]
  1. unit unit1;
  2. interface
  3. type
  4. {$ifdef my_type}
  5.  tint = integer;
  6.  {$else}
  7.  tint = smallint;
  8. {$endif}
  9. implementation
  10. end.
  11.  

// main module
Code: Pascal  [Select]
  1. {$define my_type}
  2. uses unit1;
  3. var
  4.  int1 : tint // integer expected but its smallint ?!
  5. begin
  6. end;
  7.  


sash

  • Sr. Member
  • ****
  • Posts: 348
Re: $ifdef question
« Reply #1 on: December 18, 2019, 08:54:37 pm »
Actually, your question is about {$define}. No, it works on per-unit basis, i.e. units listed in uses section will not see it.
Only text included with {$i/include} will, as it actually a part of parent unit.

To make all units see defined symbol, you need to add it in Project Options / Custom options / Defines (or use -dmy_type in CLI).
Lazarus 2.0.6 FPC 3.0.4 x86_64-linux-gtk2 @ Ubuntu 19.10 XFCE

winni

  • Hero Member
  • *****
  • Posts: 1111
Re: $ifdef question
« Reply #2 on: December 18, 2019, 08:56:55 pm »
Hi!

The range of covering for a DEFINE is from the line containing the definition to the end of the unit.

In your example you have  to put the definition  {$define my_type} into unit1.

Winni

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8051
Re: $ifdef question
« Reply #3 on: December 18, 2019, 09:58:17 pm »
The ifdef in unit1 is processed while compiling unit1 with a blank slate and store in binary representation (.o+.ppu).

The main module loads that binary representation so the compiler won't see the original source nor its ifdefs anymore.

fcu

  • New Member
  • *
  • Posts: 46
Re: $ifdef question
« Reply #4 on: December 19, 2019, 05:11:52 pm »
thanks
i thought preprocessors works the same way as C


marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8051
Re: $ifdef question
« Reply #5 on: December 19, 2019, 05:33:35 pm »
thanks
i thought preprocessors works the same way as C

It does, if you use $include, just like #include in C.   But Pascal uses has no equivalent in C.

sash

  • Sr. Member
  • ****
  • Posts: 348
Re: $ifdef question
« Reply #6 on: December 19, 2019, 05:39:32 pm »
i thought preprocessors works the same way as C
It does work the same way. Did you read my post?
C's #include is the same as Pascals' {$include}: both (on pre-compilation state) concatenate multiple text files into a single piece of code as it's seen to compiler.

C simply doesn't have a concept of units and uses.

* @marcov  :) unintentionally crosposted.
« Last Edit: December 19, 2019, 06:20:15 pm by sash »
Lazarus 2.0.6 FPC 3.0.4 x86_64-linux-gtk2 @ Ubuntu 19.10 XFCE

fcu

  • New Member
  • *
  • Posts: 46
Re: $ifdef question
« Reply #7 on: December 19, 2019, 07:56:12 pm »
i thought preprocessors works the same way as C
It does work the same way. Did you read my post?
C's #include is the same as Pascals' {$include}: both (on pre-compilation state) concatenate multiple text files into a single piece of code as it's seen to compiler.

C simply doesn't have a concept of units and uses.

thanks for explanations
so one need to make a global inc file which include the defines , and then used in each unit .

sash

  • Sr. Member
  • ****
  • Posts: 348
Re: $ifdef question
« Reply #8 on: December 19, 2019, 08:20:59 pm »
so one need to make a global inc file which include the defines , and then used in each unit .
A solution to your particular example is either to use global project define, or as @wini wrote.
No {$i} is necessary. Just use unit1 wherever you need your tint type.
Lazarus 2.0.6 FPC 3.0.4 x86_64-linux-gtk2 @ Ubuntu 19.10 XFCE

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8051
Re: $ifdef question
« Reply #9 on: December 19, 2019, 08:29:40 pm »
so one need to make a global inc file which include the defines , and then used in each unit .

For more completed cases: yes. For a handful of simple defines, define them in the project so they get passed to the compiler.