Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking Free Pascal programs to include dependencies

I have two Free Pascal units that I would like to use from a C program on linux.

Here is what I do:

$ fpc -fPIC base64.pas queueutils.pas
Warning: Only one source file supported
Free Pascal Compiler version 2.2.2 [2008/11/05] for x86_64
Copyright (c) 1993-2008 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling queueutils.pas
queueutils.pas(2088,11) Warning: Symbol "Socket" is deprecated
queueutils.pas(2097,10) Warning: Symbol "Connect" is deprecated
queueutils.pas(2104,3) Warning: Symbol "Sock2Text" is deprecated
2432 lines compiled, 0.5 sec
4 warning(s) issued

$ ppumove -o queueutils -e ppl *.ppu
PPU-Mover Version 2.1.1
Copyright (c) 1998-2007 by the Free Pascal Development Team

Processing base64.ppu... Done.
Processing queueutils.ppu... Done.
Linking queueutils.o base64.o
Done.

Seems fine so far, libqueueutils.so is created:

$ file libqueueutils.so
libqueueutils.so: ELF 64-bit LSB shared object, AMD x86-64, version 1 (SYSV), not stripped

$ ldd libqueueutils.so
ldd: warning: you do not have execution permission for `./libqueueutils.so'
        statically linked

However when the C program tries to use the library this way:

libqueue = dlopen("./libqueueutils.so", RTLD_LAZY);
if (!libqueue) {
  fprintf (stderr, "%s\n", dlerror());
}

it yields an error message:

$ ./tmbrkr
./libqueueutils.so: undefined symbol: VMT_PROCESS_TPROCESS

This VMT_PROCESS_TPROCESS-related error is resolved if I add process.o and process.ppu to the linking process done by ppumove. However after doing so another unit is missing and after that another... You get it.

Is there a way to somehow link all the necessary units together in one .so file so that the C program can dlopen() the library properly?

like image 869
Zizzencs Avatar asked Jul 31 '26 03:07

Zizzencs


1 Answers

Just like a normal binary (exe) is from a "program" source file , a .so/dll is created from a ''library'' sourcefile.

For the rest is the model is the same. You simply build the library mainprogram, and the compiler collects all units necessary and stuffs them in the .so.

With the exports keyword you can define what symbols to export.

library testdll;

uses x,y,z;

// define exportable symbols here

// some examples of symbol exports
exports
   P1 index 1,  // dll based on index
   P2 name 'Proc2', // normal export with alternate external symbol
   P3,               // just straight export.
   P4 resident    // for some MCU use
   ;

begin
  // startup code
end.

Also look up $soname $libsuffix and $libprefix in the manual.

Though I would recommend just using most recent 2.6.0, not some 5 year old 2.2.2

It might require recompiling FPC first with PIC though.

like image 57
Marco van de Voort Avatar answered Aug 01 '26 18:08

Marco van de Voort



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!