然后将项目在linux运行,在 Visual Studio 中部署、运行和调试 Linux MSBuild C++ 项目 | Microsoft Docs...

部署、运行和调试 Linux MSBuild 项目Deploy, run, and debug your Linux MSBuild project

08/08/2020

本文内容

Linux 支持在 Visual Studio 2017 及更高版本中提供。Linux support is available in Visual Studio 2017 and later. 若要查看这些版本的文档,请将目录上方的“版本”下拉列表设置为“Visual Studio 2017”或“Visual Studio 2019”。To see the documentation for these versions, set the Version drop-down located above the table of contents to Visual Studio 2017 or Visual Studio 2019.

在 Visual Studio 中创建基于 MSBuild 的 Linux C++ 项目,并使用 Linux 连接管理器连接到该项目后,即可运行和调试该项目。Once you've created a MSBuild-based Linux C++ project in Visual Studio and you've connected to the project using the Linux Connection Manager, you can run and debug the project. 在远程目标上编译、执行和调试代码。You compile, execute, and debug the code on the remote target.

Visual Studio 2019 版本 16.1:可以面向不同 Linux 系统进行调试和生成。Visual Studio 2019 version 16.1 You can target different Linux systems for debugging and building. 例如,可以在 x64 系统上进行交叉编译,并在定位 IoT 方案时部署到 ARM 设备。For example, you can cross-compile on x64 and deploy to an ARM device when targeting IoT scenarios. 有关详细信息,请参阅本文后面的指定用于生成和调试的不同计算机。

与 Linux 项目交互并对其进行调试方法有若干种。There are several ways to interact with and debug your Linux project.

使用 Visual Studio 传统功能(例如断点、监视窗口和悬停在变量上)进行调试。Debug using traditional Visual Studio features, such as breakpoints, watch windows, and hovering over a variable. 使用这些方法,可以像平常调试其他项目类型那样进行调试。Using these methods, you may debug as you normally would for other project types.

从 Linux 控制台窗口中的目标计算机查看输出。View output from the target computer in the Linux Console window. 还可以使用控制台将输入发送到目标计算机。You can also use the console to send input to the target computer.

调试 Linux 项目Debug your Linux project

在“调试”属性页中选择调试模式。Select debugging mode in the Debugging property page.

GDB 用于调试在 Linux 上运行的应用程序。GDB is used to debug applications running on Linux. 在远程系统(而非 WSL)上进行调试时,GDB 可以在两种不同的模式下运行,可从项目“调试”属性页中的“调试模式”选项中进行选择:When debugging on a remote system (not WSL) GDB can run in two different modes, which can be selected from the Debugging Mode option in the project's Debugging property page:

e794957a8b779b342f6c598e83b34686.png

GDB 用于调试在 Linux 上运行的应用程序。GDB is used to debug applications running on Linux. GDB 能以两种不同的模式运行,可从项目“调试”属性页中的“调试模式”选项中进行选择:GDB can run in two different modes, which can be selected from the Debugging Mode option in the project's Debugging property page:

a68dfc38b4131a152078a9b0fa0b3151.png

在 gdbserver 模式下,GDB 在本地运行,连接到在远程系统上的 gdbserver。In gdbserver mode, GDB is run locally, which connects to gdbserver on the remote system.

在 gdb 模式下,Visual Studio 调试程序在远程系统上驱动 GDB 。In gdb mode, the Visual Studio debugger drives GDB on the remote system. 如果 GDB 的本地版本与目标计算机上安装的版本不兼容,则这是更好的选择。This is a better option if the local version of GDB isn't compatible with the version installed on the target computer. 这是 Linux 控制台窗口唯一支持的模式。This is the only mode that the Linux Console window supports.

备注

如果不能在 gdbserver 调试模式中命中断点,请尝试 gdb 模式。If you are unable to hit breakpoints in gdbserver debugging mode, try gdb mode. 必须先将 gdb 安装在远程目标上。gdb must first be installed on the remote target.

使用 Visual Studio 中的标准“调试”工作栏,选择远程目标。Select the remote target using the standard Debug toolbar in Visual Studio.

远程目标可用时,将看见它按名称或 IP 地址列出。When the remote target is available, you'll see it listed by either name or IP address.

41dedf71c04a720dc52d1e645d317d1b.png

如果还未连接到远程目标,将会看见使用 Linux 连接管理器连接到远程目标的说明。If you haven't connected to the remote target yet, you'll see an instruction to use Linux Connection Manager to connect to the remote target.

706381632e59cab6b9f1dbda9ea44133.png

在已知将执行的某些代码的左滚动条槽中单击,设置断点。Set a breakpoint by clicking in the left gutter of some code that you know will execute.

设置断点的代码行上出现一个红点。A red dot appears on the line of code where you set the breakpoint.

按 F5(或者“调试”>“开始调试”)开始调试。Press F5 (or Debug > Start Debugging ) to start debugging.

开始调试时,应用程序先在远程目标上编译,再启动。When you start debugging, the application is compiled on the remote target before it starts. 任何编译错误都将显示在“错误列表”窗口中。Any compilation errors will appear in the Error List window.

如果没有错误,则应用将启动并且调试程序将在断点处暂停。If there are no errors, the app will start and the debugger will pause at the breakpoint.

ef984b7f960790138b8672dbdbccfe8f.png

现在,可通过按命令键(如 F10 或 F11),与处于当前状态的应用程序交互、查看变量以及逐行执行代码。Now, you can interact with the application in its current state, view variables, and step through code by pressing command keys such as F10 or F11.

如果需要使用 Linux 控制台与应用进行交互,请选择“调试”>“Linux 控制台”。If you want to use the Linux Console to interact with your app, select Debug > Linux Console.

cd6b53e149e73a52c55b3293f635b15d.png

此控制台将显示来自目标计算机的全部控制台输出并接收输入,然后将其发送到目标计算机。This console will display any console output from the target computer and take input and send it to the target computer.

31c0f7fffa49d0af458de7c0a45d4cf4.png

配置其他调试选项(MSBuild 项目)Configure other debugging options (MSBuild projects)

可以使用项目“调试”属性页中的“程序参数”项将命令行参数传递给可执行文件。Command-line arguments can be passed to the executable using the Program Arguments item in the project's Debugging property page.

可以通过在项目的“调试”属性页中使用“预启动命令”来导出 DISPLAY 环境变量。You can export the DISPLAY environment variable by using the Pre-Launch Command in the project's Debugging property pages. 例如:export DISPLAY=:0.0For example: export DISPLAY=:0.0

34e09be874111376c2ea89dac320e592.png

可使用“ 其他调试程序命令 ”条目将特定调试程序选项传递到 GDB。Specific debugger options can be passed to GDB using the Additional Debugger Commands entry. 例如,你可能需要忽略 SIGILL(非法指令)信号。For example, you might want to ignore SIGILL (illegal instruction) signals. 可以使用 handle 命令实现此目的,方法是将以下命令添加到上图所示的“其他调试程序命令”条目:You could use the handle command to achieve this by adding the following to the Additional Debugger Commands entry as shown above:

handle SIGILL nostop noprint

可以使用项目“调试”属性页中的“GDB 路径”项来指定 Visual Studio 使用的 GDB 的路径。You can specify the path to the GDB used by Visual Studio using the GDB Path item in the project's Debugging property page. Visual Studio 2019 版本 16.9 及更高版本中提供了此属性。This property is available in Visual Studio 2019 version 16.9 and later.

使用“附加到进程”进行调试Debug with Attach to Process

Visual Studio 项目的调试属性页和 CMake 项目的 Launch.vs.json 设置具有允许附加到正在运行的进程的设置 。The Debugging property page for Visual Studio projects, and the Launch.vs.json settings for CMake projects, have settings that enable you to attach to a running process. 如果需要除了这些设置中提供的控制外的其他控制,则可以将名为 Microsoft.MIEngine.Options.xml 的文件置于解决方案或工作区的根路径中。If you require additional control beyond what is provided in those settings, you can place a file named Microsoft.MIEngine.Options.xml in the root of your solution or workspace. 下面是一个简单的示例:Here is a simple example:

ExePath="C:\temp\ConsoleApplication17\ConsoleApplication17\bin\x64\Debug\ConsoleApplication17.out"/>

-enable-pretty-printing

AttachOptionsForConnection 具有你可能需要的大多数属性 。The AttachOptionsForConnection has most of the attributes you might need. 上述示例展示了如何指定搜索其他 .so 库的位置。The example above shows how to specify a location to search for additional .so libraries. 子元素 ServerOptions 允许改为使用 gdbserver 附加到远程进程 。The child element ServerOptions enables attaching to the remote process with gdbserver instead. 若要执行此操作,需要指定本地 gdb 客户端(Visual Studio 2017 中附带的客户端如上所示)和带符号的二进制文件的本地副本。To do that, you need to specify a local gdb client (the one shipped in Visual Studio 2017 is shown above) and a local copy of the binary with symbols. SetupCommands 元素允许将命令直接传递给 gdb 。The SetupCommands element enables you to pass commands directly to gdb. 可以在 GitHub 上找到 LaunchOptions.xsd 架构中提供的所有选项。You can find all the options available in the LaunchOptions.xsd schema on GitHub.

指定用于在基于 MSBuild 的 Linux 项目中生成和调试的不同计算机Specify different machines for building and debugging in MSBuild-based Linux projects

在 Visual Studio 2019 版本 16.1 中,可以将远程生成计算机与远程调试计算机分开,以便处理基于 MSBuild 的 Linux 项目和面向远程 Linux 计算机的 CMake 项目。In Visual Studio 2019 version 16.1, you can separate your remote build machine from your remote debug machine for both MSBuild-based Linux projects and CMake projects that target a remote Linux machine. 例如,现在可以在 x64 系统上进行交叉编译,并在定位 IoT 方案时部署到 ARM 设备。For example, you can now cross-compile on x64 and deploy to an ARM device when targeting IoT scenarios.

默认情况下,远程调试计算机与远程生成计算机相同(“配置属性” > “常规” > “远程生成计算机”)。By default, the remote debug machine is the same as the remote build machine ( Configuration Properties > General > Remote Build Machine ). 若要指定新的远程调试计算机,请在解决方案资源管理器中右键单击该项目,然后转到“配置属性” > “调试” > “远程调试计算机”。To specify a new remote debug machine, right-click on the project in Solution Explorer and go to Configuration Properties > Debugging > Remote Debug Machine.

73c3e2f9a6e9a1f0a50e81eb3402489e.png

“远程调试计算机”的下拉菜单中填充了所有已建立的远程连接。The drop-down menu for Remote Debug Machine is populated with all established remote connections. 若要添加新的远程连接,请导航到“工具” > “选项” > “跨平台” > “连接管理器”,或者在“快速启动”中搜索“连接管理器” 。To add a new remote connection, navigate to Tools > Options > Cross Platform > Connection Manager or search for "Connection Manager" in Quick Launch. 另外,还可以在项目的“属性页”(“配置属性” > “常规” > “远程部署目录”)中指定新的远程部署目录。You can also specify a new remote deploy directory in the project's Property Pages ( Configuration Properties > General > Remote Deploy Directory ).

默认情况下,只会将调试过程所需的文件部署到远程调试计算机。By default, only the files necessary for the process to debug will be deployed to the remote debug machine. 可以使用解决方案资源管理器配置将部署到远程调试计算机的源文件。You can use Solution Explorer to configure which source files will be deployed to the remote debug machine. 单击源文件时,可以在解决方案资源管理器的正下方看到其文件属性的预览。When you click on a source file, you'll see a preview of its File Properties directly below the Solution Explorer.

463a7afa28223b3ae3a23eb04a7960ba.png

“内容”属性指定是否将文件部署到远程调试计算机。The Content property specifies whether the file will be deployed to the remote debug machine. 可以导航到“属性页” > “配置管理器”并取消选中“部署”以获取所需配置,通过这种方式完全禁用部署 。You can disable deployment entirely by navigating to Property Pages > Configuration Manager and unchecking Deploy for the desired configuration.

在某些情况下,可能需要对项目的部署进行更多控制。In some cases, you may require more control over your project's deployment. 例如,你要部署的某些文件可能超出解决方案的范围,或者你需要为每个文件或目录自定义远程部署目录。For example, some files that you want to deploy might be outside of your solution or you want to customize your remote deploy directory per file or directory. 在这些情况下,将以下代码块附加到 .vcxproj 文件,并将“example.cpp”替换为实际文件名:In these cases, append the following code block(s) to your .vcxproj file and replace "example.cpp" with the actual file names:

$(RemoteTarget)

$(RemoteDebuggingTarget)

~/example.cpp

~/example.cpp

RemoteRemote

true

CMake 项目CMake projects

对于面向远程 Linux 计算机的 CMake 项目,可以在 launch.vs.json 中指定新的远程调试计算机。For CMake projects that target a remote Linux machine, you can specify a new remote debug machine in launch.vs.json. 默认情况下,“remoteMachineName”的值与 CMakeSettings.json 中的“remoteMachineName”属性同步,该属性对应于远程生成计算机。By default, the value of "remoteMachineName" is synchronized with the "remoteMachineName" property in CMakeSettings.json, which corresponds to your remote build machine. 这些属性不再需要匹配,launch.vs.json 中“remoteMachineName”的值将指示将哪台远程计算机用于部署和调试。These properties no longer need to match, and the value of "remoteMachineName" in launch.vs.json will dictate which remote machine is used for deploy and debug.

3c8618b6f1da726c73284dcd8ca0bd28.png

IntelliSense 将建议使用包含所有已建立的远程连接的列表。IntelliSense will suggest all a list of all established remote connections. 可以导航到“工具” > “选项” > “跨平台” > “连接管理器”,或者在“快速启动”中搜索“连接管理器”,以便添加新的远程连接 。You can add a new remote connection by navigating to Tools > Options > Cross Platform > Connection Manager or searching for "Connection Manager" in Quick Launch.

如果要完全控制部署,可以将以下代码块附加到 launch.vs.json 文件。If you want complete control over your deployment, you can append the following code block(s) to the launch.vs.json file. 请务必将占位符值替换为实际值:Remember to replace the placeholder values with real values:

"disableDeploy": false,

"deployDirectory": "~\foo",

"deploy" : [

{

"sourceMachine": "127.0.0.1 (username=example1, port=22, authentication=Password)",

"targetMachine": "192.0.0.1 (username=example2, port=22, authentication=Password)",

"sourcePath": "~/example.cpp",

"targetPath": "~/example.cpp",

"executable": "false"

}

]

后续步骤Next steps

若要在 Linux 上调试 ARM 设备,请参阅此博客文章:Debugging an embedded ARM device in Visual Studio(在 Visual Studio 中调试嵌入的 ARM 设备)。To debug ARM devices on Linux, see this blog post: Debugging an embedded ARM device in Visual Studio.

另请参阅See also

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值