首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当我在c中使用strlcpy函数时,编译器或给我一个错误

当我在c中使用strlcpy函数时,编译器或给我一个错误
EN

Stack Overflow用户
提问于 2013-08-31 10:41:15
回答 3查看 35K关注 0票数 16

有人告诉我使用strlcpy函数而不是像这样的strcpy

代码语言:javascript
运行
复制
#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);

} 

当我编译该文件时,它会给出以下错误:

代码语言:javascript
运行
复制
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

有什么问题吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-31 10:53:26

未定义的对“`strlcpy”的引用

当链接器(如果您正在使用gcc)找不到它所抱怨的函数的定义时(collect2 声明或prototype,而是 definition ,其中定义了函数的代码),就会发生这种情况。

在您的情况下,可能会发生这种情况,因为没有可以链接到strlcpy代码的共享对象或库。如果您确定有一个包含该代码的库,并且希望对其进行链接,请考虑使用传递给编译器的-L<path_to_library>参数指定到库的路径。

票数 7
EN

Stack Overflow用户

发布于 2014-04-15 07:27:00

将此代码添加到代码中:

代码语言:javascript
运行
复制
#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 */

那你就可以用它了。好好享受吧。

票数 7
EN

Stack Overflow用户

发布于 2013-08-31 10:46:09

strlcpy()不是一个标准的C函数。

您可能希望使用strncpy()或可能也使用memcpy()

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18547251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档