Bookstore

Recent

Author Topic: (SOLVED) Dynamic library question  (Read 485 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2068
    • Lazarus and Free Pascal italian community
(SOLVED) Dynamic library question
« on: February 14, 2020, 10:36:17 am »
Hi guys, I must admit with my utmost regret that I have never used dynamic libraries (.dll, .dylib, .so). Wanting to do a first test I wrote what is attached.
I managed to generate (in my case the dylib) but I can't understand how to get it into the program. Can anyone help me?
The first project is the one that generates the dll, the project contained in the client folder should instead be the program that uses it.
I know it's trivial, but I don't want to waste time looking for documentation, surely some of you can give me two tips to understand.
Thank you
« Last Edit: February 19, 2020, 08:58:40 am by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

nummer8

  • Jr. Member
  • **
  • Posts: 53
Re: Dynamic library question
« Reply #1 on: February 14, 2020, 10:42:24 am »

trev

  • Hero Member
  • *****
  • Posts: 516
  • Former Delphi 1-7 and 10.2 User
Re: Dynamic library question
« Reply #2 on: February 14, 2020, 12:26:33 pm »
Example library.dylib and an application to use it:

https://wiki.freepascal.org/macOS_Dynamic_Libraries

(FPC, Lazarus not required)
o Lazarus v2.1.0 r62775, FPC v3.3.1 r44289, macOS 10.14.6 (with sup update), Xcode 10.3.1
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

xinyiman

  • Hero Member
  • *****
  • Posts: 2068
    • Lazarus and Free Pascal italian community
Re: Dynamic library question
« Reply #3 on: February 18, 2020, 09:39:45 am »
I tried using the example, but inside a non console application it doesn't work. Returns me

GetLastOSError2 = No such file or directory

I attach the example only to be compiled and tested.
At this point I would say that I'm wrong the place to put the dylib file. But I tried all the combinations a bit. Anyone help me? thank you
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

trev

  • Hero Member
  • *****
  • Posts: 516
  • Former Delphi 1-7 and 10.2 User
Re: Dynamic library question
« Reply #4 on: February 18, 2020, 02:12:54 pm »
My example was for plain FPC with both the program and library compiled in the same directory. To fix it for your Lazarus project you need to:

1) Copy the libTestLibrary.dylib to the app bundle Contents/MacOS directory
2) Change into the app bundle Contents/MacOS directory
3) Enter: install_name_tool -add_rpath @executable_path/. project1

It should then find the dynamic library. You can read about install_name_tool in its man page (type man install_name_tool in a terminal).

I'll update my Wiki example to make this clear(er).
o Lazarus v2.1.0 r62775, FPC v3.3.1 r44289, macOS 10.14.6 (with sup update), Xcode 10.3.1
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

xinyiman

  • Hero Member
  • *****
  • Posts: 2068
    • Lazarus and Free Pascal italian community
Re: Dynamic library question
« Reply #5 on: February 19, 2020, 08:56:21 am »
My example was for plain FPC with both the program and library compiled in the same directory. To fix it for your Lazarus project you need to:

1) Copy the libTestLibrary.dylib to the app bundle Contents/MacOS directory
2) Change into the app bundle Contents/MacOS directory
3) Enter: install_name_tool -add_rpath @executable_path/. project1

It should then find the dynamic library. You can read about install_name_tool in its man page (type man install_name_tool in a terminal).

I'll update my Wiki example to make this clear(er).

I tried your advice, but nothing has changed.

To make it work, I also had to copy project1 into the Contents / MacOS directory and overwrite the link
« Last Edit: February 19, 2020, 08:58:30 am by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

trev

  • Hero Member
  • *****
  • Posts: 516
  • Former Delphi 1-7 and 10.2 User
Re: Dynamic library question
« Reply #6 on: February 19, 2020, 11:16:18 am »
Quote
To make it work, I also had to copy project1 into the Contents / MacOS directory and overwrite the link

Sorry, I assumed too much. The extra Wiki info I added included that step.

Glad you got there in the end :)
o Lazarus v2.1.0 r62775, FPC v3.3.1 r44289, macOS 10.14.6 (with sup update), Xcode 10.3.1
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

xinyiman

  • Hero Member
  • *****
  • Posts: 2068
    • Lazarus and Free Pascal italian community
Re: (SOLVED) Dynamic library question
« Reply #7 on: February 19, 2020, 11:45:21 am »
Thanks to you for the help. You have been very kind
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1