Ame*_*een 12 c string linker gcc
有人告诉我使用这个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);
}
Run Code Online (Sandbox Code Playgroud)
当我编译文件时,它给我以下错误:
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
Run Code Online (Sandbox Code Playgroud)
注意:我安装了 MinGW(Windows的Minimalist GNU),gcc版本是4.7.2
问题是什么?
未定义对`strlcpy'的引用
这种情况发生在连接(collect2如果你正在使用gcc)找不到抱怨(函数的定义不是的声明或原型,但定义,函数的代码定义)。
在您的情况下,可能会发生这种情况,因为没有共享对象或带有strlcpy链接代码的库。如果您确定有一个包含代码的库,并且想要链接到该库,请考虑使用-L<path_to_library>传递给编译器的参数来指定库的路径。
将此代码添加到您的代码中:
#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 */
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用它。好好享受。
| 归档时间: |
|
| 查看次数: |
20493 次 |
| 最近记录: |