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
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.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With