我正在尝试使用strndup
函数,但我收到错误
错误:函数“strndup”的隐式声明
[-Werror=隐式函数声明]
我四处搜索,发现它不是标准函数,因此我必须使用不同的标志进行编译。但是,我通过编译以下内容收到了同样的问题:
-std=gnu11
-Wall
-Wextra
-Werror
-Wmissing-declarations
-Wmissing-prototypes
-Werror-implicit-function-declaration
-Wreturn-type
-Wparentheses
-Wunused
-Wold-style-definition
-Wundef
-Wshadow
-Wstrict-prototypes
-Wswitch-default
-Wunreachable-code
-D_GNU_SOURCE
我正在做一项作业,因此我必须使用所有这些,但我发现我必须使用“-D_GNU_SOURCE”进行编译才能消除错误,但事实并非如此。
EDIT:
我也包括这些:
#define __STDC_WANT_LIB_EXT2__ 1
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
非常感谢帮助解决这个问题。
功能strdup
and strndup
尚未成为标准 C 的一部分。它们在 Posix.1-2008 中标准化并在<string.h>
在 Posix 系统上。你包括了吗<string.h>
?
如果你的系统没有提供这些功能,你可以自己定义它们:
#include <string.h>
#include <stdlib.h>
char *strdup(const char *s) {
size_t size = strlen(s) + 1;
char *p = malloc(size);
if (p != NULL) {
memcpy(p, s, size);
}
return p;
}
char *strndup(const char *s, size_t n) {
char *p;
size_t n1;
for (n1 = 0; n1 < n && s[n1] != '\0'; n1++)
continue;
p = malloc(n + 1);
if (p != NULL) {
memcpy(p, s, n1);
p[n1] = '\0';
}
return p;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)