对memcpy_s的未定义引用

时间:2015-07-07 19:59:05

标签: c memcpy undefined-reference c11 tr24731

我正在尝试修复对memcpy_s()错误的未定义引用。我已将string.h添加到我的文件中,memcpy()功能正常,我也尝试了memory.h。我在x64 Windows 7上并使用gcc 4.8.1进行编译。

#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);
}

1 buf的内存已在主函数中分配,调用doMemCpy(buf, 64, in, bytes)in是从标准输入读取的字符串

来自cmd终端的确切错误:

  

对“memcpy_s”collect2.exe的未定义引用:错误:ld返回1退出状态

2 个答案:

答案 0 :(得分:4)

我从来没有用过这个,但AFAIK,你需要添加

#define __STDC_WANT_LIB_EXT1__ 1

#include <string.h>

使用memcpy_s()

答案 1 :(得分:4)

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.