有人告诉我使用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);
}
当我编译该文件时,它会给出以下错误:
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
通知:i已安装了MinGW (迷你GNU ),gcc版本为4.7.2
有什么问题吗?
发布于 2013-08-31 10:53:26
未定义的对“`strlcpy”的引用
当链接器(如果您正在使用gcc)找不到它所抱怨的函数的定义时(collect2
声明或prototype,而是 definition ,其中定义了函数的代码),就会发生这种情况。
在您的情况下,可能会发生这种情况,因为没有可以链接到strlcpy
代码的共享对象或库。如果您确定有一个包含该代码的库,并且希望对其进行链接,请考虑使用传递给编译器的-L<path_to_library>
参数指定到库的路径。
发布于 2014-04-15 07:27:00
将此代码添加到代码中:
#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 */
那你就可以用它了。好好享受吧。
发布于 2013-08-31 10:46:09
strlcpy()
不是一个标准的C函数。
您可能希望使用strncpy()
或可能也使用memcpy()
。
https://stackoverflow.com/questions/18547251
复制相似问题