Recent

Author Topic: undef not working?  (Read 863 times)

srvaldez

  • Full Member
  • ***
  • Posts: 127
undef not working?
« on: February 21, 2024, 05:08:22 pm »
it seems that undef doesn't work or perhaps I am doing something wrong?
Code: [Select]
program undef_test;

var
MySymbol:longint;
{$undef MySymbol} 
{$ifndef Mysymbol} 
Mysymbol:double;
{$endif}
begin
writeln(sizeof(Mysymbol));
end.

TRon

  • Hero Member
  • *****
  • Posts: 4337
Re: undef not working?
« Reply #1 on: February 21, 2024, 05:37:24 pm »
Defines work with (compile time) symbols not identifiers/variables.

Code: Pascal  [Select][+][-]
  1. program undef_test;
  2.  
  3. {$define this_symbol}
  4. var
  5.   {$ifdef this_symbol}
  6.   variable_identifier : longint;
  7.   {$undef this_symbol}
  8.   {$else}
  9.   variable_identifier : double;
  10.   {$endif}
  11.  
  12. begin
  13.   {$if declared(variable_identifier)}
  14.   writeln(sizeof(variable_identifier));
  15.   {$endif}
  16. end.
  17.  

See Compiler defines during compilation for more information on defines that are present during compilation. That is besides symbols that you can declare yourself (as per example or at the command-line with option -d)

If you want to check if a Pascal symbol is declared or not the you can use {$if declared(symbol_name)}, see also Compile time expressions, in particular the chapter Definition

edit: modified example code to make the example more clear and added if declared example.
« Last Edit: February 21, 2024, 06:59:54 pm by TRon »
Today is tomorrow's yesterday.

srvaldez

  • Full Member
  • ***
  • Posts: 127
Re: undef not working?
« Reply #2 on: February 21, 2024, 06:30:09 pm »
thanks TRon :)

 

TinyPortal © 2005-2018