简体   繁体   English

对memcpy_s的未定义引用

[英]Undefined reference to memcpy_s

I'm trying to fix an undefined reference to memcpy_s() error. 我正在尝试修复对memcpy_s()错误的未定义引用。 I've included string.h in my file and the memcpy() function works okay, and I've also tried including memory.h . 我在我的文件中包含string.hmemcpy()函数运行正常,我也尝试过包含memory.h I'm on x64 Windows 7 and using gcc 4.8.1 to compile. 我在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);
}

memory for buf has been allocated in the main function, which calls doMemCpy(buf, 64, in, bytes) . buf内存已在main函数中分配,该函数调用doMemCpy(buf, 64, in, bytes) in is a string read from standard input in是从标准输入读取的字符串

Exact error from cmd terminal: 来自cmd终端的确切错误:

undefined reference to "memcpy_s" collect2.exe: error: ld returned 1 exit status 未定义引用“memcpy_s”collect2.exe:错误:ld返回1退出状态

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. 据我所知,GCC 4.8不包括函数memcpy_s或任何其他_s边界检查函数。 These functions are defined in ISO 9899:2011 Annex K and they are optional to implement. 这些功能在ISO 9899:2011附录K中定义,它们是可选的实施。 Before using them you must check if __STDC_LIB_EXT1__ is defined. 在使用它们之前,您必须检查是否定义了__STDC_LIB_EXT1__

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 . Carlos O'Donell和Martin Sebor将在附录K - Bounds Checking Interfaces的更新现场体验中进一步阅读。

I've never used this, but AFAIK, you need to add 我从来没有用过这个,但AFAIK,你需要添加

#define __STDC_WANT_LIB_EXT1__ 1

before 之前

#include <string.h>

to use memcpy_s() . 使用memcpy_s()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM