21

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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Jacko
  • 12,665
  • 18
  • 75
  • 126
  • 2
    Did you look up the function to find out what it is, and where it is supported? Looks like a C11 feature to me based on a quick Google search, and C++11/C++14 are based on C99. Kinda similar: http://stackoverflow.com/q/37569204/560648 – Lightness Races in Orbit Oct 14 '16 at 14:53
  • Possible duplicate of [Are there any free implementations of strcpy\_s and/or TR24731-1?](https://stackoverflow.com/questions/10067728/are-there-any-free-implementations-of-strcpy-s-and-or-tr24731-1) – S.S. Anne Oct 10 '19 at 23:01

2 Answers2

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?

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
9

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.

krzaq
  • 16,240
  • 4
  • 46
  • 61
  • 3
    Re: "Why don't you use strncpy, though?" -- um, because it doesn't work? Yes, you can make it work, but it wasn't designed to be a "safe" replacement for `strcpy`, and it isn't. – Pete Becker Oct 14 '16 at 19:32