9

I'm trying to fix an undefined reference to memcpy_s() error. I've included string.h in my file and the memcpy() function works okay, and I've also tried including memory.h. I'm on x64 Windows 7 and using gcc 4.8.1 to compile.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

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

memory for buf has been allocated in the main function, which calls doMemCpy(buf, 64, in, bytes). in is a string read from standard input

Exact error from cmd terminal:

undefined reference to "memcpy_s" collect2.exe: error: ld returned 1 exit status

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
hydrazone
  • 111
  • 1
  • 1
  • 5

2 Answers2

13

GCC 4.8 does not include the function memcpy_s, or any of the other _s bounds checking functions as far as I can tell. These functions are defined in ISO 9899:2011 Annex K and they are optional to implement. Before using them you must check if __STDC_LIB_EXT1__ is defined.

These functions were originally implemented by Microsoft and many parties objected to including them in the standard. I think the main objection is that the error handling that is done by the functions involves a global callback handle that is shared between threads, but they are also quite inefficient.

Further reading is available from Carlos O'Donell and Martin Sebor in Updated Field Experience With Annex K — Bounds Checking Interfaces.

Segfault
  • 8,036
  • 3
  • 35
  • 54
5

I've never used this, but AFAIK, you need to add

#define __STDC_WANT_LIB_EXT1__ 1

before

#include <string.h>

to use memcpy_s().

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 3
    Would this also require a `-std=c11` compile option? – Fred Larson Jul 07 '15 at 20:10
  • Nope. That's for C11. – cremno Jul 07 '15 at 20:12
  • @FredLarson it seems so from the reference that you provided. `memcpy_s` is since c11. – Rakholiya Jenish Jul 07 '15 at 20:12
  • 3
    Quoting from the above reference, _As all bounds-checked functions, memcpy_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including string.h._ the definition that you stated should be before the line `#include ` – Rakholiya Jenish Jul 07 '15 at 20:14
  • @RakholiyaJenish: I'd probably define that on the compile line too: `-DSTDC_WANT_LIB_EXT1=1` – Fred Larson Jul 07 '15 at 20:17
  • 1
    I included `#define __STDC_WANT_LIB_EXT1__ 1` before `#include ` and I'm still getting the same error. When I try compiling it with `-std=c11` I get `warning: implicit declaration of function "memcpy_s" [-Wimplicit-function-declaration]` in addition to the undefined reference error – hydrazone Jul 07 '15 at 21:00
  • 1
    I don't have access to the same compiler, but it seems unlikely that the error has anything to do with headers, defines or even compilation since this is a link time error indicating that it is missing from the library (possibly because it was not compiled to support it). Unless of course memcpy_s() is a static inline function or macro. – technosaurus Jul 22 '15 at 03:22