Sign in Join

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

Mailing lists / fpc-pascal / 2011-09

How to do conditional compilation with macros

7 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

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

Reinier Olislagers

Trying to test for a certain minimum version of FPC.

Have got FPC version 2.7.1 [2011/09/17] for i386 on Windows.

What am I doing wrong?

program conditional;
{$mode objfpc}{$H+}
uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes;
begin
  //How should I use FPCVULLVERSION in conditional compilation?
  {$IF $FPCFULLVERSION > 20200}
  writeln('Version newer than 2.2');
  //Error: Compile time expression:
  //Wanted BOOLEAN but got INTEGER at IF or ELSEIF
  {$ENDIF}
  //so maybe the IF wants a precooked boolean result
  //let's try some brackets
  //Tried to apply Programmer's Guide 2.4 on Compile time expressions:
  {$MACRO+}
  {$IF (FPCFULLVERSION > 20200)}
  //Error: Compile time expression Wanted STRING but got INTEGER at
  //"FPCFULLVERSION > 20200"
    writeln('Version newer than 2.2 another try');
  {$ENDIF}
  //adding a $:
  {$IF ($FPCFULLVERSION > 20200)}
  //Error: Syntax error while parsing a conditional compiling expression
    writeln('Version newer than 2.2 yet another');
  {$ENDIF}
end.
Reinier Olislagers
to Sven Barth
Sven Barth wrote 10 lines
On 18.09.2011 13:29, Reinier Olislagers wrote: > What am I doing wrong? Additionally to what Marco wrote: You must not use the "$" as prefix for the "variable" if you reference such a compiler define inside an $if or $ifdef. Regards, Sven

Thanks guys - reading manuals is not always my strong point ;)

This works:
program conditional;
{$mode objfpc}{$H+}
begin
  {$IF FPC_FULLVERSION > 20200}
  //apparently FPC_FULLVERSION was introduced
  //in FPC 2.2.4 though...
  //http://delphi.wikia.com/wiki/FreePascal_detection_and_versioning
  writeln('Version newer than 2.2');
  {$ENDIF}
end.
Flávio Etrusco
to Marco van de Voort: In our previous episode, Reinier Olislagers said: Not reading manuals? :-)…
Marco van de Voort wrote 22 lines
In our previous episode, Reinier Olislagers said: > > Have got FPC version 2.7.1 [2011/09/17] for i386 on Windows. > > What am I doing wrong? Not reading manuals?  :-) http://www.freepascal.org/docs-html/prog/progse5.html#x121-1210002.2 so it is FPC_FULLVERSION, not FPCFULLVERSION from http://delphi.wikia.com/wiki/FreePascal_detection_and_versioning a typical usage: {$if FPC_FULLVERSION> 20204}   // means greater than 2.2.4 here  {$ifndef}

Not wanting to hijack the thread but, why isn't there a way to print macro values? (There isn't, is it?)

Best regards,
Flávio

Marco van de Voort
to Flávio Etrusco

In our previous episode, Fl?vio Etrusco said:

Show quoted text 11 lines
> http://delphi.wikia.com/wiki/FreePascal_detection_and_versioning > > a typical usage: > > {$if FPC_FULLVERSION> 20204} > ? // means greater than 2.2.4 here > ?{$ifndef} > Not wanting to hijack the thread but, why isn't there a way to print macro values? (There isn't, is it?)

No. http://bugs.freepascal.org/view.php?id=12935

I still think this is a good thing, mostly because this is a way to get versions etc embedded in the program into the binary without going through an includefile processor.

so

 fpc  -dfreebsdversion:=`uname -r`
and then
  {$i %freebsdversion%}

According to Jonas it can also be done via the environment, and for me that was enough back then (which is why the report is closed). But in retrospect that is fine for commandline, but harder for IDE usage. (which often allows to set additional cmdline options, but not specify the environment, specially not on all targets)

Flávio Etrusco
to Marco van de Voort
Marco van de Voort wrote 32 lines
In our previous episode, Fl?vio Etrusco said: > > http://delphi.wikia.com/wiki/FreePascal_detection_and_versioning > > > > a typical usage: > > > > {$if FPC_FULLVERSION> 20204} > > ? // means greater than 2.2.4 here > > ?{$ifndef} > > > > Not wanting to hijack the thread but, why isn't there a way to print > macro values? (There isn't, is it?) No. http://bugs.freepascal.org/view.php?id=12935 I still think this is a good thing, mostly because this is a way to get versions etc embedded in the program into the binary without going through an includefile processor. so  fpc  -dfreebsdversion:=`uname -r` and then  {$i %freebsdversion%} According to Jonas it can also be done via the environment, and for me that was enough back then (which is why the report is closed). But in retrospect that is fine for commandline, but harder for IDE usage. (which often allows to set additional cmdline options, but not specify the environment, specially not on all targets) _______________________________________________

Thanks for the information and tips. I hadn't thought about using environment variables, maybe I'll add it to Lazarus :)

Best regards,
Flávio