我有一个 C++11 项目,并添加了一些strcpy_s
方法调用。这适用于 Windows,但是在 gcc 上编译时,会出现一个错误,指出strcpy_s
找不到符号。
我确实添加了这条线
#define __STDC_WANT_LIB_EXT1__ 1
到代码,无济于事。
GCC (or rather, glibc) does not support strcpy_s()
and friends. For some ideas on where you can find a library which does support them, see here: Are there any free implementations of strcpy_s and/or TR24731-1?
strcpy_s
和朋友们还不是 C++ 的一部分。似乎C++17将拥有它们,但目前提供它们取决于实现。似乎 glibc 没有。
事实上,根据cppreference,只有在被定义__STDC_WANT_LIB_EXT1__
时才会起作用。__STDC_LIB_EXT1__
在我的 Arch Linux 上不是这样。
#ifdef __STDC_LIB_EXT1__
constexpr bool can_have_strcpy_s = true;
#else
constexpr bool can_have_strcpy_s = false;
#endif
您可以使用strncpy
. 稍加小心,它可能是安全的。