0

I am using c++. I want to build code which contains memcpy_s on linux. I am using gcc 9.3. I made some modifications.

#ifdef __cplusplus
extern "C" {
#define _STDC_WANT_LIB_EXT1_ 1
#include <string.h>
}
#endif

void doMemCopy(char* buf, size_t buf_size, char* in, int chr) {
    memcpy_s(buf, buf_size, in, chr);
}

I use CMakelist file, like:

set(CMAKE_C_FLAGS_RELEASE   "-MD -Ob3 -O2 -fp:fast -JMC- -DNDEBUG -utf-8")
set(CMAKE_CXX_FLAGS_RELEASE "-MD -Ob3 -O2 -fp:fast -JMC- -DNDEBUG -utf-8")

set(CMAKE_C_STANDARD   11)
set(CMAKE_CXX_STANDARD 17)

It gives me this error: error: ‘memcpy_s’ was not declared in this scope; did you mean ‘memcpy’? What am I doing wrong.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
Vladimir Yanakiev
  • 1,240
  • 1
  • 16
  • 25
  • 4
    It's C++. Use std::string. – stark Jan 17 '22 at 17:17
  • 3
    If you are using C++, then use C++ and use `std::copy`. For primitive types, it optimizes to a `memcpy` call. – NathanOliver Jan 17 '22 at 17:18
  • 1
    it seems at least the define should be [`__STDC_WANT_LIB_EXT1__`](https://en.cppreference.com/w/c/string/byte/memcpy#Example) - with 2 underscores – dewaffled Jan 17 '22 at 17:29
  • 2
    First question to ask is "Is there a `memcpy_s` in C++?" – user4581301 Jan 17 '22 at 17:35
  • 5
    This doesn't address the question, but wrapping a `#include` directive inside an `extern "C"` is usually a bad idea. If the header wasn't designed to be used in a C++ application, that wrapper won't help. Typically, standard headers (like `` are designed to work correctly in C++, unless you mess with them like this. Note that `extern "C"` does not mean "compile this code as C". – Pete Becker Jan 17 '22 at 17:41
  • @user4581301: Implementation-defined, see C++20 [headers] p10-11 and Table 23. – Nate Eldredge Jan 17 '22 at 18:00
  • 3
    `memcpy_s` is an optional part of the C standard and AFAIK the only major implementation that supports it is MSVC. (Microsoft invented the _s functions and convinced the C standard committee to include them as the optional Annex K, but other vendors weren't impressed and never picked them up.) So your Linux/gcc setup presumably does not. You are out of luck, sorry. – Nate Eldredge Jan 17 '22 at 18:19
  • Just use `std::copy` it will use the fastest method for chars. – Galik Jan 17 '22 at 18:23

0 Answers0