简体   繁体   English

在Visual Studio中构建没有C / C ++运行时的静态库

[英]Building a static lib without C/C++ runtime in Visual Studio

Is there a way to build a static library without specifying the version of C/C++ runtime? 有没有一种方法可以在不指定C / C ++运行时版本的情况下构建静态库?

I'm using Visual Studio 2017 to build a static lib, and I have to specify a version for "Runtime Library" in the "Code Generation" option page (\\MD or \\MT). 我正在使用Visual Studio 2017生成静态库,并且必须在“代码生成”选项页(\\ MD或\\ MT)中为“运行时库”指定一个版本。 If I choose one version and the application using my lib chooses another, Visual Studio will spit out the error: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' . 如果我选择一个版本,而使用我的库的应用程序选择了另一个版本,Visual Studio将会吐出错误: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease'

I know, when building an EXE, I can use the linker switch \\NODEFAULTLIB to not include C/C++ runtime, but I'm building a static lib here which doesn't even have the Linker option page in its Properties window. 我知道,在构建EXE时,可以使用链接器开关\\ NODEFAULTLIB不包括C / C ++运行时,但是我在这里构建一个静态库,该库在其“属性”窗口中甚至没有链接器选项页。

It seems that Simple DirectMedia Layer found a way to do. 似乎简单DirectMedia层找到了一种方法。

Update: I later realized that I could actually turn on \\NODEFAULTLIB switch in the Librarian option page in the static lib's Properties window. 更新:后来我意识到,我实际上可以在静态库的“属性”窗口中的“库管理器”选项页中打开\\ NODEFAULTLIB开关。

You do it by NOT using any Runtime libraries. 您可以通过不使用任何运行时库来实现。 If you don't use any of the runtime libraries then the "Code Generation" option is meaningless as the runtime stubs are not pulled into obj file. 如果您不使用任何运行时库,则“代码生成”选项将毫无意义,因为运行时存根不会被拉到obj文件中。

SDL basically say this on there web site: SDL在网站上基本上是这样说的:

On Windows, SDL does not depend on a C runtime at all, not even for malloc(). 在Windows上,SDL完全不依赖C运行时,甚至不依赖于malloc()。

As soon as you use any system include supplied by Microsoft VS then you are using there runtime library. 一旦使用Microsoft VS提供的任何系统包含,那么您就在使用运行时库。

So if you only use the Windows SDK and the API supplied by the Win32 API then you don't need the VC runtime. 因此,如果仅使用Windows SDK和Win32 API提供的API,则不需要VC运行时。

If you build a static library, it refers to your own library and not to the application as a whole. 如果您构建静态库,则它是指您自己的库,而不是整个应用程序。 If you want to distribute the application, it just means that you will not need to distribute a separate dll. 如果要分发应用程序,则仅意味着您将不需要分发单独的dll。 So do not set NODEFAULTLIB. 因此,请勿设置NODEFAULTLIB。

If you run the application on a machine where there is no Visual Studio 2017 installed, you might get an error message. 如果在未安装Visual Studio 2017的计算机上运行该应用程序,则可能会收到错误消息。 The missing dlls are system libraries that are needed to run vs applications that are compiled with VS2017. 缺少的dll是与VS2017编译的应用程序相比运行所需的系统库。 You can get the missing libraries from: https://support.microsoft.com/en-gb/help/2977003/the-latest-supported-visual-c-downloads (this link is not guaranteed to be correct in the future). 您可以从以下网址获取缺少的库: https : //support.microsoft.com/en-gb/help/2977003/the-latest-supported-visual-c-downloads (此链接不能保证将来会正确) 。 The redistributable dlls can be distributed freely. 可重新分发的dll可以自由分发。 They are called vc_redist.x86.exe and vc_redist.x64.exe. 它们分别称为vc_redist.x86.exe和vc_redist.x64.exe。 Choose vc_redist.x86.exe if you have compiled you code for 32-bits. 如果您已编译32位代码,请选择vc_redist.x86.exe。 It does not refer to the machine on which the app is run. 它不是指运行该应用程序的计算机。

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

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