Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to memcpy_s

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

like image 530
hydrazone Avatar asked Jul 07 '15 19:07

hydrazone


2 Answers

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.

like image 136
Segfault Avatar answered Sep 28 '22 13:09

Segfault


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().

like image 44
Sourav Ghosh Avatar answered Sep 28 '22 11:09

Sourav Ghosh