g++ 上的 strdup 错误与 c++0x
我有一些 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
代码:
#include <string.h>
int main()
{
strdup("");
return 0;
}
I have some C++0x code. I was able to reproduce it below. The code below works fine without -std=c++0x
however i need it for my real code.
How do i include strdup in C++0x? with gcc 4.5.2
note i am using mingw. i tried including cstdlib, cstring, string.h and tried using std::. No luck.
>g++ -std=c++0x a.cpp
a.cpp: In function 'int main()':
a.cpp:4:11: error: 'strdup' was not declared in this scope
code:
#include <string.h>
int main()
{
strdup("");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
-std=gnu++0x (而不是 -std=c++0x)对我有用; -D_GNU_SOURCE 不起作用(我尝试使用交叉编译器,但也许它适用于其他类型的 g++)。
看来默认值(没有 -std=... 已通过)是“GNU C++”而不是“严格标准 C++”,因此“除了升级到 C++11 之外不要更改任何内容”的标志是 -std =gnu++0x,而不是-std=c++0x;后者意味着“升级到 C++11 并比默认情况更严格”。
-std=gnu++0x (instead of -std=c++0x) does the trick for me; -D_GNU_SOURCE didn't work (I tried with a cross-compiler, but perhaps it works with other kinds of g++).
It appears that the default (no -std=... passed) is "GNU C++" and not "strict standard C++", so the flag for "don't change anything except for upgrading to C++11" is -std=gnu++0x, not -std=c++0x; the latter means "upgrade to C++11 and be stricter than by default".
strdup
可能不包含在您链接的库中(您提到了 mingw)。我不确定它是否在 c++0x 中;我知道它不在早期版本的 C/C++ 标准中。这是一个非常简单的函数,您可以将其包含在您的程序中(尽管简单地将其称为“strdup”是不合法的,因为所有以“str”和小写字母开头的名称都保留用于实现扩展。)
strdup
may not be included in the library you are linking against (you mentioned mingw). I'm not sure if it's in c++0x or not; I know it's not in earlier versions of C/C++ standards.It's a very simple function, and you could just include it in your program (though it's not legal to call it simply "strdup" since all names beginning with "str" and a lowercase letter are reserved for implementation extensions.)
此页解释了 strdup 符合 POSIX 和 BSD 标准等,并且GNU 扩展实现了它。也许如果你用“-D_GNU_SOURCE”编译你的代码它可以工作吗?
编辑:只是为了扩展一点,除了在 POSIX 系统上包含 cstring 之外,您可能不需要任何其他东西。但是您在 Windows 上使用 GCC,这不是 POSIX,因此您需要额外的定义来启用 strdup。
This page explains that strdup is conforming, among others, to the POSIX and BSD standards, and that GNU extensions implement it. Maybe if you compile your code with "-D_GNU_SOURCE" it works?
EDIT: just to expand a bit, you probably do not need anything else than including cstring on a POSIX system. But you are using GCC on Windows, which is not POSIX, so you need the extra definition to enable strdup.
将此预处理器“_CRT_NONSTDC_NO_DEPRECATE”添加到项目属性 - > C / C ++构建 - > GCC C ++编译器 - >预处理器 - >工具设置
不要忘记检查仅预处理器(-E)
这对我在Windows mingw32上有用。
add this preprocessor "_CRT_NONSTDC_NO_DEPRECATE" to Project Properties->C/C++ Build->GCC C++ Compiler->Preprocessor->Tool Settings
Don't forget to check Preprocessor Only(-E)
This worked for me on windows mingw32.
如果使用 C++,请考虑使用 C++ 标头。
string.h
是一个 C 标头,
是其 C++ 等效项。上面的代码可以在 GCC 9.4.0
g++ -Wall -Wextra -std=c++11
上正常编译。额外的梗:如果你确实想要 C 语言中的
strdup()
(C23 之前),请使用 GCC 功能测试宏,例如__STDC_WANT_LIB_EXT2__
或_POSIX_C_SOURCE
。前者支持 C 标准扩展,后者支持 POSIX 扩展。在任何包含之前定义此宏非常重要,如下所示。引用strdup - cppreference.com:
If using C++, consider using C++ headers.
string.h
is a C header,<cstring>
is its C++ equivalent.The above code compiles fine with GCC 9.4.0
g++ -Wall -Wextra -std=c++11
.Bonus meme: if you do want
strdup()
in C (before C23), use a GCC Feature Test Macro such as__STDC_WANT_LIB_EXT2__
or_POSIX_C_SOURCE
. The former enables C-standard extensions, the latter enables POSIX extensions. It is important this macro be defined before any of your includes, as shown below.To quote from strdup - cppreference.com: