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

Michael Van Canneyt michael at freepascal.org
Wed Nov 27 17:34:33 CET 2019



On Wed, 27 Nov 2019, Ryan Joseph via fpc-pascal wrote:

> I'm trying to link to a static library (built from Python sources) in Pascal and having troubles on Linux. On Mac where I have experience I successfully link and every works as expected but on Linux I get slews of linker errors in what appear to be standard C library functions.
>
> For example:
>
> /usr/bin/ld.bfd: /home/chris/pas/PythonBridge-master/sources/libpython3.7m.a(bytearrayobject.o): in function `memcpy':
> /usr/include/x86_64-linux-gnu/bits/string_fortified.h:34: undefined reference to `memcpy'
> /usr/bin/ld.bfd: /home/chris/pas/PythonBridge-master/sources/libpython3.7m.a(typeobject.o): in function `_PyType_Name':
> /home/chris/Downloads/Python-3.7.4/Objects/typeobject.c:417: undefined reference to `strrchr'
> /usr/bin/ld.bfd: /home/chris/Downloads/Python-3.7.4/Objects/typeobject.c:107: undefined reference to `strlen'
> /usr/bin/ld.bfd: /home/chris/Downloads/Python-3.7.4/Objects/typeobject.c:108: undefined reference to `strncmp'
> /usr/bin/ld.bfd: /home/chris/Downloads/Python-3.7.4/Objects/typeobject.c:103: undefined reference to `strrchr'
> /usr/bin/ld.bfd: /home/chris/Downloads/Python-3.7.4/Objects/typeobject.c:107: undefined reference to `strlen'
> /usr/bin/ld.bfd: /home/chris/Downloads/Python-3.7.4/Objects/typeobject.c:108: undefined reference to `strncmp'
>
> Do I need to link to other system libraries when building on Linux?  I'm
> also confused because I thought the purpose of the static libraries was to
> contain all the code they needed to run and thus these system functions
> like "memcpy" would be present also.  I used to same make command I did on
> Mac to build the library but maybe I did something wrong on Linux.

The C library will not be in your custom-built library, but will be
referenced if it uses functions from libc.

You need to link to libc. So add a 
{$linklib c}
and you should be set.

Michael.


More information about the fpc-pascal mailing list