【问题标题】:How to debug c++ code using Bazel on Windows 10 OS如何在 Windows 10 操作系统上使用 Bazel 调试 c++ 代码
【发布时间】:2019-05-18 04:18:34
【问题描述】:

最近我开始玩 Bazel,但在调试应用程序时遇到问题,我可以使用 g++ 进行调试,但无法调试生成的 Bazel 。 exe文件。

感谢您观看此内容。

另外,我使用 Bazel 和 VsCode 任务构建源代码。

  1. 我应该以哪个 .exe 文件为目标来调试我的应用程序?
  2. 我的设置好吗?
  3. 可以在 Windows 上使用 Bazel 调试 c++ 源代码。?
  4. 我应该使用 minGW 来调试 bazel 生成的可执行文件还是一些 其他工具或调试器。?

版本

  • 操作系统:Windows 10
  • Bazel 版本:构建标签:0.20.0

我有这个项目结构:

|-- CPP_TESTS
    |-- .gitignore
    |-- a.exe  <-- I generated this with g++ minGW
    |-- readme.md
    |-- WORKSPACE
    |-- .vscode
    |   |-- c_cpp_properties.json
    |   |-- launch.json
    |   |-- settings.json
    |   |-- tasks.json
    |-- project
        |-- WORKSPACE
        |-- main
            |-- app.cc
            |-- BUILD
            |-- readme.md

app.cc

#include <iostream>

int main(int argc, char const *argv[])
{
    int a = 3;
    int b = 5;
    int c = a + b;

    /* code */
    std::cout << "Hello world" << std::endl;
    std::cout << c << std::endl;
    return 0;
}

构建文件

cc_binary(
    name = "app",
    srcs = ["app.cc"]
)

然后我在 ./CPP_TESTS 的根目录上运行此命令

bazel build //project/main:app

这个命令会生成一些文件和文件夹

某些文件夹包含 app.exe 文件,例如在 bazel-bin dir 我有这个文件和文件夹

|-- bazel-bin
    |-- project
    |   |-- main
    |       |-- app.exe
    |       |-- app.exe-2.params
    |       |-- app.exe.runfiles_manifest
    |       |-- app.pdb
    |       |-- app.exe.runfiles
    |       |   |-- MANIFEST
    |       |-- _objs
    |           |-- app
    |               |-- app.obj
    |-- project-name
        |-- main
            |-- app.exe
            |-- app.exe-2.params
            |-- app.exe.runfiles_manifest
            |-- app.pdb
            |-- app.exe.runfiles
            |   |-- MANIFEST
            |-- _objs
                |-- app
                    |-- app.obj

所以如果我在

里面运行app.exe
|-- bazel-bin
    |-- project
    |   |-- main
    |       |-- app.exe   <=========

我得到这个输出

INFO: Elapsed time: 0,145s, Critical Path: 0,01s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Hello world
8
PS C:\dev\tests\cpp_tests>

如果我调试应用程序,我会收到此错误。

在我开始之前,这是我的 tasks/json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "bazel_run",
            "type": "shell",
            "command": "bazel run //project/main:app",
            "args": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "bazel_build",
            "type": "shell",
            "command": "bazel build //project/main:app",
            "args": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "bazel_debug_build",
            "type": "shell",
            "command": "bazel",
            "args": [
                "build",
                "//project/main:app",
                "--compilation_mode=dbg"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

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": [
        {                                
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            // "program": "${workspaceFolder}/a.exe",
            "program": "${workspaceFolder}\\bazel-bin\\project\\main\\app.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/MinGW/bin/gdb.exe",
            "preLaunchTask": "bazel_debug_build",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

所以我运行bazel build //project/main:app 命令

错误信息。

但是 app.exe 在那里?

【问题讨论】:

  • 嘿,如果您将其中一个标签换成 c++ 标签,您可能会获得更多浏览量。人们倾向于看那个,而不是 c++17 标签。

标签: c++ debugging visual-studio-code bazel vscode-tasks


【解决方案1】:

您应该使用位于构建文件夹bazel-out/x64_windows-dbg/project/main/app.exe 中的可执行文件。

如果您要设置断点,您可能还需要 --strip=never 参数。

如果它不起作用,请在 launch.json 中尝试“cppvsdbg”而不是“cppdbg”作为配置类型。

【讨论】:

    【解决方案2】:

    请看我对相关问题的回答:https://stackoverflow.com/a/54007919/7778502

    虽然我无法判断您的设置是否正确(我需要了解更多信息,例如您安装了哪些 VSCode 插件,其他 .json 文件的内容是什么),但我可以肯定地说您有两个 WORKSPACE 文件看起来不对。

    您应该删除project\WORKSPACE,因为Bazel 认为每个带有WORKSPACE 文件的目录树都是一个唯一的工作区。每个工作区在其根目录中只需要一个 WORKSPACE 文件。如果子目录有自己的 WORKSPACE 文件,那么它就是一个单独的工作空间,Bazel 不会在“父”工作空间中使用它的目标。

    【讨论】:

      【解决方案3】:

      首先,您需要添加一些 bazel 选项进行调试。手册在这里:enter link description here

      如果你想调试你的程序,你需要和选项如:

      bazel build --copt="-g" --strip="never" ... 
      

      【讨论】:

        猜你喜欢
        • 2017-03-02
        • 2019-05-19
        • 1970-01-01
        • 1970-01-01
        • 2013-08-13
        • 1970-01-01
        • 1970-01-01
        • 2021-09-22
        • 1970-01-01
        相关资源
        最近更新 更多