How to do conditional compilation with macros

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to do conditional compilation with macros

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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to do conditional compilation with macros

Marco van de Voort
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}
 
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to do conditional compilation with macros

Sven Barth-2
In reply to this post by Reinier Olislagers
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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to do conditional compilation with macros

Reinier Olislagers
On 18-9-2011 13:41, Sven Barth wrote:

> 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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to do conditional compilation with macros

etrusco
In reply to this post by Marco van de Voort
On Sun, Sep 18, 2011 at 8:35 AM, Marco van de Voort <[hidden email]> wrote:

> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to do conditional compilation with macros

Marco van de Voort
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)
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to do conditional compilation with macros

etrusco
On Sun, Sep 18, 2011 at 9:19 AM, Marco van de Voort <[hidden email]> wrote:

> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal