1

I am planning to write a library in FPC that can be linked to from other compilers. Dynamic linking (.so, .dll) is no issue, however, the requirement of static linking from (at least) gcc and/or clang has come up.

Somehow, in the end, I need an object archive containing all FPC internal functions, correct? Linking to libc (cmem, cthreads etc.) instead of FPCs implementation appears to be a requirement, but what else would be the most effective and cross-platform way to achieve this?

FPC used is 2.7(trunk).

Thanks in advance.

EDIT: some progress has been made here (german, also see the linked repo in my comment there), but it strikes me as not really elegant nor simple. Also there remains the issue Marco brought up about initialization/finalization...

Martok
  • 106
  • 1
  • 8

2 Answers2

1

I had a similar issue and found this tutorial really helpful. It was basically written for iOS development, which would sound much trickier, but worked fairly well for regular C/C++ linkages.

In my case, a legacy Delphi project was used for creating a static library:

fpc -Cn -Mdelphi xxx.dpr
ar -q libxxx.a `grep "\.o$" link.res`
ranlib libxxx.a
tomyun
  • 388
  • 7
  • 6
  • This produce .o files only for project modules, but does not include other system .o files as well as .o files from used in project libraries. So, produced .a file is much smaller than .dll and not portable. – George Jan 13 '22 at 14:35
0

Well, in theory you link every .o together with AR, and then call FPC_INITIALIZEUNITS on startup and FPC_FINALIZEUNITS on shutdown.

Maybe however, FPC generates some information (like a table with the addresses of all units ini/fin routines) in the mainmodule. I can't quickly think of a solution for that.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
  • Good point about FPC_INITIALIZEUNITS. I've updated the original question with some progress BenBE and I have made in the meantime, but I think we completely overlooked that. And you're right about code only being generated for a full build, we kinda hacked around that by building a library and then using the main object to extract some sections from it. But that can't be the best solution. – Martok Apr 06 '14 at 22:22