Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug c++ code using Bazel on Windows 10 OS

Recently I start to play with the Bazel and I face a problem with debugging the app, I can debug with g++ but I can't debug the Bazel generated .exe file.

Thank you for looking on this.

Also, I build the source code with the Bazel and VsCode tasks.

  1. What .exe file should I target to debug my application?
  2. Is my setup Ok.?
  3. It is possible to debug a c++ source code using Bazel on Windows.?
  4. Should I use minGW for debugin a bazel generated executable or some othertools or debuggers.?

Versions

  • OS: Windows 10
  • Bazel version: Build label: 0.20.0

I have this project structure:

|-- 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 file

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

And then I run this command on the root of the ./CPP_TESTS

bazel build //project/main:app

this command will generate some files and folders

enter image description here

Some of the folders contain the app.exe file for example inside the bazel-bin dir I have this files and folder

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

so If I run the app.exe inside the

|-- bazel-bin
    |-- project
    |   |-- main
    |       |-- app.exe   <=========

I get this output

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>

if I debug the app I get this error.

before I start this is my 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
                }
            ]
        }
    ]
}

So I run the bazel build //project/main:app command enter image description here

Error message.

enter image description here

But the app.exe is there? enter image description here

like image 472
George C. Avatar asked Dec 17 '18 18:12

George C.


3 Answers

First, you need to add some bazel options to debug. Manual is in here:enter link description here

If you want to debug your program, you need and options such as:

bazel build --copt="-g" --strip="never" ... 
like image 152
changeset Avatar answered Oct 21 '22 10:10

changeset


You should use the executable located in the build folder bazel-out/x64_windows-dbg/project/main/app.exe.

If you are setting breakpoints, you may need the --strip=never argument as well.

If it does not work, try "cppvsdbg" rather than "cppdbg" as configuration type in your launch.json.

like image 44
TheFabbius Avatar answered Oct 21 '22 09:10

TheFabbius


Please see my answer on the related question: https://stackoverflow.com/a/54007919/7778502

While I can't tell whether your setup is correct (I'd need to know more, such as what VSCode plugins you have installed, what's the content of the other .json files), I can say for sure that it looks wrong that you have two WORKSPACE files.

You should remove project\WORKSPACE, because Bazel considers every directory tree with a WORKSPACE file to be a unique workspace. Every workspace needs just one WORKSPACE file in its root directory. If a subdirectory has its own WORKSPACE file, then it's a separate workspace, and Bazel won't use targets from it in the "parent" workspace.

like image 28
László Avatar answered Oct 21 '22 09:10

László