strcpy_s not working with gcc
Asked Answered
B

2

23

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.

Boling answered 14/10, 2016 at 14:44 Comment(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: https://mcmap.net/q/585847/-why-does-mingw-w64-support-strcpy_s-while-tdm-gcc-fails-compiling-that-call/560648Sorci
Possible duplicate of Are there any free implementations of strcpy_s and/or TR24731-1?Pseudohermaphroditism
H
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?

Haddix answered 14/10, 2016 at 14:49 Comment(1)
Thanks, John. I just ended up implementing it myself.Boling
S
11

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.

Shavers answered 14/10, 2016 at 14:52 Comment(1)
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.Banebrudge

© 2022 - 2024 — McMap. All rights reserved.