strdup error on g++ with c++0x

user34537 picture user34537 · Apr 7, 2011 · Viewed 14.3k times · Source

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;
}

Answer

Yossi Kreinin picture Yossi Kreinin · Nov 29, 2012

-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".