在 Visual Studio Code 中调试之前如何自动运行构建任务?

Svi*_*len 19 visual-studio-code

在 VS Code 中,我必须先运行构建任务,然后开始调试,而在 CLion 中,我只需单击调试,然后它会在必要时自动构建并开始调试。有没有办法在 VS Code 中自动执行此操作?

Ent*_*s3d 25

向 Launch.Json 添加构建任务


我对这个主题不够熟悉,无法逐步引导您完成整个过程,但是您正在寻找的可能是如何将构建任务链接到您的调试配置。我将尝试说明下面的过程。

调试配置

要访问您的构建配置,请转到侧面的调试栏 (1),然后按齿轮图标访问您的 launch.json 配置文件 (2)。您需要在该 launch.json 文件中的配置下添加一个预启动任务,并将其链接到您的构建任务 (3)。

在 Tasks.Json 中定义构建任务


任务.json 文件

然后,您需要通过使用 ctrl-shift-b 运行它来设置构建任务。

如果它已经存在(如您的帖子中所暗示的那样),您可以在 tasks.json 文件的 .vs-code 文件夹中找到它。如果您打开该 task.json 文件,您将在列表中找到构建任务。您现在需要做的就是获取该任务的“标签”并将其放置在启动前配置中的 launch.json 中。

祝你好运!

附录


附录添加了在共享预启动构建之后并行构建和运行配置的示例。

问:如果构建任务失败,但启动过程从旧的二进制文件开始怎么办?

A:@JoKing 给出的潜在解决方案:添加一个新任务,删除二进制文件并在每次构建之前执行此任务,方法是在构建任务中使用“dependsOn”选项要求它。下面给出了一个示例,说明它在 tasks.json 文件中的外观,

   "tasks": [
    {
      "taskName": "build",
      "command": "tsc",
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "dependsOn": [
        "build client",
        "build server"
      ]
    },
    {
      "taskName": "build client",
      "command": "tsc",
      "args": [
        "-w",
        "-p",
        "${workspaceRoot}/src/typescript/client"
      ]
    },
    {
      "taskName": "build server",
      "command": "tsc",
      "args": [
        "-w",
        "-p",
        "${workspaceRoot}/src/typescript/server"
      ]
    }
  ]
Run Code Online (Sandbox Code Playgroud)

Q:我有多个配置,但是想在所有配置之前运行一次构建任务,可以吗?

A:我之前没有亲自设置过,但复合发射配置可能是您正在寻找的。该页面中的示例有两个配置,“服务器”和“客户端”,它们可以在遵循 prelaunchTask ('defaultBuildTask') 的同时并行启动。

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Server",
      "program": "${workspaceFolder}/server.js"
    },
    {
      "type": "node",
      "request": "launch",
      "name": "Client",
      "program": "${workspaceFolder}/client.js"
    }
  ],
  "compounds": [
    {
      "name": "Server/Client",
      "configurations": ["Server", "Client"],
      "preLaunchTask": "${defaultBuildTask}"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)


VMM*_*MMF 11

我花了一些时间才理解已接受的答案。从该解释中我不清楚如何通过一键单击来构建和调试我的程序。我还在 Windows 中使用 Mingw-w64。按照此链接上的说明并根据接受的答案,我创建了以下文件:

启动.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": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

任务.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "build",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

关键方面是重命名tasks.json"label": "C/C++: g++.exe build active file"中的默认值,例如我使用了这个词,然后在launch.json内部引用了同一个词(不是tasks.json的路径或链接) 。"build""preLaunchTask": "build"

请注意,重命名并不是绝对必要的。你也可以在launch.json"preLaunchTask": "C/C++: g++.exe build active file"中输入,它也会起作用。