Visual C++ 中的部署Deployment in Visual C++

在开发计算机以外的计算机上安装应用程序称为部署。Installation of your application on a computer other than your development computer is known as deployment. 将 Visual C++ 应用程序部署到另一台计算机时,必须安装该应用程序及其依赖的任何库文件。When you deploy a Visual C++ application to another computer, you must install both the application and any library files it depends on. Visual Studio 提供三种方法来部署 Visual C++ 库以及你的应用程序:集中部署、本地部署和静态链接。Visual Studio enables three ways to deploy the Visual C++ libraries together with your application: central deployment, local deployment, and static linking. 集中部署将库文件放在 Windows 目录下,Windows 更新服务可在其中自动更新它们。Central deployment puts the library files under the Windows directory, where the Windows Update service can update them automatically. 本地部署将库文件放在与应用程序相同的目录中。Local deployment puts the library files in the same directory as your application. 要进行更新,用户必须亲自重新部署任何本地部署的库。You must redeploy any locally deployed libraries yourself to update them. 静态链接将库代码绑定到应用程序中。Static linking binds the library code into your application. 使用静态链接时,必须重新编译和重新部署应用程序才能利用库的任何更新。You must recompile and redeploy your application to take advantage of any updates to the libraries when you use static linking.

在 Visual Studio 2015 中,Microsoft C 运行时库被重构为特定于版本的本地库组件和一个新的通用 C 运行库,该库现在属于 Windows。In Visual Studio 2015, the Microsoft C Runtime library was refactored into version-specific local library components, and a new Universal C Runtime library that is now part of Windows. 如需深入了解通用 CRT 的部署,请参阅通用 CRT 部署For details on deployment of the Universal CRT, see Universal CRT deployment.

集中部署Central Deployment

在集中部署中,库 DLL 文件安装在 Windows\System32 目录中,对于 x64 系统上的 32 位库文件,则安装在 Windows\SysWow64 目录中。In central deployment, library DLL files are installed in the Windows\System32 directory, or for 32-bit library files on x64 systems, the Windows\SysWow64 directory. Microsoft 会自动更新集中部署的库。Microsoft automatically updates its libraries that are centrally deployed. 对于本地部署或静态链接的 Visual C++ 库,必须提供更新。For Visual C++ libraries that are locally deployed or statically linked, you must provide the updates.

要集中部署 Visual C++ 库,可使用以下两个源中的一个源来安装文件:To centrally deploy Visual C++ libraries, you can use one of these two sources for the files to install:

  • 可再发行组件包文件,这是独立的命令行可执行文件,包含压缩形式的所有 Visual C++ 可再发行库,或Redistributable package files, which are stand-alone command-line executables that contain all the Visual C++ redistributable libraries in compressed form, or

  • 可再发行合并模块(.msm 文件),可用于部署特定的库,并且将其包含在应用程序的 Windows Installer (.msi) 文件中。Redistributable merge modules (.msm files), which you can use to deploy specific libraries, and which you include in your application’s Windows Installer (.msi) file.

可再发行组件包文件将安装特定系统体系结构的所有 Visual C++ 库。A redistributable package file installs all of the Visual C++ libraries for a particular system architecture. 例如,如果应用程序是为 x64 生成的,则可使用 vcredist_x64.exe 可再发行组件包安装应用程序使用的所有 Visual C++ 库。For example, if your application is built for x64, you can use the vcredist_x64.exe redistributable package to install all the Visual C++ libraries your application uses. 安装应用程序之前,可编程应用程序安装程序以运行作为先决条件的可再发行组件包。You can program your application installer to run the redistributable package as a prerequisite before you install your application.

合并模块可将特定 Visual C++ 库的安装逻辑包含在 Windows Installer 应用程序的安装文件中。A merge module enables the inclusion of setup logic for a specific Visual C++ library in a Windows Installer application setup file. 可包含应用程序所要求的任意数量的合并模块。You can include as many or as few merge modules as your application requires. 如果需要最小化部署二进制文件的大小,请使用合并模块。Use merge modules when you need to minimize the size of your deployment binaries.

由于使用可再发行组件包或合并模块进行集中部署使 Windows 更新能自动更新 Visual C++ 库,因此建议在应用程序中使用库 DLL 而不是静态库,并使用集中部署而不使用本地部署。Because central deployment by using a redistributable package or merge modules enables Windows Update to automatically update the Visual C++ libraries, we recommend that you use the library DLLs in your application instead of static libraries, and use central deployment instead of local deployment.

本地部署Local Deployment

在本地部署中,库文件以及可执行文件安装在应用程序文件夹中。In local deployment, library files are installed in your application folder together with the executable file. 不同版本的 Visual C++ 可再发行库可安装在同一文件夹中,因为每个版本的文件名都包含其版本号。Different versions of Visual C++ redistributable libraries can be installed in the same folder because the file name of each version includes its version number. 例如,C++ 运行时库的版本 12 是 msvcp120.dll,版本 14 是 msvcp140.dll。For example, version 12 of the C++ runtime library is msvcp120.dll, and version 14 is msvcp140.dll.

库可以分布在多个附加 DLL 中,称为“点库”。A library may be spread across multiple additional DLLs, known as dot libraries. 例如,Visual Studio 2017 版本 15.6 中发布的标准库中的某些功能已添加到 msvcp140_1.dll 中,以维护 msvcp140.dll 的 ABI 兼容性。For example, some functionality in the standard library released in Visual Studio 2017 version 15.6 was added into msvcp140_1.dll, to preverve the ABI compatibility of msvcp140.dll. 如果使用 Visual Studio 2017 版本 15.6(工具集 14.13)或 Visual Studio 2017 中更高版本的工具集,则可能需要本地部署这些点库以及主库。If you use Visual Studio 2017 version 15.6 (toolset 14.13), or a later toolset from Visual Studio 2017, you may need to locally deploy these dot libraries as well as the main library. 然后,当 ABI 发生更改时,这些单独的点库将滚动到基本库的下一个主版本中。These separate dot libraries are then rolled into the next major version of the base library, when the ABI changes.

由于 Microsoft 无法自动更新本地部署的 Visual C++ 库,因此不建议本地部署这些库。Because Microsoft cannot automatically update locally deployed Visual C++ libraries, we do not recommend local deployment of these libraries. 如果你决定使用可再发行库的本地部署,建议你实现自己的自动更新本地部署库的方法。If you decide to use local deployment of redistributable libraries, we recommend that you implement your own method of automatically updating the locally deployed libraries.

静态链接Static Linking

除动态链接的库外,Visual Studio 还会将其大多数库作为静态库提供。In addition to dynamically linked libraries, Visual Studio supplies most of its libraries as static libraries. 可将静态库静态链接到应用程序,即将库对象代码直接链接到应用程序中。You can statically link a static library to your application, that is, link the library object code directly into the application. 这将创建一个没有 DLL 依赖项的二进制文件,因此无需单独部署 Visual C++ 库文件。This creates a single binary without a DLL dependency, so that you don't have to deploy the Visual C++ library files separately. 但是,我们不建议采用这种方法,因为静态链接的库无法就地更新。However, we do not recommend this approach because statically-linked libraries cannot be updated in place. 如果你使用静态链接并且希望更新链接的库,则必须重新编译和重新部署应用程序。If you use static linking and you want to update a linked library, you have to recompile and redeploy your application.

部署问题疑难解答Troubleshooting deployment issues

Visual C++ 库的加载顺序与系统相关。The load order of Visual C++ libraries is system-dependent. 若要诊断加载程序问题,请使用 depends.exe 或 where.exe。To diagnose loader issues, use depends.exe or where.exe. 有关详细信息,请参阅 Dynamic-Link Library Search Order (Windows)(动态链接库搜索顺序 (Windows))。For more information, see Dynamic-Link Library Search Order (Windows).

请参阅See also