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


  • Newbie
  • Posts: 1
{$LINKLIB c} vs -k-lc
« on: March 24, 2019, 09:20:42 pm »
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
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
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: 630
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.