C语言 当我在 c 中使用 strlcpy 函数时,编译器给我一个错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18547251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
when I use strlcpy function in c the compilor give me an error
提问by Ameen
Someone told me to use the strlcpyfunction instead of strcpylike this
有人告诉我使用该strlcpy功能而不是strcpy这样
#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 gccversion is 4.7.2
注意:我已经安装了 MinGW(Minimalist GNU for Windows) 并且gcc版本是4.7.2
What is the problem?
问题是什么?
采纳答案by NlightNFotis
undefined reference to `strlcpy'
对 `strlcpy' 的未定义引用
This happens when the linker (collect2if you are using gcc) can not find the definition of the function it complains about (notthe declaration or prototype, but the definition, where the function's code is defined).
这种情况发生在连接(collect2如果你正在使用gcc)找不到抱怨(函数的定义不是的声明或原型,但定义,函数的代码定义)。
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.
在您的情况下,可能会发生这种情况,因为没有strlcpy可链接的代码的共享对象或库。如果您确定有一个包含代码的库并且想要链接它,请考虑使用-L<path_to_library>传递给编译器的参数指定库的路径。
回答by kangear
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] = '##代码##';
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] = '##代码##';
return (srclen);
}
#endif /* !HAVE_STRLCPY */
then, you can use it. enjoy it.
然后,您可以使用它。好好享受。
回答by alk
strlcpy()ist not a standard C function.
strlcpy()不是标准的 C 函数。
You might like to use strncpy()or propably also memcpy()instead.
您可能更喜欢使用strncpy()or propably memcpy()。
回答by Niklas
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.
我在尝试编译代码时也遇到了这个错误,发现在 Ubuntu 1604 中,如果我链接到-lbsd.

