Undefined reference to memcpy_s

hydrazone picture hydrazone · Jul 7, 2015 · Viewed 14.1k times · Source

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

Answer

Segfault picture Segfault · Oct 20, 2016

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.