在 Visual Studio 中远程调试 C++ 项目Remote Debugging a C++ Project in Visual Studio

若要调试其他计算机上的 Visual Studio 应用程序,请在将要部署应用的计算机上安装和运行远程工具,将项目配置为从 Visual Studio 连接到远程计算机,然后部署和运行应用。To debug a Visual Studio application on a different computer, install and run the remote tools on the computer where you will deploy your app, configure your project to connect to the remote computer from Visual Studio, and then deploy and run your app.

远程调试器组件Remote debugger components

如需详细了解如何远程调试通用 Windows 应用 (UWP),请参阅调试已安装的应用包For information about remote debugging Universal Windows Apps (UWP), see Debug an Installed App Package.

要求Requirements

Windows 7 和更高版本(非电话)以及从 Windows Server 2008 Service Pack 2 开始的 Windows Server 版本支持远程调试器。The remote debugger is supported on Windows 7 and newer (not phone) and versions of Windows Server starting with Windows Server 2008 Service Pack 2. 有关要求的完整列表,请参阅要求For a complete list of requirements, see Requirements.

备注

不支持在通过代理连接的两台计算机之间进行调试。Debugging between two computers connected through a proxy is not supported. 不建议通过高延迟或低带宽连接(如拨号 Internet)或跨国家/地区的 Internet 进行调试,否则可能会导致调试失败或速度过慢。Debugging over a high latency or low bandwidth connection, such as dialup Internet, or over the Internet across countries is not recommended and may fail or be unacceptably slow.

下载和安装远程工具Download and Install the remote tools

在要调试的远程设备或服务器上(而不是 Visual Studio 计算机上),请从下表中的链接下载并安装正确的远程工具版本。On the remote device or server that you want to debug on, rather than the Visual Studio machine, download and install the correct version of the remote tools from the links in the following table.

  • 下载适合 Visual Studio 版本的最新远程工具。Download the most recent remote tools for your version of Visual Studio. 最新的远程工具版本与早期的 Visual Studio 版本兼容,但早期的远程工具版本与后来的 Visual Studio 版本不兼容。The latest remote tools version is compatible with earlier Visual Studio versions, but earlier remote tools versions aren't compatible with later Visual Studio versions. (例如,如果使用的是 Visual Studio 2017,请下载 Visual Studio 2017 远程工具的最新更新。(For example, if you are using Visual Studio 2017, download the latest update of the remote tools for Visual Studio 2017. 在这种情况下,请不要下载 Visual Studio 2019 的远程工具。)In this scenario, do not download the remote tools for Visual Studio 2019.)
  • 下载与要安装远程工具的计算机具有相同体系结构的远程工具。Download the remote tools with the same architecture as the machine you're installing them on. 例如,如果要在运行 64 位操作系统的远程计算机上调试 32 位应用,请安装 64 位远程工具。For example, if you want to debug a 32-bit app on a remote computer running a 64-bit operating system, install the 64-bit remote tools.
VersionVersion 链接Link 说明Notes
Visual Studio 2019Visual Studio 2019 远程工具Remote tools 兼容所有 Visual Studio 2019 版本。Compatible with all Visual Studio 2019 versions. 下载与设备操作系统(x86、x64 或 ARM64)匹配的版本。Download the version matching your device operating system (x86, x64, or ARM64). 在 Windows 服务器上,请参阅取消阻止文件下载,获取有关下载远程工具的帮助。On Windows Server, see Unblock the file download for help downloading the remote tools.
Visual Studio 2017Visual Studio 2017 远程工具Remote tools 兼容所有 Visual Studio 2017 版本。Compatible with all Visual Studio 2017 versions. 下载与设备操作系统(x86、x64 或 ARM64)匹配的版本。Download the version matching your device operating system (x86, x64, or ARM64). 在 Windows 服务器上,请参阅取消阻止文件下载,获取有关下载远程工具的帮助。On Windows Server, see Unblock the file download for help downloading the remote tools.
Visual Studio 2015Visual Studio 2015 远程工具Remote tools Visual Studio 2015 的远程工具可从 My.VisualStudio.com 获取。Remote tools for Visual Studio 2015 are available from My.VisualStudio.com. 如果出现提示,请加入免费的 Visual Studio Dev Essentials 计划,或使用 Visual Studio 订阅 ID 登录。If prompted, join the free Visual Studio Dev Essentials program, or sign in with your Visual Studio subscription ID. 在 Windows 服务器上,请参阅取消阻止文件下载,获取有关下载远程工具的帮助。On Windows Server, see Unblock the file download for help downloading the remote tools.
Visual Studio 2013Visual Studio 2013 远程工具Remote tools Visual Studio 2013 文档中的下载页面Download page in Visual Studio 2013 documentation
Visual Studio 2012Visual Studio 2012 远程工具Remote tools Visual Studio 2012 文档中的下载页面Download page in Visual Studio 2012 documentation
VersionVersion 链接Link 说明Notes
Visual Studio 2017Visual Studio 2017 远程工具Remote tools 兼容所有 Visual Studio 2017 版本。Compatible with all Visual Studio 2017 versions. 下载与设备操作系统(x86、x64 或 ARM64)匹配的版本。Download the version matching your device operating system (x86, x64, or ARM64). 在 Windows 服务器上,请参阅取消阻止文件下载,获取有关下载远程工具的帮助。On Windows Server, see Unblock the file download for help downloading the remote tools. 有关最新版本远程工具的信息,请打开 Visual Studio 2019 文档For the most recent version of the remote tools, open the Visual Studio 2019 doc.
Visual Studio 2015Visual Studio 2015 远程工具Remote tools Visual Studio 2015 的远程工具可从 My.VisualStudio.com 获取。Remote tools for Visual Studio 2015 are available from My.VisualStudio.com. 如果出现提示,请加入免费的 Visual Studio Dev Essentials 计划,或使用 Visual Studio 订阅 ID 登录。If prompted, join the free Visual Studio Dev Essentials program, or sign in with your Visual Studio subscription ID. 在 Windows 服务器上,请参阅取消阻止文件下载,获取有关下载远程工具的帮助。On Windows Server, see Unblock the file download for help downloading the remote tools.
Visual Studio 2013Visual Studio 2013 远程工具Remote tools Visual Studio 2013 文档中的下载页面Download page in Visual Studio 2013 documentation
Visual Studio 2012Visual Studio 2012 远程工具Remote tools Visual Studio 2012 文档中的下载页面Download page in Visual Studio 2012 documentation

可以通过将 msvsmon.exe 复制到远程计算机,而不是安装远程工具来运行远程调试器。You can run the remote debugger by copying msvsmon.exe to the remote computer, rather than installing the remote tools. 但是,远程调试器配置向导 (rdbgwiz.exe) 仅在安装远程工具时才可用。However, the Remote Debugger Configuration Wizard (rdbgwiz.exe) is available only when you install the remote tools. 如果要将远程调试器作为服务运行,则可能需要使用向导进行配置。You may need to use the wizard for configuration if you want to run the remote debugger as a service. 有关详细信息,请参阅(可选)将远程调试器配置为服务For more information, see (Optional) Configure the remote debugger as a service.

备注

  • 若要在 ARM 设备上调试 Windows 10 应用,请使用 ARM64,它可用于最新版本的远程工具。To debug Windows 10 apps on ARM devices, use ARM64, which is available with the latest version of the remote tools.
  • 若要在 Windows RT 设备上调试 Windows 10 应用,请使用 ARM,它仅在 Visual Studio 2015 远程工具下载中可用。To debug Windows 10 apps on Windows RT devices, use ARM, which is available only in the Visual Studio 2015 remote tools download.

提示

在某些情况下,从文件共享运行远程调试器可能是最有效的方法。In some scenarios, it can be most efficient to run the remote debugger from a file share. 有关详细信息,请参阅从文件共享运行远程调试器For more information, see Run the remote debugger from a file share.

设置远程调试器Set up the remote debugger

  1. 在远程计算机上,从“开始”菜单中查找并启动“远程调试器” 。On the remote computer, find and start the Remote Debugger from the Start menu.

    如果你没有远程计算机的管理权限,请右键单击“远程调试器”应用,然后选择“以管理员身份运行”********。If you don't have administrative permissions on the remote computer, right-click the Remote Debugger app and select Run as administrator. 否则,正常启动即可。Otherwise, just start it normally.

    如果打算附加到以管理员身份运行或在其他用户帐户(如 IIS)下运行的进程,请右键单击“远程调试器”应用,然后选择“以管理员身份运行” 。If you are planning to attach to a process which is running as an administrator, or is running under a different user account (such as IIS), right-click the Remote Debugger app and select Run as administrator. 有关详细信息,请参阅以管理员身份运行远程调试器For more information, see Run the remote debugger as an administrator.

  2. 当第一次(或在配置它之前)启动远程调试器时,将显示“远程调试配置”对话框。The first time you start the remote debugger (or before you have configured it), the Remote Debugging Configuration dialog box appears.

    远程调试器配置Remote Debugger configuration

  3. 如果未安装 Windows Web 服务 API(仅会在 Windows Server 2008 R2 上发生这种情况),请选择“安装”按钮。If the Windows Web Services API is not installed, which happens only on Windows Server 2008 R2, select the Install button.

  4. 请至少选择一种要对其使用远程工具的网络类型。Select at least one network type you want to use the remote tools on. 如果这些计算机通过域连接,则必须选择第一项。If the computers are connected through a domain, you must choose the first item. 如果这些计算机通过工作组或家庭组连接,请根据需要选择第二或第三项。If the computers are connected through a workgroup or homegroup, choose the second or third item as appropriate.

  5. 选择“配置远程调试”,配置防火墙并启动远程调试器。Select Configure remote debugging to configure the firewall and start the remote debugger.

  6. 配置完成后,将显示“远程调试器”窗口。When configuration is complete, the Remote Debugger window appears.

    (../media/remotedebuggerwindow.png "远程调试器窗口![远程调试器窗口]")Remote Debugger window

    远程调试器正在等待连接。The remote debugger is now waiting for a connection. 使用显示的服务器名称和端口号在 Visual Studio 中设置远程连接配置。Use the server name and port number shown to set the remote connection configuration in Visual Studio.

若要停止远程调试器,请选择“文件” > “退出” 。To stop the remote debugger, select File > Exit. 你可以从“开始”菜单或通过以下命令行重新启动它:You can restart it from the Start menu, or from the command line:

<Remote debugger installation directory>\msvsmon.exe

备注

如果需要为其他用户添加权限,请更改远程调试器的身份验证模式或端口号,请参阅配置远程调试器If you need to add permissions for additional users, change the authentication mode, or port number for the remote debugger, see Configure the remote debugger.

远程调试 C++ 项目Remote debug a C++ project

在下面的过程中,项目的名称和路径是 C:\remotetemp\MyMfc,且远程计算机的名称是 MJO-DL。In the following procedure, the name and path of the project is C:\remotetemp\MyMfc, and the name of the remote computer is MJO-DL.

  1. 创建名为 mymfc 的 MFC 应用程序。Create an MFC application named mymfc.

  2. 在应用程序中容易到达的地方设置断点,例如,在 MainFrm.cpp 中(位于 CMainFrame::OnCreate 的开头)。Set a breakpoint somewhere in the application that is easily reached, for example in MainFrm.cpp, at the start of CMainFrame::OnCreate.

  3. 在“解决方案资源管理器”中,右键单击该项目并选择“属性”。In Solution Explorer, right-click on the project and select Properties. 打开“调试”选项卡。Open the Debugging tab.

  4. 将“要启动的调试器”更改为“远程 Windows 调试器” 。Set the Debugger to launch to Remote Windows Debugger.

    RemoteDebuggingCPlusRemoteDebuggingCPlus

  5. 对属性进行以下更改:Make the following changes to the properties:

    设置Setting “值”Value
    远程命令Remote Command C:\remotetemp\mymfc.exeC:\remotetemp\mymfc.exe
    工作目录Working Directory C:\remotetempC:\remotetemp
    远程服务器名称Remote Server Name MJO-DL:portnumberMJO-DL:portnumber
    连接Connection 带 Windows 身份验证的远程访问Remote with Windows Authentication
    调试器类型Debugger Type 仅限本机Native Only
    部署目录Deployment Directory C:\remotetemp。C:\remotetemp.
    其他要部署的文件Additional Files to Deploy C:\data\mymfcdata.txt。C:\data\mymfcdata.txt.

    如果部署其他文件(可选),则两台计算机上必须存在该文件夹。If you deploy additional files (optional), the folder must exist on both machines.

  6. 在“解决方案资源管理器”中,右键单击解决方案,然后选择“配置管理器”。In Solution Explorer, right-click the solution and choose Configuration Manager.

  7. 对于“调试”配置,请选中“部署”复选框 。For the Debug configuration, select the Deploy check box.

    RemoteDebugCplusDeployRemoteDebugCplusDeploy

  8. 开始调试(单击“调试”>“启动调试”,或按 F5) 。Start debugging (Debug > Start Debugging, or F5).

  9. 可执行文件会自动部署到远程计算机。The executable is automatically deployed to the remote computer.

  10. 如果出现提示,请输入网络凭据以连接到远程计算机。If prompted, enter network credentials to connect to the remote machine.

    所需的凭据特定于网络的安全配置。The required credentials are specific to your network's security configuration. 例如,在域计算机上,你可以选择安全证书或输入域名和密码。For example, on a domain computer, you might choose a security certificate or enter your domain name and password. 在非域计算机上,可以输入计算机名和有效用户帐户名(如 MJO-DL\name@something.com)以及正确的密码。On a non-domain machine, you might enter the machine name and a valid user account name, like MJO-DL\name@something.com, along with the correct password.

  11. 在 Visual Studio 计算机上,你应看到在断点处已停止执行。On the Visual Studio computer, you should see that execution is stopped at the breakpoint.

    提示

    或者,你可以采用单独的步骤部署文件。Alternatively, you can deploy the files as a separate step. 在“解决方案资源管理器”中,右键单击“mymfc”节点,然后选择“部署” 。In the Solution Explorer, right-click the mymfc node and then choose Deploy.

    如果应用程序需要非代码文件,则可以在“远程 Windows 调试器”页上的“要部署的附加文件”中指定这些文件 。If you have non-code files that are required by the application, you can specify them in Additional Files to Deploy on the Remote Windows Debugger page.

    或者,可以在项目中包括文件,并在每个文件的“属性”页中将“内容”属性设置为“是” 。Alternatively, you can include the files in your project, and set the Content property to Yes in the Properties page for each file. 将这些文件复制到“远程 Windows 调试器”页上指定的“部署目录” 。These files are copied to the Deployment Directory specified on the Remote Windows Debugger page. 如果需要将文件复制到“部署目录”的子文件夹,还可以将“项类型”更改为“复制文件”并指定其他属性 。You can also change the Item Type to Copy File and specify additional properties there if you need the files to be copied to a subfolder of the Deployment Directory.

使用远程符号设置调试Set Up Debugging with Remote Symbols

你应能够使用你在 Visual Studio 计算机生成的符号调试你的代码。You should be able to debug your code with the symbols you generate on the Visual Studio computer. 使用本地符号时远程调试器的性能更佳。The performance of the remote debugger is much better when you use local symbols. 如果必须使用远程符号,则需要告诉远程调试监视器以查找远程计算机上的符号。If you must use remote symbols, you need to tell the remote debugging monitor to look for symbols on the remote machine.

从 Visual Studio 2013 Update 2 开始,你可以使用以下 msvsmon 命令行开关来使用用于托管代码的远程符号:Msvsmon /FallbackLoadRemoteManagedPdbsStarting in Visual Studio 2013 Update 2, you can use the following msvsmon command-line switch to use remote symbols for managed code: Msvsmon /FallbackLoadRemoteManagedPdbs

有关详细信息,请参阅远程调试帮助(在远程调试器窗口中,按 F1 或依次单击“帮助”>“用法” )。For more information, please see the remote debugging help (press F1 in the remote debugger window, or click Help > Usage). 有关详细信息,可以参阅 Visual Studio 2012 和 2013 中的 .NET 远程符号加载更改You can find more information at .NET Remote Symbol Loading Changes in Visual Studio 2012 and 2013

请参阅See also