Recent

Author Topic: Shared Library Tutorial  (Read 8862 times)

Aruna

  • Hero Member
  • *****
  • Posts: 807
Shared Library Tutorial
« on: November 19, 2024, 11:48:10 am »
Hi everyone, I put together a learn-by-doing type of tutorial and was not sure where this may fit in the wiki (if at all). Should I add it as a external link or create a completely new page on the wiki?

The tutorial is here:

Would appreciate some guidance. Thank you.

cdbc

  • Hero Member
  • *****
  • Posts: 2814
    • http://www.cdbc.dk
Re: Shared Library Tutorial
« Reply #1 on: November 19, 2024, 12:18:53 pm »
Hi
I was expecting something ...more. When I read the title 'Mastering...'  %)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Aruna

  • Hero Member
  • *****
  • Posts: 807
Re: Shared Library Tutorial
« Reply #2 on: November 19, 2024, 12:39:07 pm »
Hi
I was expecting something ...more. When I read the title 'Mastering...'  %)
Regards Benny
Hi Benny,

Thanks for your feedback! It sounds like the title set a high bar, and I’m sorry if the content didn’t fully meet your expectations. Could you please share a bit more about what you were hoping to see? That would help me improve and better align with readers' needs in the future.

Looking forward to hearing your thoughts!

Best regards, Aruna

gidesa

  • Sr. Member
  • ****
  • Posts: 252
Re: Shared Library Tutorial
« Reply #3 on: November 19, 2024, 01:04:16 pm »
Hello,
good idea.
I note that:
- the tutorial is limited to Lazarus on Linux;
- it's about directly writing a C library, and using that; no informations on using external C libraries, where you have only the headers, not the source.
- it is not about writing shared libraries just in Lazarus/Fpc.
So the title should be more informative. Expecially if you want to add to wiki.

Aruna

  • Hero Member
  • *****
  • Posts: 807
Re: Shared Library Tutorial
« Reply #4 on: November 19, 2024, 01:16:11 pm »
Hello,
good idea.
I note that:
- the tutorial is limited to Lazarus on Linux;
- it's about directly writing a C library, and using that; no informations on using external C libraries, where you have only the headers, not the source.
- it is not about writing shared libraries just in Lazarus/Fpc.
So the title should be more informative. Expecially if you want to add to wiki.
Hello @gidesa

Thank you for your thoughtful feedback! You’ve raised some excellent points. It’s clear the title and content could be refined for better clarity and coverage. I have removed the word 'Mastering' from the title.
Here’s what I’m thinking:
  •     Expanding the tutorial to include examples of using external C libraries with only headers available.
  •     Covering how to write shared libraries entirely in Lazarus/FPC.
  •     Updating the title to accurately reflect the scope, especially for inclusion in a wiki.

Your suggestions are invaluable for improving the tutorial—thank you for taking the time to share them! If you have any additional ideas or specific use cases you’d like to see covered, I’d love to hear them. What would 'you' like to see for a title?

Best regards, Aruna

ackarwow

  • Full Member
  • ***
  • Posts: 178
    • Andrzej Karwowski's Homepage
Re: Shared Library Tutorial
« Reply #5 on: November 19, 2024, 01:20:34 pm »
@Aruna
Maybe word "Introduction" could be better for the title... ?

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Shared Library Tutorial
« Reply #6 on: November 19, 2024, 01:28:32 pm »
note that there is also some information on libraries available in the programmer's manual (https://www.freepascal.org/docs-html/prog/progch12.html#progse55.html).

That  doesn't necessarily imply the effort from Aruna is in vain (if even for his/her own understanding).
Today is tomorrow's yesterday.

Aruna

  • Hero Member
  • *****
  • Posts: 807
Re: Shared Library Tutorial
« Reply #7 on: November 19, 2024, 01:31:35 pm »
@Aruna
Maybe word "Introduction" could be better for the title... ?
Thank you @ackarwow I have changed the title.

Aruna

  • Hero Member
  • *****
  • Posts: 807
Re: Shared Library Tutorial
« Reply #8 on: November 19, 2024, 01:34:24 pm »
note that there is also some information on libraries available in the programmer's manual (https://www.freepascal.org/docs-html/prog/progch12.html#progse55.html).
I had no idea.. thanks @TRon.

That  doesn't necessarily imply the effort from Aruna is in vain (if even for his/her own understanding).
Soo.. keep moving forward with the tutorial or drop it like a hot potato? It was initially for my own (his/male) understanding but later I thought why not share what I found?

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Shared Library Tutorial
« Reply #9 on: November 19, 2024, 01:56:54 pm »
Soo.. keep moving forward with the tutorial or drop it like a hot potato?
I find such a question always difficult to answer.

Reason is that as long as you learn(ed) something from it then it has meaning (if even for the writing of the tutorial itself, no matter the subject).

It is always difficult to add something to a topic that has been addressed extensively, f.e. see wiki.

There is so much details in those wiki pages that it is difficult to compete with that. And having said that imho it still lacks some (practical) approaches.

f.i. a small overview, like you did, might come in handy to be used as a quick reference (so that you do not need to go through all these articles in case someone only needs a small refresh on the topic)

Quote
It was initially for my own (his/male) understanding but later I thought why not share what I found?
Sorry for the pronouns: Not meant to offend.

I consider it good character that in case you've learned something that you would like to share what you've just learned. So, I can only applaud that.

Just give it a good thought yourself so that you would be able to decide yourself if there is anything in your own tutorial that adds to that what already exists. In case there is something seriously missing (either in the manual or wiki) then consider contributing.

As an example there a few people that have written a complete tutorial/manual on how Free Pascal works and is (can be) used. Is it redundant ? for sure it is. But they do target another audience so there is an actual use (also inspired by the many requests that are made over the years).
Today is tomorrow's yesterday.

gidesa

  • Sr. Member
  • ****
  • Posts: 252
Re: Shared Library Tutorial
« Reply #10 on: November 19, 2024, 02:30:56 pm »
Thank you for your thoughtful feedback! You’ve raised some excellent points. It’s clear the title and content could be refined for better clarity and coverage. I have removed the word 'Mastering' from the title.
Here’s what I’m thinking:
  •     Expanding the tutorial to include examples of using external C libraries with only headers available.
  •     Covering how to write shared libraries entirely in Lazarus/FPC.
  •     Updating the title to accurately reflect the scope, especially for inclusion in a wiki.

Your suggestions are invaluable for improving the tutorial—thank you for taking the time to share them! If you have any additional ideas or specific use cases you’d like to see covered, I’d love to hear them. What would 'you' like to see for a title?

Hello, glad that my opinion is useful.
In a tutorial I would:
- add a (very) short introduction about what are shared libraries and their advantages/disadvantages. Maybe two lines with a link to a wikipedia article, or similar?
- begin with an entirely Pascal solution: write the library in FPC, and then write a FPC program that uses it. Because why Pascal programmers as first thing would write C library if they can write Pascal library??
- add the case of your library written in C, and client program written in FPC; your tutorial just addresses this part
- add the broader case of external C library, describing how to extract Pascal function signatures from C headers, using the nice tool h2pas (and maybe using modern AI tools??)

As others noted, there are wiki informations on these subjects (indeed the manual section is not super-detailed. The wiki article IS super-detailed).
Perhaps you can "add value" merging wiki informations in a more complete tutorial (I do not mean "cut and paste"! :-))
Or maybe the previous subjects could be a "check list" for you to learn by yourself.
« Last Edit: November 19, 2024, 02:35:47 pm by gidesa »

cdbc

  • Hero Member
  • *****
  • Posts: 2814
    • http://www.cdbc.dk
Re: Shared Library Tutorial
« Reply #11 on: November 19, 2024, 04:17:35 pm »
Hi
Have you given the loading of dyn-libs any more thoughts?!?
I'm thinking how brilliantly 'Advanced Records', fit that bill...
E.g.:
Code: Pascal  [Select][+][-]
  1. unit libloader;
  2. {$mode ObjFPC}{$H+}
  3. {$modeswitch advancedrecords}
  4. interface
  5. uses Classes, SysUtils,dynlibs;
  6. type
  7.   {$i sharedata_types.inc}
  8.   { TSharedataLoader }
  9.  
  10.   TSharedataLoader = record
  11.     const sdlName: string = 'libsharedbuf.so';
  12.     var
  13.       hLib: TLibHandle;
  14.       sdlInit: Tshdt_Init;
  15.       sdlFini: Tshdt_Fini;
  16.     function LoadLib(const aPath: string): boolean;
  17.     function Loaded: boolean;
  18.     procedure UnloadLib;
  19.   end;
  20.  
  21. var
  22.   shdtLoader: TSharedataLoader;
  23.  
  24. implementation
  25.  
  26. { TSharedataLoader }
  27. function TSharedataLoader.LoadLib(const aPath: string): boolean;
  28. begin
  29.   if aPath = '' then exit(false);
  30.   hLib:= LoadLibrary(IncludeTrailingPathDelimiter(aPath)+sdlName);
  31.   Result:= (hLib <> NilHandle);
  32.   if Result then begin
  33.     pointer(sdlInit):= GetProcAddress(hLib,'shdt_Init');
  34.     pointer(sdlFini):= GetProcAddress(hLib,'shdt_Fini');
  35.     Result:= ((sdlInit <> nil) and (sdlFini <> nil));
  36.   end;
  37.   if not Result then UnloadLib;
  38. end;
  39.  
  40. function TSharedataLoader.Loaded: boolean;
  41. begin
  42.   Result:= ((hLib <> NilHandle) and (sdlInit <> nil) and (sdlFini <> nil));
  43. end;
  44.  
  45. procedure TSharedataLoader.UnloadLib;
  46. begin
  47.   if hLib <> NilHandle then begin
  48.     sdlInit:= nil;
  49.     sdlFini:= nil;
  50.     if UnloadLibrary(hLib) then hLib:= NilHandle;
  51.   end;
  52. end;
  53.  
  54. end.
  55.  
the use of include files to share api with the application, e.g.:
Code: Pascal  [Select][+][-]
  1. {$interfaces corba}
  2.   IShareData = interface['IShareData']
  3.     function get_Handle: ptruint;
  4.     function get_Name: shortstring;
  5.     procedure set_Name(aValue: shortstring);
  6.     { if data is available for your handle or 0-handle(anybody), returns datalength in bytes,
  7.       if no data are waiting for your handle or 0-handle, returns 0 (zero) }
  8.     function IsDataAvailable(aHandle: ptruint): ptrint;
  9.     { reads data into an allocated buffer of size designated by a call to 'IsDataAvailable',
  10.       returns the number of bytes read, or 0 if 'aData' = nil or 'aLen' < available count }
  11.     function ReadData(aHandle: ptruint;aData: pointer;aLen: ptrint): ptrint;
  12.     { writes data to the buffer, marked with the destination-handle or 0,
  13.       returns the number of bytes written or 0 on failure }
  14.     function WriteData(aHandle,aDest: ptruint;aData: pointer;aLen: ptrint): ptrint;
  15.     property Handle: ptruint read get_Handle;
  16.     property Name: shortstring read get_Name write set_Name;
  17.   end;
  18. {$interfaces com}
  19. ///// function prototypes /////
  20.   Tshdt_Init = function(aHandle: ptruint;aName: shortstring): IShareData;
  21.   Tshdt_Fini = procedure(aHandle: ptruint);
  22. (*
  23. Library exports 2 funcs / procs:
  24.  
  25. - function shdt_Init(aHandle: ptruint;aName: shortstring): IShareData;
  26. - procedure shdt_Fini(aHandle: ptruint);
  27.  
  28. BOTH ARE MANDATORY!!!
  29. In return you get an interface to the shared data buffer within.
  30. *)
...So you see, my expectations were somewhat higher, but I too like the idea of sharing what you've learnt with others  8)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Roland57

  • Hero Member
  • *****
  • Posts: 609
    • msegui.net
Re: Shared Library Tutorial
« Reply #12 on: November 19, 2024, 04:52:24 pm »
@Aruna

Thanks for the tutorial. Please continue it, if you have time (and desire).  ;)

All successfully tested here (Linux Mageia).
« Last Edit: November 19, 2024, 05:11:28 pm by Roland57 »
My projects are on Codeberg.

indydev

  • Full Member
  • ***
  • Posts: 131
Re: Shared Library Tutorial
« Reply #13 on: November 19, 2024, 05:35:55 pm »
Here are some good example articles on the subject that, unfortunately, are VERY Borland/Embarcardero specific.  Much of the code and explanation rely on Windows, the specific C++ compiler, and COM only Interfaces.  When these were "originally" written, cross compiling may not even have been possible, so the specificity to Windows is understandable. There are asides for generalizing, but something like this for Free Pascal/Lazarus would be nice.

http://rvelthuis.de/articles/articles-cobjs.html
http://rvelthuis.de/articles/articles-cppobjs.html
http://rvelthuis.de/programs/convertpack.html
http://rvelthuis.de/articles/articles-convert.html#macros

Tony Stone

  • Sr. Member
  • ****
  • Posts: 280
Re: Shared Library Tutorial
« Reply #14 on: November 19, 2024, 09:59:15 pm »
Soo.. keep moving forward with the tutorial or drop it like a hot potato? It was initially for my own (his/male) understanding but later I thought why not share what I found?

I would say make all the tutorials/articles you want whether you are an expert or not is not a valid factor if you are providing useful information!  Just remember there is always other people out there on the same skill level as yourself so to have anyone willing to try and put out educational info for others is huge IMO.  Like others said, if it was useful for you it will probably be useful to others! 

Putting together quality tutorials is time consuming and sort of a skill in itself so if you like making tutorials DO IT!  They will only get better with time.

 

TinyPortal © 2005-2018