0

I am using strlcpy and strlcat in place of strncat/cpy; however, whenever I go to compile it GCC -o Project Project.c it will continuously through me errors saying:

Undefined reference to strlcpy; Undefined reference to 'strlcat' My code:

#include <bsd/string.h>
strlcpy(path, ARTICLEPATH, sizeof(ARTICLEPATH));
strlcat(path, ARTICLEPATH, sizeof(path));

I have added the library to my file, but it seems to continue to throw me the error: #include <bsd/string.h>

Is there something else I need to do? Or is there an another alternative to using strncpy that utilizes null byte termination?

EDIT: As background, I am on ubuntu 20.04

James Ukilin
  • 851
  • 2
  • 12
  • 17
  • Does this answer your question? [when I use strlcpy function in c the compilor give me an error](https://stackoverflow.com/questions/18547251/when-i-use-strlcpy-function-in-c-the-compilor-give-me-an-error) – Retired Ninja May 02 '21 at 18:48
  • 4
    I suspect the `-lbsd` answer on that question is the one you want here. – Nate Eldredge May 02 '21 at 18:51
  • @NateEldredge do you mean adding -lbsd to when I compile the program? Ie: gcc -o -lbsd project project.c ? – James Ukilin May 02 '21 at 20:15
  • 1
    @JamesUkilin: Yes, but put `-lbsd` at the end. `gcc -o project project.c -lbsd` https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc It certainly can't go after `-o` or you will write your binary to a file named `./-lbsd`. – Nate Eldredge May 02 '21 at 20:15

1 Answers1

2

As is (sort of) explained in the libbsd man page, you need to link the libbsd library as well as including the header. So add -lbsd to your command line when linking. For a simple program, you might do

gcc -o prog prog.c -lbsd

Note that the ordering of options is important here, see Why does the order in which libraries are linked sometimes cause errors in GCC? If you put -lbsd before your source file on the command line, it will probably not work.

The way you asked the question suggests you might have some confusion about the difference between a header and a library, and the roles that each one plays. You may want to read What's the difference between a header file and a library?

(This is almost a duplicate of when I use strlcpy function in c the compilor give me an error, but that question is more generic and some of the answers aren't applicable to Ubuntu specifically, so I thought a separate answer would be useful.)

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82