gdb<错误读取变量>对于任何字符串对象

发布于 2025-01-28 10:10:19 字数 1412 浏览 3 评论 0原文

让我们在此处以这个非常简单的程序来进行:

// test.cpp
#include <string>
#include <iostream>

using namespace std;

int main() {
    string str = "Hello";
    cout << str << endl;
    return 0;
}

现在,我使用G ++编译器编译此代码:

g++ -g test.cpp -o test.exe

现在,我尝试使用GDB进行调试:

gdb test.exe

在MAIN上设置断点后,然后到达Line return,我尝试查看字符串str中的内容。但是我无法在控制台中打印它。它说&lt;错误读取变量&gt;。不仅在GDB控制台中,即使是使用GDB的Visual Studio Code UI也可以提供相同的输出。

这是我的控制台的屏幕截图:

我到处都在搜索此问题,而我发现的唯一相关问题是 this ,它不起作用。 我还找到了 this 在github vs vs code code repo问题上。我不确定是否有效的修复建议,我找不到他在Windows 11机器上建议的设置。

如何在调试模式下读取字符串中的值?

编辑 @SSBSSA建议我更新GCC之后,我使用MSYS2获取最新的GCC,G ++和GDB版本。现在我有GDB 12.1。现在,它不再显示旧错误,但现在说“转换字符集:无效的参数”。仍在努力使它工作。

Lets take this very simple program here for example:

// test.cpp
#include <string>
#include <iostream>

using namespace std;

int main() {
    string str = "Hello";
    cout << str << endl;
    return 0;
}

now I compile this code with g++ compiler:

g++ -g test.cpp -o test.exe

now I am trying to debug this with gdb:

gdb test.exe

after I set breakpoint on main and then reach the line return 0, I try to see what is in the string str. But I cannot print it in the console. It says <error reading variable>. Not only in gdb console, even Visual Studio Code UI using gdb gives the same output.

Here is a screenshot of my console:
enter image description here

I have searched for this everywhere and the only relevant question I found was this, which did not work.
I also found this post on github VS Code repo issues. The fix suggested there might work I am not sure, I cannot find the setting that he suggested on my Windows 11 machine.

How do I read the value in the string in debug mode?

Edit
After @ssbssa suggested me to update my gcc, I used MSYS2 to get the latest gcc, g++, and gdb versions. Now I have gdb 12.1. Now it is not showing the old error anymore but now it says "Converting character sets: Invalid argument". Still struggling to get it to work.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气称霸 2025-02-04 10:10:19

首先使用GDB这样的程序运行您的程序:

gdb test.exe

现在在命令行界面内运行命令:

set charset UTF-8

这应该暂时解决您的问题。唯一的不便可能是,每次使用GDB在命令提示符上进行调试时,您都需要运行此行。

我注意到您还在使用Visual Studio代码。您可以为VS代码安装C ++扩展程序,在那里您可以在启动中添加命令设置charset UTF-8,如图所示,如图所示。 /67991808/vscode-debugger-having-having-having-with-character-insoding“> there 。这样,您可以更快地调试应用程序。

First run your program with gdb like so:

gdb test.exe

Now inside the command line interface run the command:

set charset UTF-8

This should temporarily fix your problem. The only inconvenience might be that you need to run this line every time you debug on your command prompt with GDB.

I noticed that you are also using Visual Studio Code. You can install C++ extensions for VS Code and there you can add the command set charset UTF-8 in the launch.json setupCommands array as shown here. This way you can debug your application faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文