19

我有一个 C++11 项目,并添加了一些strcpy_s方法调用。这适用于 Windows,但是在 gcc 上编译时,会出现一个错误,指出strcpy_s找不到符号。

我确实添加了这条线

#define __STDC_WANT_LIB_EXT1__ 1

到代码,无济于事。

4

2 回答 2

18

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?

于 2016-10-14T14:49:44.487 回答
9

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. 稍加小心,它可能是安全的。

于 2016-10-14T14:52:34.670 回答