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

  • @ChandrachurMukherjee [This](https://stackoverflow.com/a/6778040/14414944) didn't work? – lmonninger May 13 '22 at 04:07
  • no, screenshot shows i tried it. same result. – Chandrachur Mukherjee May 13 '22 at 04:30
  • Since your gdb version is from 2013, I suggest you update. – ssbssa May 13 '22 at 05:48
  • What is your GCC version? Do you build 64 bit executable? – ks1322 May 13 '22 at 10:21
  • gcc version was previously 7.6.1 and I updated it to the latest version after @ssbssa suggested so. Now I am having a different problem. I am going to edit my question to update my current problem. Yes I am running 64 bit g++ on 64 bit machine, so I think the executable will also be 64bit. – Chandrachur Mukherjee May 13 '22 at 10:58
  • According to [this question](https://stackoverflow.com/q/67991808/1983398), the character set problem is related to the reading variable problem. Maybe try `set charset UTF-8` as the answer states. – ssbssa May 13 '22 at 12:21

1 Answers1

1

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.

AllLuckBased
  • 157
  • 1
  • 14