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.
Versions
|-- 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
#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"]
)
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"
]
}
]
}
{
// 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
But the app.exe is there?
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" ...
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With