7

I'm trying to compile a source code which was originally written for FreeBSD and since strlcpy isn't included in the linux GlibC , I'm now stopped at this stage.

GCC has suggested that I can use strncpy but I don't want to actually change the source code.

Do I need to re-compile my LibC ?

Any help is appreciated.

7
  • Did you include proper headers? linux.die.net/man/3/strlcpy
    – KamilCuk
    Commented May 25, 2020 at 8:39
  • 2
    @KamilCuk The problem is here : in the BSD the "bsd/" part in the "#include <bsd/string.h>" is not necessary ( in fact forbidden) . But I haven't the BSD's version of the string.h . So I'm looking for a way to install it. Commented May 25, 2020 at 8:41
  • So where I can find the BSD version of string.h and put it somewhere and at the end add some flags to the "CFLAGS" of the makefile ? Commented May 25, 2020 at 8:44
  • 1
    Don't you have /usr/include/bsd/string.h? Then install them using you linux distribution package manager. On my archlinux it's owned by extra/libbsd package.
    – KamilCuk
    Commented May 25, 2020 at 8:48
  • 2
    @ParsaMousavi: You can use apt-file to find out which (uninstalled) package contains a file. It's very useful. apt-file search /usr/include/bsd/string.h reports that the file is in the package libbsd-dev, just as Ismael's answer says. Commented May 25, 2020 at 17:32

2 Answers 2

9

The best solution to port BSD applications is libbsd; and it's already packaged for most systems.

On Debian-based systems the development package is named libbsd-dev.

You can compile unmodified BSD source code by adding the following to your CFLAGS: -DLIBBSD_OVERLAY -I/usr/include/bsd, and linking with -lbsd.

However, instead of hardcoding those values, you should use pkg-config with the libbsd-overlay package if you intend to distribute your build system.

1
  • 2
    Here's what worked for me on Ubuntu to compile a program c.c: gcc $(pkg-config --cflags libbsd-overlay) c.c -o c $(pkg-config --libs libbsd-overlay) Commented Aug 11, 2021 at 17:55
3

You can download source code from https://github.com/freebsd/freebsd/blob/master/sys/libkern/strlcpy.c and add it with 2 modifications to your source code:

$ diff strlcpy.c strlcpy.c.orig 
20c20
< //__FBSDID("$FreeBSD$");
---
> __FBSDID("$FreeBSD$");
23c23
< //#include <sys/libkern.h>
---
> #include <sys/libkern.h>

I have compiled successfully this function with gcc 7.5.0 on Ubuntu 18.04.

2
  • I use ubuntu 18.04 server with gcc 7.5 too . Thanks. Commented May 25, 2020 at 10:39
  • I would strongly recommend against using any version of a string copying function written in C. This is one of the last refuges of things that ought to be hand written assembly. Commented May 2, 2023 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.