gdb <error reading variable> for any string object - c++

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.

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

Notepad++ NppExec console warning, need explanation "C++"

I've tried using Notepad++ to code c++ and followed a few tutorials on youtube, here's what I did:
-Installed gcc/g++ compiler using mingw64
-Installed NppExec plugin on N++
-Typed in the following compilier script and saved as C++:
NPP_SAVE cd $(CURRENT_DIRECTORY) g++ $(FILE_NAME) cmd /c $(CURRENT_DIRECTORY)\program.exe
Anyways whenever compiling a program, for example a simple program
#include <iostream>
using namespace std;
int main(){
cout << "Online\n";
system("pause"); //So that cmd doesn't disappear immeadiately on running.
return 0;
}
The console displays the following warning:
"C:\Users\pc\Desktop\Courses\Projects\C\program.exe' is not recognized as an internal or external command, operable program or batch file."
My question is, When I run the program on cmd, it runs perfectly but the error displayed during linking says that the folder does not exist in %PATH%
Any explanation?
Thank you!
Ok so, what I basically did was change the script,
cmd /c $(CURRENT_DIRECTORY)\program.exe
To be later
cmd /c $(CURRENT_DIRECTORY)\a.exe
the console worked fine and even received input
Here is a link to a similar problem:
How to compile/execute C++ code from within Notepad++

Command line cpp compile isn't working properly

First of all, I made a simple program to test if compiling was working properly, it is as follows:
#include <iostream>
int main() {
std::cout << "Oi!";
}
Quite simple indeed, should compile properly, right? Wrong.
I am using MinGW for Windows 10, just to let you know.
I tried compiling it using "cpp <file name>.cpp -o a.exe" and was, first time, going through with the compile, but once I tried to execute the file it would send me this:
./a.exe: line 14: namespace: command not found
./a.exe: line 20: syntax error near unexpected token `('
./a.exe: line 20: ` typedef decltype(nullptr) nullptr_t;'
Clearly something was wrong, so I searched for a solution, found out someone had a similar problem and doing a reinstall solved it, so I went to the MinGW installation manager, noticed the C++ compiler library wasn't installed and installed it. It felt I was doing alright.
I also noticed that they were using g++ instead of cpp and tried that instead, it worked properly this time, but I would like to note that I have "git bash" installed, so I assume it used "git bash" 's command and not MinGW's.
So I opened cmd and tried using cpp, once I tried executing the program cmd said that the software wasn't compatible with the version of windows being executed, that doesn't sound right. I did a verbose compile with cpp and noticed this oddity:
#include <...> search starts here:
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include/c++
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include/c++/mingw32
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include/c++/backward
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include-fixed
End of search list.
That doesn't follow Windows's file path standard...
Does it have anything to do with the problems I am facing? I just want things to compile! ;w;
As mentioned above, you need to use g++ to compile your code. Try the command g++ --version If you get any result that doesn't include a version number you need to install g++. Here is a guide that may be helpful.
https://techsupportwhale.com/install-gcc-compiler-on-windows/

GDB exited unexpectedly when debugging with vscode

I'm using vscode on Windows, and use mingw-w64 as compiler. my test code are as follows:
vector<string> words = {"SEND", "MORE"};
string result = "MONEY";
when I debug it, I can print 'words' and 'result' object correctly, but when I try to use subscript, something wrong occured:
print words[0] // gdb: Could not find operator[].
and more seriously, when I try print string's subscript, gdb just crashed:
print result[0]
// ERROR: GDB exited unexpectedly with exit code -1073741819 (0xC0000005). Debugging will now abort.
Those errors occurred samely whether debug with vscode or use gdb in cmd shell manually.
However, when I debug on linux platform using gdb, it works perfectly. That makes me confusing. I can't find if there's any bug in mingw, or maybe gdb doesn't support windows platform well. My g++ and gdb version are 8.1.0. I have tried some older version but all those have same problems. Is there any good solution, or shall I just give it up?
Clean your path environment and PY_HOME.

Printing a double to std::cout results in Segmentation fault (C++)

I am new to C++ and want to print out a double value. It is not that I actually need to print that value, I just want to know what is going wrong here.
This is my code (HelloWorld.cpp):
#include <iostream>
int main() {
double i = 5.5;
std::cout << i << std::endl;
return 0;
}
Executing this with the debugger attached results in the following error:
Thread 1 hit Breakpoint 1, main () at src/HelloWorld.cpp:4
4 double i = 5.5;
Thread 1 received signal SIGSEGV, Segmentation fault.
0x00000000929ba260 in ?? ()
When I put a breakpoint in there, creating and assigning the variable is no problem. The error only occurs once the program is supposed to print that value. Executing the exe without the debugger results in no output at all. The same happens when I replace the double with a long double or float. Printing anything else works fine (Strings, char, int, short, etc.).
I am using Visual Studio Code and MinGW (x86_64-8.1.0-posix-seh-rt_v6-rev0). VS Code is using following files for compilation / debugging:
c_cpp_properties.json
launch.json
tasks.json
And here you can see the complete output, in case that helps.
Any Idea what I am doing wrong here? Thank you.
EDIT:
When compiling manually using g++ -g .\src\HelloWorld.cpp -std=c++11 -o HelloWorld.exe (or just g++ .\src\HelloWorld.cpp -o HelloWorld.exe) and running that from the console, the same happens (no output).
I installed MinGW from here using the following settings:
Version: 8.1.0
Architecture: x86_64
Threads: posix
Exception: seh
Build revision: 0
EDIT 2:
Found the problem. There was an old version of gcc lurking in my PATH (maybe from Visual Studio or MSSQL Server?). I just move the current gcc to the top of PATH and now it's working fine. Thank you all for your help!
As many pointed out, this should usually work. The problem was with my setup: I had an old version of gcc somewhere in my PATH variable (version 4.1). Moving the path of the newer version to the beginning of PATH resolves the issue. Thank you all for helping.
To check weather the same happens to you you can do the following: execute g++ --version in your project directory. Compare this with the output of g++.exe --version when you are in the directory where gcc is installed (for me, this was C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin).

how to compile c++ program in mac terminal

i wrote a program in my mac using sublime text as the plateform..
#include<iostream>
using namespace std;
int main()
{
cout<<"HELLOW WORLD";
return 0;
}
this was my program..
i saved it in desktop as hellow.cpp
while compiling on mac terminal as g++ hellow.cpp, i found an error
adarshs-MacBook-Air:Desktop adarshak$ g++ hellow.cpp
xcrun: error: invalid active developer path
(/Library/Developer/CommandLineTools), missing xcrun at:
/Library/Developer/CommandLineTools/usr/bin/xcrun
anyone plese help me to find out the error
It sounds like you don't have the command line developer tools installed. Run this command from Terminal once:
xcode-select --install
This will bring up the download & installation UI. Follow this through to the end. (It may take a while depending on the speed of your internet connection.)
From then on compiling should work.