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?
Usage. strlcpy() takes the full size of the buffer, not only the length, and terminates the result with NUL as long as is greater than 0. Include a byte for the NUL in your value. size size. The strlcpy() function returns the total length of the string that would have been copied if there was unlimited space.
strlcpy returns the length of the string whether or not it was possible to copy it all -- this makes it easier to calculate the required buffer size. If dest and src are overlapping buffers, the behavior is undefined. One possible result is a buffer overrun - accessing out-of-bounds memory.
The functions you are using are usually a part of the C library provided by a target system.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With