Search code examples
c++cvisual-studiostrdup

strdup or _strdup?


When I use strdup in Microsoft Visual C++, it warns me:

warning C4996: 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strdup. See online help for details.

Thus it seems _strdup is correct.

But when I use _strdup in GCC (Fedora Linux OS), the compiler shows an error:

error: ‘_strdup’ was not declared in this scope

With GCC and Linux, compiler does not show any error for strdup.

Which is correct - strdup or _strdup?

Note: I include <string.h> in my code.


Solution

  • strdup is not a standard C++ function. But it is apparently a POSIX function, and anyway it's a well known function which has been there since K&R C. So if you absolutely must use it, do not fret about any possible name collision, and just write strdup for maximum portability.