[fpc-pascal] Static linking to C library on Linux

Jonas Maebe jonas at freepascal.org
Wed Nov 27 21:37:49 CET 2019

On 27/11/2019 21:27, Ryan Joseph via fpc-pascal wrote:
>> On Nov 27, 2019, at 2:22 PM, Jonas Maebe <jonas at freepascal.org> wrote:
>> There is no tool that can show this, because a static library is just a
>> collection of random object files. It does not contain any metadata
>> about where these symbols are supposed to come from (and they could in
>> fact come from anywhere, be it individual object files, object files
>> collected in another static library, or a dynamic library).
> That's what I had originally thought. Can static libraries have dependancies on other static libraries?

Again, a static library has no dependencies because it is literally just
a collection of object files. They're packed like a zipfile into a
single file for convenience, but that's it. Those object files can
contain references to symbols that are not defined in that same
collection of object files, but I mentioned above already that those
symbols can come from anywhere. It's like saying that an object file has
a dependency on a library. It never has. It just references external
symbols, which in turn could be defined anywhere.

The only thing that can depend on a (by definition dynamic (*) )
library, is an executable or dynamic library (or their kin, like
frameworks and dylib bundles on macOS). This happens when the static
linker, while looking for those undefined symbols, at link time
discovers that they happen to be defined in a dynamic library. In that
case, it will store a reference to that dynamic library in the generated
executable/dynamic library to indicate to the dynamic linker where it
should look for external symbols.

> I noticed that when I linked a 12MB library my final binary was only 5MB so I presume there were duplicate symbols that were ignored.

Code and data that are defined in object files (in turnoptionally
contained in static libraries), but that are not referenced any other
code part of the linking proceess, are simply not linked/included into
your binary (unless you explicitly force linking everything from a
referenced static library/object file).


(*) On some platforms, like AIX, the situation is a bit more blurry, but
the main concept holds.

More information about the fpc-pascal mailing list