如何安装并包括我的C编译器的库?
我正在使用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。 我已经尝试了几个小时,但是找不到解决方案。
如果有人可以将我重定向到手册或解释概念,那就太好了。
感谢您的帮助!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 eitherlibbsd
,libbsd-dev
orlibbsd-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).