Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2002-08

defines do not apply to units

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

I am new to FreePascal, so please be tolerant
if this is a feature, but it looks like a bug to me.

The unexpected behavior is that {$Define Sym}
statements given in a main program do not stay
defined for a unit compiled in the same run.

Below is an example involving 3 very short files
to illustrate the problem.

When I compile and run the program TestDefs.Pas
with FreePascal v 1.0.6 on Win32, I expect the writelns to say that the symbols are defined. Instead, they say that the symbols are undefined.

***Main program file: TestDefs.pas:

{$I TestDefs.Inc}
Program MyProg;
Uses TestUnit;
Begin
LargeMemCheck;
UseCRTCheck;
ProcPtrsCheck;
End.
***Include file with defines: TestDefs.Inc:
{$Define FPC}
{ Conditional defines for FreePascal (version 1.0.6): }
{$IfDef FPC}
{$Define LargeMem}
{$Define UseCRT}
{$Define ProcPtrs}
{$EndIf}
***Unit file: TestUnit.Pas:
Unit TestUnit;
Interface
Procedure LargeMemCheck;
Procedure UseCRTCheck;
Procedure ProcPtrsCheck;
Implementation
Procedure LargeMemCheck;
Begin
{$IfDef LargeMem}
Writeln('LargeMem is defined.');
{$Else}
Writeln('LargeMem is undefined.');
{$EndIf}
End;
Procedure UseCRTCheck;
Begin
{$IfDef UseCRT}
Writeln('UseCRT is defined.');
{$Else}
Writeln('UseCRT is undefined.');
{$EndIf}
End;
Procedure ProcPtrsCheck;
Begin
{$IfDef ProcPtrs}
Writeln('ProcPtrs is defined.');
{$Else}
Writeln('ProcPtrs is undefined.');
{$EndIf}
End;
End.

It is not difficult to work around (include the
defines file within each unit), but that seems a bit ugly.

Jeff Miller

Reply by email

Marco van de Voort
to Unknown author
I am new to FreePascal, so please be tolerant if this is a feature, but it looks like a bug to me.

Point taken,

The unexpected behavior is that {$Define Sym} statements given in a main program do not stay defined for a unit compiled in the same run.

This is normal behaviour, contrary to languages that don't have any scoping what..so..ever.

The classic workarounds are :

It is not difficult to work around (include the defines file within each unit), but that seems a bit ugly.

1. Indeed, check.

2. Make sure your makefile passes the define to each unit.

Seriously, this is for a reason. With the FPC (and any other Wirthian (pascal-like)) language with a unit/module system, you can't predict the exact way the different files/units are compiled, so therefore defines are restricted to that compilationunit.

This is the downside of an actual generally postive feature ( the compile figures the right compilation order, and is generally right about it), with the above 2 workarounds as an alternative.

Reply by email