5 gcc compiler-errors g++ strdup c++11
我有一些C++ 0x代码.我能够在下面重现它.下面的代码工作正常,-std=c++0x但我需要它为我的真实代码.
我如何在C++ 0x中包含strdup?与gcc 4.5.2
请注意我正在使用mingw.我尝试包括cstdlib,cstring,string.h并尝试使用std ::.没运气.
>g++ -std=c++0x a.cpp
a.cpp: In function 'int main()':
a.cpp:4:11: error: 'strdup' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
码:
#include <string.h>
int main()
{
strdup("");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
小智 8
-std = gnu ++ 0x(而不是-std = c ++ 0x)对我有用; -D_GNU_SOURCE不起作用(我尝试使用交叉编译器,但也许它适用于其他类型的g ++).
看来默认的(没有-std = ...传递)是"GNU C++"而不是"严格的标准C++",所以"除了升级到C++ 11之外什么都不改变"的标志是-std = gnu ++ 0x,不是-std = c ++ 0x; 后者意味着"升级到C++ 11并且比默认更严格".
strdup可能不包含在您链接的库中(您提到过mingw).我不确定它是否在c ++ 0x中; 我知道它不是早期版本的C/C++标准.
这是一个非常简单的函数,你可以把它包含在你的程序中(虽然把它简称为"strdup"是不合法的,因为所有以"str"开头的名字和一个小写字母都保留用于实现扩展.)
char *my_strdup(const char *str) {
size_t len = strlen(str);
char *x = (char *)malloc(len+1); /* 1 for the null terminator */
if(!x) return NULL; /* malloc could not allocate memory */
memcpy(x,str,len+1); /* copy the string into the new buffer */
return x;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13323 次 |
| 最近记录: |