[fpc-pascal] optionally uses a unit

Tomas Hajny XHajT03 at hajny.biz
Mon Nov 18 09:42:59 CET 2013


On Mon, November 18, 2013 07:38, Xiangrong Fang wrote:


Hi,

> Is it possible to optionally use a unit? e.g.
>
> unit myunit;
> uses {$ifexists persist.pas}persist,{$endif} Classes;
>
> type
>   TMyClass = class
>   public
>     {$ifexists persist.pas}
>     procedure SaveToStream(s: TStream);
>     {$endif}
>   end;

Isn't {$IFDEF PERSIST}... combined with calling the compiler with/without
-dPERSIST depending on availability of the source sufficient (and more
general)? Obviously, you can test availability of the source file in a
makefile/shell script/batch file/... and have the -dPERSIST added to
compiler options accordingly.

BTW, your example (and partly the whole concept) might give
unexpected/unwanted results - what if a precompiled version (.ppu/.o) of
that unit is available but not the source file? Which directories should
be checked when searching the file (note that the compiler couldn't know
if the filename mentioned should belong to a unit source file, specific
include file, compiled object, library or even a completely independent
file, and that different search paths are used for searching files of
different types)?

Tomas





More information about the fpc-pascal mailing list