Home > database >  gdb <error reading variable> for any string object
gdb <error reading variable> for any string object

Time:05-14

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 enter image description here

CodePudding user response:

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.

  • Related