# Making strlcpy available in linux

Ask Question
Asked 5 years, 1 month ago
Modified 5 years, 1 month ago
Viewed 13k times
8

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.

- linux
- gcc
- freebsd
- glibc

edited May 25, 2020 at 8:52
Parsa Mousavi
asked May 25, 2020 at 8:35
Parsa Mousavi Parsa Mousavi
1,212 3 3 gold badges 17 17 silver badges 35 35 bronze badges
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.
  – Parsa Mousavi
  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 ?
  – Parsa Mousavi
  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.
  – Nate Eldredge
  Commented May 25, 2020 at 17:32

| Show 2 more comments

## 2 Answers 2

Sorted by: Reset to default
10

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.

answered May 25, 2020 at 17:27
Ismael Luceno Ismael Luceno
2,138 15 15 silver badges 27 27 bronze badges
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)`
  – Keith Thompson
  Commented Aug 11, 2021 at 17:55

Add a comment |
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.

answered May 25, 2020 at 10:10
pifor pifor
7,942 2 2 gold badges 11 11 silver badges 18 18 bronze badges
2

- I use ubuntu 18.04 server with gcc 7.5 too . Thanks.
  – Parsa Mousavi
  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.
  – Roflcopter4
  Commented May 2, 2023 at 7:21

Add a comment |

## Your Answer

Thanks for contributing an answer to Stack Overflow!

- Please be sure to answer the question. Provide details and share your research!

But avoid …

- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

### Sign up or log in

Sign up using Google
Sign up using Email and Password

### Post as a guest

Name
Email

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

- linux
- gcc
- freebsd
- glibc

See similar questions with these tags.