无法在Visual Studio Code中调试Rust?

8
我正在尝试在VS Code中调试Rust程序,但是出现了错误: https://i.imgur.com/KcRC8gU.png 点击确定后,VS Code会打开“settings.json”文件: https://i.imgur.com/2Wy02O5.png 我已安装了这些扩展程序: https://i.imgur.com/9QfEy0D.png 我的程序是一个简单的“hello world”应用程序。
3个回答

12

很遗憾,VS Code不能直接调试Rust :( 但是不用担心,只需要进行几个配置步骤即可完成 :)

步骤

  1. Install C/C++ extension if you are on windows and CodeLLDB if on OS X/Linux

  2. Click Debug -> Add Configuration, a launch.json file should open, you need to change the program name here manually

     {
         "version": "0.2.0",
         "configurations": [
             {
                 "name": "(Windows) Launch",
                 "type": "cppvsdbg",
                 "request": "launch",
                 "program": "${workspaceRoot}/target/debug/foo.exe",
                 "args": [],
                 "stopAtEntry": false,
                 "cwd": "${workspaceRoot}",
                 "environment": [],
                 "externalConsole": true
             },
             {
                 "name": "(OSX) Launch",
                 "type": "lldb",
                 "request": "launch",
                 "program": "${workspaceRoot}/target/debug/foo",
                 "args": [],
                 "cwd": "${workspaceRoot}",
             }
         ]
     }
    
  3. Make sure Allow setting breakpoints in any file is checkend under File -> Preferences -> Settings

如需详细步骤和更多信息,请参考我用来回答这个问题的文章

致谢 - Forrest Smith


这个方法有效!截至2023年10月,适用于Rust 1.75在Windows 10 x64和VS Code 1.83上。两个不同的MS/VSCode指南都说CodeLLDB应该可以工作,但是我无法正确配置.json文件,运行时会出现"未知错误",尽管"cargo run"正常工作。所以我卸载了CodeLLDB,只使用了C/C++ v1.18,并且使用上述配置可以工作 - 但为什么它不像CodeLLDB那样自动生成一个可用的.json文件呢?而且,现在我仍然遇到了退出代码0xc0000135和'externalConsole'已被弃用的问题,唉,又要开始另一个搜索了... - undefined

9
根据您的截图,您使用的是Windows操作系统,请按照以下步骤操作。这假定您已经处理了基础事项:
  1. 安装了推荐的rust-analyzer扩展
  2. 使用 cargo init 初始化了项目文件夹。(项目文件夹名称必须与Cargo.toml中的包名称相同。)
  3. 能够在项目目录中运行 cargo run
根据网上的各种信息,您需要安装另一个VsCode扩展程序以便进行调试。因为您使用的是Windows,所以您应该使用 MS C++ DevTools扩展 而不是 CodeLLDB。
接下来,您需要设置“launch”配置。选择 VsCode 的 Run >>> Add Configuration... 菜单项。在下拉菜单中选择 C/C++: (Windows) launch 选项。现在您将获得一个包含单个配置对象的launch.json 文件。
您需要更改 program 属性为 "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe";这取决于您的包名称与项目文件夹名称相同。 (我也改变了 cwd 属性为 "${workspaceFolder}",但我不确定它是否重要。) 为了更清晰,这是我目前在 launch.json 文件中的配置(preLaunchTask 属性是稍后使用的):
{
    "name": "(Windows) Launch",
    "type": "cppvsdbg",
    "request": "launch",
    "program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
    "preLaunchTask": "rust: cargo build",
    "args": [],
    "stopAtEntry": false,
    "cwd": "${workspaceFolder}",
    "environment": [],
    "console": "externalTerminal"
}

在这一点上,只要您已经至少构建了一次项目,您可以按下F5进行调试。

如果您希望F5在调试之前同时保存更改并重新构建项目,则还必须添加一个构建任务并配置它在调试开始之前运行。

为此,请通过打开Show All Commands框(F1Ctrl+Shift+p)添加构建任务并选择Tasks: Configure Task。选择rust: cargo build。它将在launch.json旁边创建一个tasks.json文件;默认值就足够了。我的文件看起来像这样:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cargo",
            "command": "build",
            "problemMatcher": [
                "$rustc"
            ],
            "group": "build",
            "label": "rust: cargo build"
        }
    ]
}

然后,要将所有东西连接起来,您只需要手动添加preLaunchTask属性到启动配置中,并将其值设置为任务标签。例如:"preLaunchTask":"rust: cargo build",就像我上面的示例launch.json中一样。

此时,每当您按下F5键,VsCode都会保存您的工作,重新构建您的项目,然后开始调试。


当我按下运行>>添加配置...时,它会打开一个没有下拉选项的大settings.json文件。在底部有一件事情,写着:```"launch": { "configurations": [ ] }```,但是当我将代码粘贴到列表中时,会将一堆条目标记为错误。然而,当我运行代码时收到的错误不同了——```“找不到任务‘rust: cargo build’。”```。将第二个代码添加到launch.json中无法解决这个问题。按下“仍要调试”按钮会显示配置的调试类型'cppvsdbg'不受支持。 - stunlocked

4
Visual Studio Code是一款通用的编辑器,但它可以配置以调试Rust代码。 步骤1。 假设已经安装了Visual Code、Rust和Cargo,第一步是启用所需的扩展:
  1. rust-analyzer
  2. CodeLLDB
这些只需要安装一次。 步骤2 第二步是创建Rust代码。我有一个名为"Rust"的文件夹,里面存放着Rust代码。切换到该文件夹后,使用"cargo new hello_world"命令创建一个新的Rust项目。 步骤3 第三步是更改项目的文件夹。有两个可行的选项,但只有一个会起作用。
我切换到我的Rust文件夹,然后可以通过"hello_world - src"来编辑源代码。

enter image description here

为了调试代码,需要创建一个launch.json文件,使用Run - Add configuration...。然而,该文件不正确,应该使用正确的名称<your program>。这是错误的方法。
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "type": "lldb",
        "request": "launch",
        "name": "Debug",
        "program": "${workspaceFolder}/<your program>",
        "args": [],
        "cwd": "${workspaceFolder}"
    }
]

}

文档在这一点上有些简略。正确的方法是选择一个不同的文件夹,即项目的顶层 hello_worldCargo.toml 文件可用。

enter image description here

现在,当使用“运行-添加配置...”选项,并选择“LLDB”选项时-

enter image description here

Cargo.toml 文件可以被捡起 -

enter image description here

然后使用 Cargo.toml 文件来正确构建 launch.json 文件 -

  {
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
     {
        "type": "lldb",
        "request": "launch",
        "name": "Debug executable 'hello_world'",
        "cargo": {
            "args": [
                "build",
                "--bin=hello_world",
                "--package=hello_world"
            ],
            "filter": {
                "name": "hello_world",
                "kind": "bin"
            }
        },
        "args": [],
        "cwd": "${workspaceFolder}"
    },
    {
        "type": "lldb",
        "request": "launch",
        "name": "Debug unit tests in executable 'hello_world'",
        "cargo": {
            "args": [
                "test",
                "--no-run",
                "--bin=hello_world",
                "--package=hello_world"
            ],
            "filter": {
                "name": "hello_world",
                "kind": "bin"
            }
        },
        "args": [],
        "cwd": "${workspaceFolder}"
    }
]
} 

现在,运行 - 启动调试运行 - 无调试运行都正常工作。

1
此解决方案适用于2022年的VS Code。 - yoonghm
谢谢你分享这个。你救了我一命! - lwb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接