如何安装并包括我的C编译器的库?

发布于 2025-02-01 08:31:45 字数 1598 浏览 4 评论 0 原文

我正在使用Debian 11,

我正在尝试重现Strlcpy。

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 unsigned int ft_strlcpy(char *dest, char *src, unsigned int size)
  6 {
  7   unsigned int i;
  8 
  9   i = 0;
 10   while (src[i] && i < size)
 11   {
 12     dest[i] = src[i];
 13     i++;
 14   }
 15   dest[i] = '\0';
 16   while (src[i])
 17     i++;
 18   return (i);
 19 }
 20 
 21 int main()
 22 {
 23   unsigned int i;
 24   char *dest1 = malloc(sizeof(char) * 50);
 25   char *dest2 = malloc(sizeof(char) * 50);
 26 
 27   i = 0;
 28   while (i < 26)
 29   {
 30     printf("%d ", ft_strlcpy(dest1, "hello my name is marcel", i));
 31     printf("%s\n", dest1);
 32     printf("%ld ", strlcpy(dest2, "hello my name is marcel", i));
 33     printf("%s\n", dest2);
 34     i++;
 35   }
 36   free(dest1);
 37   free(dest2);
 38   return (0);
 39 }

但是,当我编译代码时,我会收到此消息:

ft_strlcpy.c: In function ‘main’:
ft_strlcpy.c:32:18: warning: implicit declaration of function ‘strlcpy’; did you mean ‘strncpy’? [-Wimplicit-function-declaration]
   32 |   printf("%ld ", strlcpy(dest2, "hello my name is marcel", i));
      |                  ^~~~~~~
      |                  strncpy
/usr/bin/ld: /tmp/ccukR8g6.o: in function `main':
ft_strlcpy.c:(.text+0xf0): undefined reference to `strlcpy'
collect2: error: ld returned 1 exit status
make: *** [<builtin>: ft_strlcpy] Error 1

我不知道如何包括libbsd或使用pkgconf。 我已经尝试了几个小时,但是找不到解决方案。

如果有人可以将我重定向到手册或解释概念,那就太好了。

感谢您的帮助!

I am using Debian 11

I am trying to reproduce strlcpy.

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 unsigned int ft_strlcpy(char *dest, char *src, unsigned int size)
  6 {
  7   unsigned int i;
  8 
  9   i = 0;
 10   while (src[i] && i < size)
 11   {
 12     dest[i] = src[i];
 13     i++;
 14   }
 15   dest[i] = '\0';
 16   while (src[i])
 17     i++;
 18   return (i);
 19 }
 20 
 21 int main()
 22 {
 23   unsigned int i;
 24   char *dest1 = malloc(sizeof(char) * 50);
 25   char *dest2 = malloc(sizeof(char) * 50);
 26 
 27   i = 0;
 28   while (i < 26)
 29   {
 30     printf("%d ", ft_strlcpy(dest1, "hello my name is marcel", i));
 31     printf("%s\n", dest1);
 32     printf("%ld ", strlcpy(dest2, "hello my name is marcel", i));
 33     printf("%s\n", dest2);
 34     i++;
 35   }
 36   free(dest1);
 37   free(dest2);
 38   return (0);
 39 }

However, I get this message when I compile my code:

ft_strlcpy.c: In function ‘main’:
ft_strlcpy.c:32:18: warning: implicit declaration of function ‘strlcpy’; did you mean ‘strncpy’? [-Wimplicit-function-declaration]
   32 |   printf("%ld ", strlcpy(dest2, "hello my name is marcel", i));
      |                  ^~~~~~~
      |                  strncpy
/usr/bin/ld: /tmp/ccukR8g6.o: in function `main':
ft_strlcpy.c:(.text+0xf0): undefined reference to `strlcpy'
collect2: error: ld returned 1 exit status
make: *** [<builtin>: ft_strlcpy] Error 1

I have no idea how to include libbsd or use pkgconf.
I have tried for a couple of hours, but I couldn't find the solution.

If someone could redirect me to a manual or explain the concepts, that would be great.

Thank you for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱人如己 2025-02-08 08:31:45

strlcpy 函数包含在 posix bsd操作系统。为了使其由编译器识别,您必须首先通过软件包管理器安装库,其名称将是 libbsd libbsd-dev libbsd-devel 取决于您的发行版是否使用单独的开发库,而不是将其包含在&lt; bsd/string.h&gt; 。然后,您可以使用(假设使用GCC) gcc&lt; your -filename&gt; .c -lbsd ,指定库为链接。由于可移植性问题(POSIX不完整),我不建议在BSD特定软件之外使用BSD功能。

The strlcpy function is included in the BSD libc, a superset (extended version) of the POSIX standard library for BSD operating systems. For it to be recognized by the compiler you have to first install the library through your package manager, the name of which will be either libbsd, libbsd-dev or libbsd-devel depending on whether your distribution uses seperate development libraries or not, and than include it as <bsd/string.h>. You can then compile it with (assuming you use GCC) gcc <your-filename>.c -lbsd, specifying the library to be linked. I wouldn't recommended using BSD functions outside of BSD specific software due to portability issues (POSIX incompliences).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文