简体   繁体   English

在没有CRT,memcpy和memset内部函数的情况下构建时链接错误

[英]Linking error when building without CRT, memcpy and memset intrinsic functions

I'm trying to build an application as tiny as possible, and in doing so I'm trying to avoid use of the CRT by using Win API calls instead of standard C/C++ calls. 我正在尝试尽可能小地构建一个应用程序,并且这样做我试图通过使用Win API调用而不是标准的C / C ++调用来避免使用CRT。 Unfortunately, I'm still getting a single linker error: 不幸的是,我仍然遇到一个链接器错误:

Error   2   error LNK2001: unresolved external symbol _memcpy

I don't call memcpy anywhere in my code, so I presume one of the Windows functions is calling it. 我不会在我的代码中的任何地方调用memcpy,所以我假设其中一个Windows函数正在调用它。 Turning on intrinsic functions gives an unresolved symbol _memset, which I don't use either. 打开内部函数会给出一个未解析的符号_memset,我也没有使用它。 From my understanding, both memcpy and memset should be included with intrinsic functions enabled. 根据我的理解,memcpy和memset都应包含在启用的内部函数中。 Since my code is too long to post, here are the Win API calls in my program: 由于我的代码太长而无法发布,以下是我的程序中的Win API调用:

  • lstrcpy lstrcpy
  • wsprintf wsprintf
  • CopyMemory - the error switches to _memset when I comment this out CopyMemory - 当我发表评论时,错误会切换到_memset
  • OpenFileMapping OpenFileMapping
  • MapViewOfFile MapViewOfFile
  • CreateFileMapping 的CreateFileMapping

My questions: 我的问题:

  • Why aren't the intrinsic functions being included if I have /Oi declared? 如果我/ Oi宣布,为什么不包含内在函数?
  • Do I need to declare memset and memcpy on my own? 我需要自己声明memset和memcpy吗?
    • If so, how do I do so without Visual Studio complaining of redefinition of intrinsic functions? 如果是这样,如果没有Visual Studio抱怨重新定义内部函数,我该怎么办呢?

/Oi is not documented as necessarily inserting all intrinsics where possible, instead it merely gives the compiler the option of doing so. /Oi没有记录为必要时尽可能插入所有内在函数,而只是让编译器可以选择这样做。 I haven't been able to figure out what logic MSVC uses to reach its final conclusion, but some factors include the project mode (it's far more likely to inject the intrinsics in RELEASE as compared to DEBUG) and the length of your functions. 我无法弄清楚MSVC用什么逻辑来达到最终结论,但是一些因素包括项目模式(与DEBUG相比,它更可能在RELEASE中注入内在函数)和函数的长度。

Recent versions of Visual Studio really have integrated MSVCRT dependencies into the compiler and it's become increasingly harder to generate code that does not depend on the standard C runtime. 最近版本的Visual Studio确实将MSVCRT依赖项集成到编译器中,生成不依赖于标准C运行时的代码变得越来越困难。

The standard way of working around these issues (though extremely looked down upon by Microsoft) is to link against the system copy of MSVCRT.dll, which ships in some form or the other with all versions of Windows. 解决这些问题的标准方法(尽管微软非常鄙视)是链接MSVCRT.dll的系统副本,MSVCRT.dll 以某种形式或其他版本的Windows发布。 So long as you are using standard C functions like memset you can soundly ignore Microsoft's piercing glares of disapproval and link away to your heart's content, but don't try to use it for more complicated functions and APIs provided by the CRT. 只要您使用像memset这样的标准C函数,您就可以完全忽略微软的反对意见并将其链接到您内心的内容,但不要尝试将其用于CRT提供的更复杂的功能和API。

To link against msvcrt.dll you'll need to either use LoadLibrary and co or else use a pre-generated msvcrt.lib (Microsoft purposely does not provide one) to tell MSVC which functions are available in the system MSCRT.dll 若要链接到msvcrt.dll,您需要使用LoadLibrary和co或者使用预先生成的msvcrt.lib(Microsoft故意不提供)来告诉MSVC系统MSCRT.dll中有哪些功能可用


Update: we now publish precreated mscvrt.lib files for statically linking against the CRT (at your own risk!) for x86 and x64 platforms: https://github.com/neosmart/msvcrt.lib 更新:我们现在发布mscvrt.lib文件,用于静态链接CRT(风险自负!),适用于x86和x64平台: https//github.com/neosmart/msvcrt.lib

memset() is coming from ZeroMemory() memset()来自ZeroMemory()

On hitting this I just typed in the canonical definitions of memcpy, memmove, memset out of "The C Programming Language". 在点击这个时,我只输入了“C编程语言”中memcpy,memmove,memset的规范定义。

You cannot avoid linking to CRT. 你无法避免链接到CRT。 However, to reduce the size of your EXE, you can link to CRT statically. 但是,要减小EXE的大小,您可以静态链接到CRT。 The linker will strip out all the unneeded CRT code, so your application will be as small as possible. 链接器将删除所有不需要的CRT代码,因此您的应用程序将尽可能小。 And there won't be any linker errors. 并且不会有任何链接器错误。

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

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