Undefined reference to memcpy_s
Asked Answered
H

2

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

Hydraulics answered 7/7, 2015 at 19:59 Comment(5)
have both buf and in been allocated? What is the exact error you get? Show the context where you call the functionSepticidal
buf has been allocated in the main() function, and in is a string read from standard input. The error I got was undefined reference to "memcpy_s" collect2.exe: error: ld returned 1 exit statusHydraulics
new information should be added to the question. please add the info from your comment to your question.Ackley
@Ackley not required. It is unrelated to the actual question.Therine
is it trying to link to a wrong version?Acetylate
A
14

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.

Automat answered 20/10, 2016 at 19:14 Comment(0)
T
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().

Therine answered 7/7, 2015 at 20:3 Comment(7)
Would this also require a -std=c11 compile option?Murraymurre
Nope. That's for C11.End
@FredLarson it seems so from the reference that you provided. memcpy_s is since c11.Rayleigh
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 <string.h>Rayleigh
@RakholiyaJenish: I'd probably define that on the compile line too: -DSTDC_WANT_LIB_EXT1=1Murraymurre
I included #define __STDC_WANT_LIB_EXT1__ 1 before #include <string.h> 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 errorHydraulics
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.Meristic

© 2022 - 2024 — McMap. All rights reserved.