Recent

Author Topic: {$LINKLIB c} vs -k-lc  (Read 936 times)

GromBeestje

  • Newbie
  • Posts: 1
{$LINKLIB c} vs -k-lc
« on: March 24, 2019, 09:20:42 pm »
Hello,
I am investigating combining Pascal and C code in a single binary. To do this, I have to link in the C library.
According to https://www.freepascal.org/docs-html/current/prog/progsu46.html
I have to use {$LINKLIB c} in my Pascal source file, this works as expected, the binary links correcly
and the resulting program runs as expected, however the documentation mentions
Quote
The same can be obtained by removing the linklib directive in the above unit, and specify -k-lc on the command line:
ppc386 -k-lc foo.pp
This does not give me the same results. The program appears to link correctly, but the resulting binary refuses to run.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 674
Re: {$LINKLIB c} vs -k-lc
« Reply #1 on: March 24, 2019, 09:29:55 pm »
{$linklib c} is indeed the only correct way, and this is an error in the manual.

-k-lc just passes the "-lc" command line parameter to the linker, without the compiler having any clue about what it does. This means that the C library will be linked, but nothing else gets changed.

{$linklib c} additionally also makes the compiler replace the initialisation code of the library (or program) with a version that initialises the C library (since by default FPC programs and libraries do not use the C library on most platforms, it cannot contain any code to initialise it either). Without this initialisation, the C library indeed won't work correctly.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7641
Re: {$LINKLIB c} vs -k-lc
« Reply #2 on: July 02, 2019, 04:16:26 pm »
(since by default FPC programs and libraries do not use the C library on most platforms, it cannot contain any code to initialise it either).

(Let the startup provide weak defaults that are overriden by the shared lib?)