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.
- Extract the tarball
./configure
make
sudo make install
- 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.
{$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.
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,
[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:
ld -lzmq --verbose
You will receive an output that looks something like the following,
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.
How does Delphi/Linux relate to the Windows Subsystem for Linux (Bash Shell) ?
LikeLike
We do all of our work on VMWare Workstation using Ubuntu Linux 16.04.2 virtual machines (currently) because our primary interest is building server related processes that will be deployed on this exact setup/scenario. That said, I don’t see any reason why the results wouldn’t be the same but we have not tested on the Windows Subsystem for Linux, yet.
LikeLike