如何在Windows 10操作系统上使用Bazel调试C++代码

5

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

谢谢您的关注。

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

  1. 我应该针对哪个.exe文件来调试我的应用程序?
  2. 我的设置是否正确?
  3. 在Windows上使用Bazel调试C ++源代码是否可行?
  4. 我应该使用minGW调试bazel生成的可执行文件还是其他工具或调试器?

版本

  • 操作系统:Windows 10
  • Bazel版本:Build label: 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

enter image description here

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;
}

BUILD 文件

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

然后我在./CPP_TESTS的根目录下运行以下命令:

bazel build //project/main:app

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

enter image description here

一些文件夹包含应用程序的 app.exe 文件,例如在 bazel-bin 目录中,我有这些文件和文件夹。
|-- 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>

如果我调试应用程序,我会得到这个错误。
在开始之前,这是我的任务/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命令enter image description here

错误信息。

enter image description here

但是 app.exe 在那里呢? 在此输入图片描述

嘿,如果你把其中一个标签换成c++标签,你可能会得到更多的观看量。人们往往关注c++这个标签,而不是c++17标签。 - jaggedSpire
3个回答

2

首先,您需要添加一些bazel选项进行调试。手册在这里:进入链接描述

如果您想要调试您的程序,您需要使用以下选项:

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

2
你应该使用位于构建文件夹中的可执行文件 bazel-out/x64_windows-dbg/project/main/app.exe
如果你正在设置断点,你可能还需要使用--strip=never参数。
如果它不起作用,请在launch.json中将配置类型更改为"cppvsdbg"而不是"cppdbg"。
最初的回答:在build文件夹中使用bazel-out/x64_windows-dbg/project/main/app.exe可执行文件。如果需要设置断点,则可能需要--strip=never参数。如果不起作用,请将配置类型更改为"cppvsdbg"。

1
请查看我在相关问题上的答案:https://dev59.com/1LDla4cB1Zd3GeqP-ZaS#54007919 虽然我无法确定您的设置是否正确(我需要了解更多信息,例如您安装了哪些VSCode插件,其他.json文件的内容是什么),但我可以肯定地说,您有两个WORKSPACE文件看起来是错误的。
您应该删除project\WORKSPACE,因为Bazel认为每个具有WORKSPACE文件的目录树都是唯一的工作区。每个工作区在其根目录中只需要一个WORKSPACE文件。如果子目录有自己的WORKSPACE文件,则它是单独的工作区,Bazel不会在“父”工作区中使用它的目标。

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