Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when I use strlcpy function in c the compilor give me an error

Tags:

c

string

gcc

linker

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?

like image 765
Ameen Avatar asked Aug 31 '13 10:08

Ameen


People also ask

What is strlcpy in C?

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.

What does strlcpy return?

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.

Which library is strlcpy in?

The functions you are using are usually a part of the C library provided by a target system.


1 Answers

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.

like image 92
NlightNFotis Avatar answered Sep 30 '22 05:09

NlightNFotis