strcpy_s not working with gcc

冷暖自知 提交于 2019-12-17 20:35:17

问题


I have a C++11 project, and I added some strcpy_s method calls. This works on windows, but when compiling on gcc, there is an error stating that strcpy_s symbol is not found.

I did add the line

#define __STDC_WANT_LIB_EXT1__ 1

to the code, to no avail.


回答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?




回答2:


strcpy_s and friends are not a part of C++ just yet. It seems that C++17 will have them, but as of now providing them is up to the implementations. It seems glibc doesn't.

In fact, according to the cppreference, __STDC_WANT_LIB_EXT1__ will only work if __STDC_LIB_EXT1__ is defined. On my Arch Linux it isn't.

#ifdef __STDC_LIB_EXT1__
constexpr bool can_have_strcpy_s = true;
#else
constexpr bool can_have_strcpy_s = false;
#endif

You can use strncpy. With some care, it can be safe.



来源:https://stackoverflow.com/questions/40045973/strcpy-s-not-working-with-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!