Recent

Author Topic: imports ease  (Read 1556 times)

mercurhyo

  • Sr. Member
  • ****
  • Posts: 253
imports ease
« on: December 18, 2023, 07:47:50 pm »
I think it would be useful to add a directive so FPC compiler understands/crossplatforminterprets and reminds what libraries symbol to refer without the need to repeat it in source code

Code: Pascal  [Select][+][-]
  1. unit myimports;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. const
  8.   MyLibrary = 'mylibname' +
  9. {$ifdef WINDOWS}
  10. '.dll'
  11. {$else}
  12. {$ifdef DARWIN}
  13. '.dylib'
  14.  {$else}
  15.  '.so'
  16. {$endif}
  17. {$endif};
  18.  
  19. procedure proc1; external MyLibrary;
  20. procedure proc2; external MyLibrary;
  21. procedure proc3; external MyLibrary;
  22. ...
  23. procedure procN; external MyLibrary;
  24.  
  25. implementation
  26.  
  27. end.

should be replaced by

Code: Pascal  [Select][+][-]
  1. unit myimports;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. {$DYNLIB mylibname}
  8.  
  9. procedure proc1; external;
  10. procedure proc2; external;
  11. procedure proc3; external;
  12. ...
  13. procedure procN; external;
  14.  
  15. implementation
  16.  
  17. end.

with a new compiler directive like {$dynlib name} (unit scope or block of externals scope automatycally adjusting platforms naming conventions for all the following 'external' with no name mentioned
« Last Edit: December 18, 2023, 07:55:40 pm by mercurhyo »
DEO MERCHVRIO - Linux, Win10pro - Ryzen9XT 24threads + Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10542
  • Debugger - SynEdit - and more
    • wiki
Re: imports ease
« Reply #1 on: December 18, 2023, 07:57:37 pm »
with a new compiler directive like {$dynlib name} (unit scope or block of externals scope} automatycally adjusting platforms naming conventions for all the following 'external' with no name mentioned

Well, to adjust platform-naming you don't need to get rid of the constant, nor the repeated "external the_lib_name".

And I think, you can use  System.SharedSuffix
Code: Pascal  [Select][+][-]
  1. const
  2.   MyLibrary = 'mylibname.' + System.SharedSuffix;


As for the repeated "MyLibrary" after the (repeated) "external" => there a lots of ways to quickly put that in.

- (regex) search/replace on "external".
- Syncro edit
- Editor Macro
- external script
- fpc macro (define a single identifier that will be replaced with the full sequence)
...

PascalDragon

  • Hero Member
  • *****
  • Posts: 5750
  • Compiler Developer
Re: imports ease
« Reply #2 on: December 19, 2023, 11:01:56 pm »
I think it would be useful to add a directive so FPC compiler understands/crossplatforminterprets and reminds what libraries symbol to refer without the need to repeat it in source code

Code: Pascal  [Select][+][-]
  1. unit myimports;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. const
  8.   MyLibrary = 'mylibname' +
  9. {$ifdef WINDOWS}
  10. '.dll'
  11. {$else}
  12. {$ifdef DARWIN}
  13. '.dylib'
  14.  {$else}
  15.  '.so'
  16. {$endif}
  17. {$endif};
  18.  
  19. procedure proc1; external MyLibrary;
  20. procedure proc2; external MyLibrary;
  21. procedure proc3; external MyLibrary;
  22. ...
  23. procedure procN; external MyLibrary;
  24.  
  25. implementation
  26.  
  27. end.

You are aware that the file extension isn't required, because FPC will add the correct suffix (and prefix) for the platform?

should be replaced by

Code: Pascal  [Select][+][-]
  1. unit myimports;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. {$DYNLIB mylibname}
  8.  
  9. procedure proc1; external;
  10. procedure proc2; external;
  11. procedure proc3; external;
  12. ...
  13. procedure procN; external;
  14.  
  15. implementation
  16.  
  17. end.

with a new compiler directive like {$dynlib name} (unit scope or block of externals scope automatycally adjusting platforms naming conventions for all the following 'external' with no name mentioned

This will fail as soon as one wants to import from multiple libraries in the same unit (think OpenSSL which consists of two modules).

 

TinyPortal © 2005-2018