问题
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.
- What .exe file should I target to debug my application?
- Is my setup Ok.?
- It is possible to debug a c++ source code using Bazel on Windows.?
- 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
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
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
Error message.
But the app.exe is there?
回答1:
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.
回答2:
You should use the executable placed inside the 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 your configuration type inside the launch.json file.
来源:https://stackoverflow.com/questions/53821315/how-to-debug-c-code-using-bazel-on-windows-10-os