16

Someone told me to use the strlcpy function instead of strcpy like this

#include <stdio.h>
#include <string.h>

void main()
{
   char var1[6] = "stuff";
   char var2[7] = "world!";
   strlcpy(var1, var2, sizeof(var2));
   printf("hello %s", var1);

} 

and when I compile the file it gives me the following error:

C:\Users\PC-1\AppData\Local\Temp\ccafgEAb.o:c.c:(.text+0x45): undefined referenc
e to `strlcpy'
collect2.exe: error: ld returned 1 exit status

notice: I have installed MinGW (Minimalist GNU for Windows) and gcc version is 4.7.2

What is the problem?

alk
  • 69,737
  • 10
  • 105
  • 255
Ameen
  • 1,747
  • 3
  • 16
  • 31
  • @MitchWheat A valid point, but that's not why `ld` is complaining :-)) – cnicutar Aug 31 '13 at 10:43
  • 1
    I didn't say it was. :) – Mitch Wheat Aug 31 '13 at 10:43
  • 1
    var1 only have 6 char space and you are trying to copy 6 char word to it. Aint' you missing our good old '\0' or aint' you going to write past buffer of var1? – Abhineet Aug 31 '13 at 10:46
  • I get confused because it works perfectly when I compile it on gcc of(cygwin package) – Ameen Aug 31 '13 at 10:57
  • @ameen What is confusing? I already explained to you what's happening. The linker can not find the definition of the function you are using. Perhaps the version of the library provided by MinGW doesn't provide an implementation of `strlcpy` You can use another compiler with another library, link against another library manually, or even write an implementation for `strlcpy` yourself. – NlightNFotis Aug 31 '13 at 11:12
  • 1
    @NlightNFotis I'm sorry but I dont know that each compiler has its own library – Ameen Aug 31 '13 at 11:20
  • 2
    @ameen You needn't worry, you are not supposed to know everything and it's fine. The functions you are using are usually a part of the C library provided by a target system. Unix like systems usually come with `glibc` or a variant. You are using windows, so the compilers you are using (an adapted version of `gcc`) comes packed with what it needs from the `c library` in case it's not provided by the target system, so that a minimal runtime is provided to the programs you write. – NlightNFotis Aug 31 '13 at 11:24
  • 3
    looking at myself 4 and half years ago .. where I was .. and how much I learned .. gives me a nice feeling! – Ameen Mar 10 '18 at 09:58

4 Answers4

7

undefined reference to `strlcpy'

This happens when the linker (collect2 if you are using gcc) can not find the definition of the function it complains about (not the declaration or prototype, but the definition, where the function's code is defined).

In your case it may happen because there is no shared object or library with strlcpy's code to link against. If you are sure there is a library with the code and you want to link against it, consider specifying the path to the library with the -L<path_to_library> parameter passed to the compiler.

NlightNFotis
  • 9,559
  • 5
  • 43
  • 66
6

Add this code to your code:

#ifndef HAVE_STRLCAT
/*
 * '_cups_strlcat()' - Safely concatenate two strings.
 */

size_t                  /* O - Length of string */
strlcat(char       *dst,        /* O - Destination string */
              const char *src,      /* I - Source string */
          size_t     size)      /* I - Size of destination string buffer */
{
  size_t    srclen;         /* Length of source string */
  size_t    dstlen;         /* Length of destination string */


 /*
  * Figure out how much room is left...
  */

  dstlen = strlen(dst);
  size   -= dstlen + 1;

  if (!size)
    return (dstlen);        /* No room, return immediately... */

 /*
  * Figure out how much room is needed...
  */

  srclen = strlen(src);

 /*
  * Copy the appropriate amount...
  */

  if (srclen > size)
    srclen = size;

  memcpy(dst + dstlen, src, srclen);
  dst[dstlen + srclen] = '\0';

  return (dstlen + srclen);
}
#endif /* !HAVE_STRLCAT */

#ifndef HAVE_STRLCPY
/*
 * '_cups_strlcpy()' - Safely copy two strings.
 */

size_t                  /* O - Length of string */
strlcpy(char       *dst,        /* O - Destination string */
              const char *src,      /* I - Source string */
          size_t      size)     /* I - Size of destination string buffer */
{
  size_t    srclen;         /* Length of source string */


 /*
  * Figure out how much room is needed...
  */

  size --;

  srclen = strlen(src);

 /*
  * Copy the appropriate amount...
  */

  if (srclen > size)
    srclen = size;

  memcpy(dst, src, srclen);
  dst[srclen] = '\0';

  return (srclen);
}
#endif /* !HAVE_STRLCPY */

then, you can use it. enjoy it.

kangear
  • 2,493
  • 2
  • 31
  • 44
4

strlcpy() ist not a standard C function.

You might like to use strncpy() or propably also memcpy() instead.

alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    thank you for your answer but when I compile the same file in (cygwin, gcc 4.7.3) it works – Ameen Aug 31 '13 at 10:53
  • @ameen: The libraries in use by those compilers might provide the function as an extension to the C standard. – alk Aug 31 '13 at 10:55
  • 1
    Keep in mind strlcpy fixes your 0 termination charackter, strncpy won't do that. – maxbit89 Mar 03 '21 at 16:08
1

I too got this error when trying to compile code and found that with Ubuntu 1604 the error will go away if I link with -lbsd.

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424