VC++程序脱机运行DLL库缺失问题总结

当VC++程序在未安装VS环境的电脑上运行时,可能会出现DLL库缺失的问题。这通常由于缺少MFC和C++运行时库导致。解决方案包括静态链接CRT库、安装相应版本的VCRedist或调整工程配置。对于VS2015,Universal CRT的变化需要通过Windows Update或VCRedist安装更新。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

        常用VS开发软件的同学们经常会遇到“程序在本机运行正常,拷贝到其他电脑上运行会报***.DLL缺失”的问题,本人也是深受该问题折磨,在此对该类问题做一个总结。为了方便问题的描述,我将开发程序的电脑称为“developer",运行程序的电脑称为”runner“。

一、追根索源

        表面上看,此类问题一般由两种原因引起:一是runner没有安装VS开发环境;二是runner的VS版本与developer不一致。具体细究起来,又可以发现,一般缺这么几个库(分为release和debug:

1,release版

mfc100.dll        msvcp100.dll        msvcr100.dll        

2,debug版

mfc100d.dll        msvcp100d.dll        msvcr100d.dll 


注:以上仅以vs2010为例,如果是vs2012或vs2013,则其中的数字分别为110和120。而vs2015不遵循这个规则,它引入了Universal CRT(C-Runtime),后面专门说。


        其中,mfc100.dll是MFC工程相关的库,另外两个是Mircrosoft Visual C++ Runtime Library相关的库。

        如果新建的工程是MFC工程或MFC DLL库,则必须包含MFC库。因此,我们新建工程的时候,如无必要,尽量新建为win32 console工程或win32 DLL工程,这样就不需要包含MFC的库。

        而另外两个CRT库是微软的VC++编译器依赖的C-Runtime库,不同的C/C++编译器有不同的C-Rumtime库。不同版本的编译器也不相同,故有msvcp*.dll和msvcr*.dll。


3,VS2015的CRT的变化

        到VS2015的时候,CRT的包含规则发生了一些变化,可以参考MSDN上Visual C++团队的博客:Introuducing the Universal CRT。具体包括:

1)msvcr*.dll不再存在了,它的功能被分散到编译器版本相关的”vcruntime140.dll“和操作系统相关的”ucrtbase.dll“。

2)”ucrtbase.dll“是操作系统的一部分,不能像之前的msvcr*.dll,直接拷贝到*.exe下就可运行,而是需要在runner上安装VS2015的”redis.exe“(redistribution)。

3)CRT相关的头文件、源文件和库文件从 C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC 迁移到了C:\Program Files (x86)\Windows Kits\10 。

4)它包含有以下几个版本

    $(UniversalCRT_IncludePath)
    $(UniversalCRT_LibraryPath_x86)
    $(UniversalCRT_LibraryPath_x64)
    $(UniversalCRT_LibraryPath_arm)
5)它包含以下几个库

    Release DLLs   (/MD ): msvcrt.lib   vcruntime.lib      ucrt.lib
    Debug DLLs     (/MDd): msvcrtd.lib  vcruntimed.lib     ucrtd.lib
    Release Static (/MT ): libcmt.lib   libvcruntime.lib   libucrt.lib
    Debug Static   (/MTd): libcmtd.lib  libvcruntimed.lib  libucrtd.lib
二、解决方案

1,static link

        编译程序的时候,采用静态链接的方法,可以将相关依赖库直接嵌入程序包中。

参考文章摘要:

We have two ways to link  the CRT library : static link and dynamic link . For  more information , please reference http://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx  .

Therefore, there are two solutions :

1: Using /MT . Follow the step to change runtime library : Project Properties->Configuration Properties->Code Generation->Runtime Library. And then Build->Build solution . Now you can use the output application directly.

2: Using /MD . Please try to  install CRT of Visual Studio  2010 on the target machine. Remember to choose vcredist_x86 or vcredist_x64 of the best suitable and latest version according to which platform the application targeting ,then download and install it .

For Microsoft Visual C++ 2010 SP1 Redistributable Package (x64) ,I recommend the follow download link : http://www.microsoft.com/download/en/details.aspx?id=13523 .

For Microsoft Visual C++ 2010 SP1 Redistributable Package (x86) , I recommend this one :http://www.microsoft.com/download/en/details.aspx?id=8328.


2,清空”运行库“选项

        将”工程属性 -> C/C++ -> 代码生成 -> 运行库“选项,清空。默认为MT或MdT,即多线程(Multiple-Thread)。该方法适用于不依赖运行时库的程序,不可通用。


3,安装redis.exe

        在msdn或bing中搜索redis.exe,找到对应的版本,下载安装即可。


三、VS2015

In the past, you might have used one of the many ways described in “Deployment in Visual C++” to redistribute the Visual C++ libraries along with your software. For all Visual C++ libraries except the Universal CRT, there is no change in the way deployment can be done. Whatever mode of deployment (central, local or static linking) was used earlier can still be used.

However, with the above mentioned change to move the Universal CRT into the Windows Operating System, there are a few note-worthy changes:

  1. The Universal CRT is a Windows operating system component. It is a part of Windows 10. For Windows versions prior to Windows 10, the Universal CRT is distributed via Windows Update. There are Windows Update MSU packages for Windows Vista through Windows 8.1. Currently these MSU packages are installed as part of the VCRedist installation. In a future build of Visual Studio 2015, these MSU packages will also be distributed separately as part of the Universal CRT SDK and made available for download on support.microsoft.com.

  2. If you build software designed for use on Windows operating systems where the Universal CRT is not guaranteed to be installed (i.e., Windows 8.1 and below), your software will need to depend on the above mentioned Windows Update packages to install the Universal CRT.

  3. If you currently use the VCRedist (our redistributable package files), then things will just work for you as they did before. The Visual Studio 2015 VCRedist package includes the above mentioned Windows Update packages, so simply installing the VCRedist will install both the Visual C++ libraries and the Universal CRT. This is our recommended deployment mechanism. On Windows XP, for which there is no Universal CRT Windows Update MSU, the VCRedist will deploy the Universal CRT itself.

  4. If you currently statically link the Visual C++ libraries, then things will continue to work just as they currently work for you.  We strongly recommend against static linking of the Visual C++ libraries, for both performance and serviceability reasons, but we recognize that there are some use cases that require static libraries and we will continue to support the static libraries for those reasons.

  5. There will not be a merge module for the Universal CRT. If you currently use the CRT merge modules and still want to deploy the Visual C++ libraries centrally, we recommend that you move to the above mentioned Windows Update package or to the VCRedist. Alternatively, you may choose to link statically to the Universal CRT and the Visual C++ libraries.


推荐使用第三种方法,即下载VCRedist.exe,并在runner上安装。


开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值