Articles Importing third-party Linux libraries on Delphi 10.2 Tokyo by Allen Drennan

emailx45

Местный
Регистрация
5 Май 2008
Сообщения
3,571
Реакции
2,439
Credits
574
Importing third-party Linux libraries on Delphi 10.2 Tokyo by Allen Drennan
April 6, 2017 Allen Drennan
[SHOWTOGROUPS=4,20]
This primer covers the basics of getting started using third-party libraries with the new Delphi 10.2 and Linux.

Introduction
If you are planning to use third-party Linux libraries on Delphi for Linux you may encounter some difficulties when you attempt to build and the linker attempts to find those libraries. Some basic Linux assistance with linked libraries could help you quickly resolve the issues.

Step 1 – Download and install library related POSIX tarball
Typically you download a .tar file containing the library related sources and extract it. Most libraries follow a similar pattern.
  1. Extract the tarball
  2. ./configure
  3. make
  4. sudo make install
  5. Finally you run ldconfig to update the library references.
Step 2 – Import your library routine
If you are using a third-party library you need to define your imports. In the following example we are importing the API called zmq_connect() from the libzmq.so library.

1
2
3
4
5
6
{$IFDEF LINUX}
ZMQ_LIB = 'libzmq.so';
_PU = '';
{$ENDIF}

function zmq_connect(s: Pointer; const addr: MarshaledAString): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_connect';

If all you need is an API contained in libc you can do something more direct. Here we are importing epoll_create() from libc with assistance from the Posix.Base unit.

1
2
3
4
uses
Posix.Base;

function epoll_create(size: Integer): Integer; cdecl; external libc name _PU + 'epoll_create';

Step 3 – Locate the library (.SO) for your undefined references
If you encounter a linker error message about undefined references like the following,

1
2
[DCC Error] E2597 C:\Program Files (x86)\Embarcadero\Studio\19.0\bin\ld-linux.exe: error: cannot find -lzmq
C:\Grijjy\Projects\Research\Delphi\HttpConsole\Linux64\Debug\zmq.o:zmq:function Zmq::zmq_ctx_new(): error: undefined reference to 'zmq_ctx_new'

You should take note of the portion of the error that says “cannot find -lzmq“. This is relatively cryptic, but you can ask the Linux OS about it. On Linux you type in the following:

1ld -lzmq --verbose

You will receive an output that looks something like the following,

1
2
3
4
5
attempt to open //lib64/libzmq.a failed
attempt to open //usr/lib64/libzmq.so failed
attempt to open //usr/lib64/libzmq.a failed
attempt to open //usr/local/lib/libzmq.so succeeded
-lzmq (//usr/local/lib/libzmq.so)

The important part is that it was able to locate the -lzmq reference and it is located at /usr/local/lib/libzmq.so.
If you don’t see an entry that matches the reference then the library is not properly installed on Linux or you may have forgotten to run ldconfig.
Step 4 – Add the library path to Delphi
Under your Delphi Options, SDK Manager add the Library Path $(SDKROOT)/usr/local/lib and then click, Update Local Cache. At this point your project should build and link correctly.

Conclusion
I hope this tip helps you get up and running quickly on Delphi Linux. In the coming weeks we will show some other various examples for Linux and Delphi.

[/SHOWTOGROUPS]