gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:19:20 37 4
gpt4 key购买 nike

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

感谢您的关注。

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

  1. 我应该以哪个 .exe 文件为目标来调试我的应用程序?
  2. 我的设置好吗?
  3. 可以在 Windows 上使用 Bazel 调试 c++ 源代码。?
  4. 我应该使用 minGW 来调试 bazel 生成的可执行文件还是一些其他工具或调试器。?

版本

  • 操作系统:Windows 10
  • Bazel 版本:构建标签: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

应用.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;
}

构建文件

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

然后我在 ./CPP_TESTS 的根目录上运行这个命令

bazel build //project/main:app

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

enter image description here

一些文件夹包含 app.exe 文件,例如在bazel-bin dir 我有这个文件和文件夹

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

如果我调试应用程序,我会收到此错误。

在我开始之前这是我的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"
]
}
]
}

启动.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

错误信息。

enter image description here

但是 app.exe 在那里? enter image description here

最佳答案

首先,您需要添加一些 bazel 选项以进行调试。手册在这里:enter link description here

如果你想调试你的程序,你需要和选项如下:

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

关于c++ - 如何在 Windows 10 操作系统上使用 Bazel 调试 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53821315/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com