gdb<错误读取变量>对于任何字符串对象
让我们在此处以这个非常简单的程序来进行:
// 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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先使用GDB这样的程序运行您的程序:
现在在命令行界面内运行命令:
这应该暂时解决您的问题。唯一的不便可能是,每次使用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:
Now inside the command line interface run the command:
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.