为什么 Windows 和 Linux 有不同的 strdup 实现:strdup() 和 _strdup()?
Why do Windows and Linux have different strdup implementations: strdup() and _strdup()?
在 Windows 上使用 strdup
时,我发现 _strdup
是 Windows 特定的,但是当我 运行 在 [= 上使用相同的代码时19=] 它要求 strdup
没有下划线。有谁知道这种差异背后的历史,以及您在编写跨平台代码时如何处理这个问题的一些信息?
有几个函数是 POSIX 规范的一部分,即 Linux 和大多数其他 UNIX 变体,它们不是标准 C 的一部分。这些包括 strdup
、write
、read
,以及其他。
前导下划线的推理如下,摘自MSDN docs:
The Universal C Run-Time Library (UCRT) supports most of the C
standard library required for C++ conformance. It implements the C99
(ISO/IEC 9899:1999) library, with certain exceptions: The type-generic
macros defined in , and strict type compatibility in
. The UCRT also implements a large subset of the POSIX.1
(ISO/IEC 9945-1:1996, the POSIX System Application Program Interface)
C library. However, it's not fully conformant to any specific POSIX
standard. The UCRT also implements several Microsoft-specific
functions and macros that aren't part of a standard.
Functions specific to the Microsoft implementation of Visual C++ are
found in the vcruntime library. Many of these functions are for
internal use and can't be called by user code. Some are documented for
use in debugging and implementation compatibility.
The C++ standard reserves names that begin with an underscore in the
global namespace to the implementation. Both the POSIX functions and
Microsoft-specific runtime library functions are in the global
namespace, but aren't part of the standard C runtime library. That's
why the preferred Microsoft implementations of these functions have a
leading underscore. For portability, the UCRT also supports the
default names, but the Microsoft C++ compiler issues a deprecation
warning when code that uses them is compiled. Only the default names
are deprecated, not the functions themselves. To suppress the warning,
define _CRT_NONSTDC_NO_WARNINGS before including any headers in code
that uses the original POSIX names.
我已经通过 #define
检查程序是否正在为 Windows 编译,如果是,则创建另一个 #define
来映射 POSIX ] 名称到 Windows 特定名称。您可以检查几个选项,尽管最可靠的选项可能是 _MSC_VER
,如果 MSVC 是编译器,它就会被定义。
#ifdef _MSC_VER
#define strdup(p) _strdup(p)
#endif
在 Windows 上使用 strdup
时,我发现 _strdup
是 Windows 特定的,但是当我 运行 在 [= 上使用相同的代码时19=] 它要求 strdup
没有下划线。有谁知道这种差异背后的历史,以及您在编写跨平台代码时如何处理这个问题的一些信息?
有几个函数是 POSIX 规范的一部分,即 Linux 和大多数其他 UNIX 变体,它们不是标准 C 的一部分。这些包括 strdup
、write
、read
,以及其他。
前导下划线的推理如下,摘自MSDN docs:
The Universal C Run-Time Library (UCRT) supports most of the C standard library required for C++ conformance. It implements the C99 (ISO/IEC 9899:1999) library, with certain exceptions: The type-generic macros defined in , and strict type compatibility in . The UCRT also implements a large subset of the POSIX.1 (ISO/IEC 9945-1:1996, the POSIX System Application Program Interface) C library. However, it's not fully conformant to any specific POSIX standard. The UCRT also implements several Microsoft-specific functions and macros that aren't part of a standard.
Functions specific to the Microsoft implementation of Visual C++ are found in the vcruntime library. Many of these functions are for internal use and can't be called by user code. Some are documented for use in debugging and implementation compatibility.
The C++ standard reserves names that begin with an underscore in the global namespace to the implementation. Both the POSIX functions and Microsoft-specific runtime library functions are in the global namespace, but aren't part of the standard C runtime library. That's why the preferred Microsoft implementations of these functions have a leading underscore. For portability, the UCRT also supports the default names, but the Microsoft C++ compiler issues a deprecation warning when code that uses them is compiled. Only the default names are deprecated, not the functions themselves. To suppress the warning, define _CRT_NONSTDC_NO_WARNINGS before including any headers in code that uses the original POSIX names.
我已经通过 #define
检查程序是否正在为 Windows 编译,如果是,则创建另一个 #define
来映射 POSIX ] 名称到 Windows 特定名称。您可以检查几个选项,尽管最可靠的选项可能是 _MSC_VER
,如果 MSVC 是编译器,它就会被定义。
#ifdef _MSC_VER
#define strdup(p) _strdup(p)
#endif