我已经使用 Bazel 构建系统在 Visual Studio Code 中启动了一个 C++ 项目。但是,当使用 Bazel 构建二进制文件时,调试在 IDE 中不起作用。
我可以调试使用 clang++ -g main.cpp -o sample
构建的应用程序。
我的设置: 操作系统:MacOS,Bazel:发布 0.17.2-homebrew,VS 代码:1.27.2
这是由 Bazel 的编译方式造成的。是否有任何解决方法可以使调试工作?
这是一个最小的例子。只需在编辑器中放置断点,运行调试,并观察断点未命中:
├── .vscode
│ ├── launch.json
│ └── tasks.json
├── BUILD
├── WORKSPACE
├── main.cpp
└── sample.code-workspace
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/bazel-bin/sample",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "bazel build",
}
]
}
.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "bazel build",
"type": "shell",
"command": "bazel",
"args": [
"build",
"--compilation_mode=dbg",
"sample"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
构建
cc_binary(
name = "sample",
srcs = ["main.cpp"],
visibility = ["//main:__pkg__"],
)
main.cpp
#include <iostream>
int main() {
std::cout << "tests" << std::endl;
return 0;
}
sample.code-workspace
{
"folders": [ {"path": "."} ],
"settings": {}
}
更新。 1
尝试调试 bazel 构建的可执行文件并直接使用 lldb 手动构建:
Bazel 构建的二进制文件 bazel build --compilation_mode=dbg 示例
lldb bazel-bin/sample
(lldb) breakpoint set -n main
Breakpoint 1: 13 locations.
(lldb) r
Process 24391 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x00000001000021b0 sample`main
sample`main:
-> 0x1000021b0 <+0>: pushq %rbp
0x1000021b1 <+1>: movq %rsp, %rbp
0x1000021b4 <+4>: subq $0x20, %rsp
0x1000021b8 <+8>: movq 0xe51(%rip), %rdi ; (void *)0x00007fff9c93a660: std::__1::cout
Target 0: (sample) stopped.
手动构建的二进制文件 clang++ -g main.cpp -o sample
lldb sample
(lldb) breakpoint set -n main
Breakpoint 1: where = sample`main + 29 at main.cpp:4, address = 0x0000000100000f9d
(lldb) r
Process 24410 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000100000f9d sample`main at main.cpp:4
1 #include <iostream>
2
3 int main() {
-> 4 std::cout << "tests" << std::endl;
5 return 0;
6 }
Target 0: (sample) stopped.
在可执行文件中查找“调试说明”后(感谢 this answer)
nm -pa bazel-bin/sample | grep OSO
000000005bb7c96b - 03 0001 OSO /private/var/tmp/_bazel_teivaz/1e731ee5ae5a3ce6177976984318ec76/bazel-sandbox/1688134534644271312/execroot/__main__/bazel-out/darwin-dbg/bin/_objs/sample/main.o
我发现这些路径指向 Bazel 用来构建的沙箱环境。并且此路径不再可访问。
这可能是由 Bazel 中的一个已知问题引起的 #5545
更新。 2
将 Bazel 更新到版本 0.17.2 后(问题 #5545 已修复),此问题仍然存在。
更新。 3
最后证明是Bazel的问题。我在这里创建了一个问题 #6327
最佳答案
(促进更新回答):看起来像一个已知问题:#6327
关于c++ - 在 MacOS 上使用 Bazel 构建时调试 C++ 代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52672101/