Recent

Author Topic: [SOLVED] Howto use a DEFINE present in an installed package  (Read 4709 times)

tintinux

  • Sr. Member
  • ****
  • Posts: 351
    • Gestinux
[SOLVED] Howto use a DEFINE present in an installed package
« on: November 25, 2024, 04:27:36 pm »
Hi

I have installed the package ZeosLib in the EDI.
In my projects, I need to know and test the version installed, to make them more independent of the package version and avoid compatibility breaks.

In a source of the ZeosLib package (Zeos.inc) there is
Code: Pascal  [Select][+][-]
  1. {$DEFINE ZEOS80UP}
which could help me if I could write in my projects
Code: Pascal  [Select][+][-]
  1. {$IF ZEOS80UP}
  2. // Code for 8.0
  3. {$ELSE}
  4. // Code for older version
  5. {$ENDIF}

Before, I have to add in my source
Code: Pascal  [Select][+][-]
  1. {$I  zeosfolder/Zeos.inc}


But, the exact root path of the package sources (zeosfolder) can change on every installation.
Is there not a way to know where are the source of the installed package, or a way to find them without adding a hard coded path in the options ?

Thanks for your suggestions.

[Of course, this is a general issue, not a ZeosLib issue]

« Last Edit: December 02, 2024, 04:55:34 pm by tintinux »
Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8083
Re: Howto use a DEFINE present in an installed package
« Reply #1 on: November 25, 2024, 04:35:45 pm »
I don't think you can. you can check for a {$define ... } in the same unit using {$ifdef ... } (where the define might actually be in a .inc file), but the best you can do across units is using {$if declared( ... ) } at compilation time, or a version-returning function at runtime.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TRon

  • Hero Member
  • *****
  • Posts: 3756
Re: Howto use a DEFINE present in an installed package
« Reply #2 on: November 25, 2024, 05:13:26 pm »
You can set a specific include dir to search in in the project options.

Another suggestion might be to add the define to the compiler options in case it is sure that the version matches.
I do not have to remember anything anymore thanks to total-recall.

tintinux

  • Sr. Member
  • ****
  • Posts: 351
    • Gestinux
Re: Howto use a DEFINE present in an installed package
« Reply #3 on: November 26, 2024, 10:19:30 am »
You can set a specific include dir to search in in the project options.

Another suggestion might be to add the define to the compiler options in case it is sure that the version matches.
No, this is what I would like to avoid : specific include directory or specific compiler option.
I wondered whether if the compiler could find the directory where the package sources have been downloaded, or the version of the package.
Thanks
Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8083
Re: Howto use a DEFINE present in an installed package
« Reply #4 on: November 26, 2024, 11:33:53 am »
The compiler doesn't /find/ anything, it needs to be told about paths either on the command line or in fpc.cfg.

In principle you can put a path in either a uses clause or in an include directive. However experience shows that this is risky: there is a problem that's never been made adequately reproducible either in FPC or possibly the Lazarus IDE whereby it doesn't notice that a unit has changed and refuses to recompile it.

I've also seen situations where if you pull in a single include file it insists on recompiling a whole lot of stuff that shouldn't be. That might be specific to some versions of the Lazarus IDE, or might be related to the above problem.

As far as I am aware, you can't specify the value of a define on the command line: it has to be there or not there.

So it boils down to the fact that either you have to use something like

Code: Pascal  [Select][+][-]
  1. (* This is an experimental replacement for the RTL's Now() based on the POSIX
  2.   clock.
  3. *)
  4. function posixNow(): TDateTime;
  5.  
  6. var     ts: timespec;
  7.         days: extended;
  8.  
  9. begin
  10.   result := UnixEpoch;
  11.  
  12. (* This test appears to be unreliable for functions provided directly by the *)
  13. (* kernel or a standard system library.                                      *)
  14.  
  15. {$if defined(clock_gettime) }
  16.   if clock_gettime(CLOCK_REALTIME, @ts) = 0 then begin
  17.     days := ts.tv_sec / SecsPerDay;
  18.     days += ts.tv_nsec / SecsPerDay / 10E9;
  19.  
  20. (* Since even the extended type doesn't really have enough digits to express *)
  21. (* times relative to the unix epoch in nanoseconds, assume we have to be     *)
  22. (* very careful with the evaluation order.                                   *)
  23.  
  24.     result := days + UnixDateDelta
  25.   end
  26. {$endif                     }
  27. end { posixNow } ;
  28.  

or you have to use an external tool to build a compiler/project configuration, plus possibly a .inc file containing macros of the various things you need.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

CharlyTango

  • Jr. Member
  • **
  • Posts: 91
Re: Howto use a DEFINE present in an installed package
« Reply #5 on: November 28, 2024, 09:56:45 am »

which could help me if I could write in my projects
Code: Pascal  [Select][+][-]
  1. {$IF ZEOS80UP}
  2. // Code for 8.0
  3. {$ELSE}
  4. // Code for older version
  5. {$ENDIF}


ZEOS provides a version-variable you can use:

Code: Pascal  [Select][+][-]
  1.     if (ZEOS_MAJOR_VERSION >= 8) then
  2.        //code for 8.0 following
  3.     else
  4.       // Code for older version
  5.     ;
Lazarus stable, Win32/64

MarkMLl

  • Hero Member
  • *****
  • Posts: 8083
Re: Howto use a DEFINE present in an installed package
« Reply #6 on: November 28, 2024, 12:19:23 pm »
ZEOS provides a version-variable you can use:

Code: Pascal  [Select][+][-]
  1.     if (ZEOS_MAJOR_VERSION >= 8) then
  2.        //code for 8.0 following
  3.     else
  4.       // Code for older version
  5.     ;

That's not a compile-time operation, so is NBG if he wants to refer to things which exist only in a later (or earlier) version or have different parameter lists etc.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11980
  • FPC developer.
Re: Howto use a DEFINE present in an installed package
« Reply #7 on: November 28, 2024, 12:57:33 pm »
If it is a proper constant you can also test them with $if

tintinux

  • Sr. Member
  • ****
  • Posts: 351
    • Gestinux
Re: Howto use a DEFINE present in an installed package
« Reply #8 on: November 28, 2024, 06:22:12 pm »
Quote from: MarkMLI
That's not a compile-time operation, so is NBG if he wants to refer to things which exist only in a later (or earlier) version or have different parameter lists etc.
Yes !

Quote from: marcov
If it is a proper constant you can also test them with $if

How ?
Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1447
    • Lebeau Software
Re: Howto use a DEFINE present in an installed package
« Reply #9 on: November 28, 2024, 06:40:11 pm »
Quote from: marcov
If it is a proper constant you can also test them with $if

How ?

Code: Pascal  [Select][+][-]
  1. {$IF (ZEOS_MAJOR_VERSION >= 8)}
  2. //code for 8.0 following
  3. {$ELSE}
  4. // Code for older version
  5. {$ENDIF}
« Last Edit: December 02, 2024, 04:26:56 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

tintinux

  • Sr. Member
  • ****
  • Posts: 351
    • Gestinux
Re: Howto use a DEFINE present in an installed package
« Reply #10 on: December 02, 2024, 09:21:39 am »
Hi

This should be very interesting, but with ZEOS 8.0 installed the test
Code: Pascal  [Select][+][-]
  1.  {$IF (ZEOS_MAJOR_VERSION >= 8)}

is wrong.
Was it a mere example ?
Or is there something else to do ?

Thanks
Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8083
Re: Howto use a DEFINE present in an installed package
« Reply #11 on: December 02, 2024, 09:42:15 am »
Depends on the mode.

Quote
The content of an expression is restricted to what can be evaluated at compile-time:

*    Constants (strings, numbers)
*    Macros
*    Compile time variables (mode MacPas only)
*    Pascal constant expressions (mode Delphi only) <=====

https://www.freepascal.org/docs.html -> Programmer's Guide detailed discussion of $IF. It's safest that I don't put in a URL for that since there's no guarantee that if will remain consistent over different versions of the compiler.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zoran

  • Hero Member
  • *****
  • Posts: 1885
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Howto use a DEFINE present in an installed package
« Reply #12 on: December 02, 2024, 09:50:09 am »
Hi

This should be very interesting, but with ZEOS 8.0 installed the test
Code: Pascal  [Select][+][-]
  1.  {$IF (ZEOS_MAJOR_VERSION >= 8)}

is wrong.
Was it a mere example ?
Or is there something else to do ?

Thanks

It is not wrong provided that this is a true constant (not a typed constant). And it is, as we can see: https://sourceforge.net/p/zeoslib/code-0/HEAD/tree/trunk/src/core/ZClasses.pas#l65

That is of course, provided the unit where it is declared is added in uses section. You added it, didn't you? Which error do you get?

tintinux

  • Sr. Member
  • ****
  • Posts: 351
    • Gestinux
Re: Howto use a DEFINE present in an installed package
« Reply #13 on: December 02, 2024, 02:51:11 pm »
Sorry, I was meaning False, not Wrong.

Even with referring ZClasses in the uses clause (where the const ZEOS_MAJOR_VERSION  is defined in the interface part) the condition is negative.

I don't understand...


Code: Pascal  [Select][+][-]
  1. interface
  2.  
  3. uses
  4.   Classes,
  5.   SysUtils,
  6.   LResources,
  7.   Forms,
  8.   Controls,
  9.   DB,
  10.   ZClasses;
  11.  
  12.  type
  13.   {$IF (ZEOS_MAJOR_VERSION >= 8)}
  14.      // greyed (not compiled)
  15.   {$ELSE}
  16.     // black (compiled)
  17.   {$ENDIF}
Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11980
  • FPC developer.
Re: Howto use a DEFINE present in an installed package
« Reply #14 on: December 02, 2024, 03:38:19 pm »
The syntax highlighter and the compiler can differ in opinion. Make sure you test it by running a compiled binary and printing or logging the result.

 

TinyPortal © 2005-2018