std::codecvt_utf8 facet erroneous output with MSYS2 Bash and CMD - c++

Reproduce: codecvt.cpp
#include <iostream>
#include <locale>
#include <string>
#include <codecvt>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
locale utf8(locale(), new codecvt_utf8<wchar_t>);
wcout.imbue(utf8);
wstring str(L"Test String OÖ UÜ SŞ iİ ıI");
wcout << str << '\n';
}
Compile with GCC 6.3.0: g++ codecvt.cpp -o codecvt -O3
Windows CMD: codecvt.exe
Output: Test String O├û U├£ S┼Ş i─░ ─▒I
MSYS2 Bash: $ ./codecvt.exe
Output: Test String O├û U├£ S┼Ş i─░ ─▒I (same)
Ubuntu subsystem for Windows 10 Bash:
Compile with GCC 5.4.0: g++ codecvt.cpp -o codecvt -O3
Execute: ./codecvt
Output: Test String OÖ UÜ SŞ iİ ıI
I've been struggling with wide character problems in c++ lately. I ran into this solution but code only seems to be working in linux environment. (I am a c++ beginner (ish))
I also tried some other solutions:
First solution:
setlocale(LC_ALL, "");
Windows CMD outputs as desired, no problems. But input is erroneous.
MSYS2 Bash does not outputs as desired, but input is correct.
Ubuntu subsystem for Windows 10 Bash is correct with both input and output.
Second solution:
locale::global(locale("C.UTF-8"));
Both Windows CMD and MSYS2 Bash crashes at start of program, MSYS2 Bash also reported:
what(): locale::facet::_S_create_c_locale name not valid
Ubuntu subsystem Bash is still cool, works for input/output correcly.
I am guessing this is a problem related to Windows?

Related

gdb <error reading variable> for any string object

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.

C++ error when using std::vector on mingw and powershell

I have come across some strage behaviour when executing a simple program that uses std::vector with powershell
#include <vector>
#include <iostream>
int main() {
auto v = std::vector<int>{};
v.push_back(0);
std::cout << "Hello, World!\n";
return 0;
}
g++ -v returns gcc version 11.2.0 (Rev10, Built by MSYS2 project)
I then compile the program with g++ main.cpp -o main.exe and i get an output executable.
When I run .\main.exe with powershell I get no console output but when i run the same executable with git bash I get Hello, World! printed to the console.
Ive tested both Powershell 7 and Windows PowerShell.
When I remove the both lines that have to do wiht std::vector it works in both shells.
When I run the executbale from 'cmd' I get the following error message

C++, Nothing in the output [duplicate]

This question already has answers here:
Why do I get no output for very simple Hello World program?
(7 answers)
Closed 6 months ago.
I am new to C++ and coded my first main.cpp but I got an error, not exactly an error, it is a logical error, I guess because I wrote the following code:
#include <iostream> // including the iostream
using namespace std; // using the std namespace
int main() { // starting the main function
cout << "Hello World!" << endl; // My favorite line of code in all the of languages
return 0; // returning 0 to stop the function execution
}
In the terminal I expected
C:\Users\DELL\Desktop> g++ main.cpp
Hello World!
C:\Users\DELL\Desktop>
But it is just showing:
C:\Users\DELL\Desktop> g++ main.cpp
C:\Users\DELL\Desktop>
No output is coming, but when I try in Code::Blocks, it is working just fine! In vscode terminal, the same problem but when I run the file, it is working again! Why is it not working? I tried it in Command Prompt, installed the Windows Terminal, and tried it in that also (keeping the terminal as Command Prompt, cause I don't know what PowerShell is and how to use it) but any method is not working.
Please tell me what to do, I am new to c++ and know only some things amount it.
The command g++ main.cpp creates the file a.out or a.exe. After that file is created you need to run it by the command ./a.out on Linux and Mac or a.exe on Windows.
It's pretty simple, after g++ "file.cpp", you get an output file in your current working directory, which is the executable. C++ is compiled, not interpreted.
That output file is the executable, which by default will be "a.out", with gcc/g++. You can use the option "-o" to specify the output directory.
g++ main.cpp will compile the file and produce a.exe, you just need to type a.exe in the terminal and it will print Hello World!.

C++ compiles but gives error when executed

I am new to Linux Ubuntu 11.10 and have basic C++exposure.
I installed the g++ by
sudo apt-get install build-essential
and created a directory cpp in my home directory. I then wrote a program hello.cpp in my cpp directory
#include <iostream>
using namespace std;
int main() {
cout << "Hello !" ; return 0;
}
and compiled using
g++ -W hello.cpp -o hello
The program compiles without any errors/warnings. When I try to execute the file
./hello.cpp
I get error messages:
line 3: using: command not found
line 6: syntax error near unexpected token `('
line 6: `int main() {'
I tried looking at a lot of posts but could not resolve this. I have MS VisualStudio on Windows, but I would rather learn C++ on Ubuntu. Thanks in advance.
I think that the problem is that you're trying to execute the .cpp source file rather than the generated executable. Try running ./hello instead of ./hello.cpp, since hello is the actual executable. The errors you're currently getting are caused by the shell interpreter choking on C++ syntax, since it's trying to run it as a shell script.
Hope this helps!

c++ using system command problem

hi every body i have a strange problem i write a code in c++ complied it successfully and run it successfully. i compiled with following command
g++ 1.c -o abc
to run program i use ./abc
now my problem is that i write a another code in c++ like
#include <fstream>
#include<iostream>
using namespace std;
int main()
{
ofstream SaveFile("/home/hadoop/project/hadoop-0.20.0/conf/core-site2.xml");
SaveFile <<"<configuration>";
SaveFile<<endl;
SaveFile<<"<property>";
SaveFile<<endl;
savefile.close();
return 0;
}
now i want to run abc in this code how to do this ?
how to use or run abc in this file?
how to use ./abc in this program ?
Actually, your question title ("... using system ...") says it all. Use:
system ("./abc");
to run the ./abc program.
There are other ways to run programs from within a program (which usually depend on platform-specific features) but this is the most portable.
A full sample program, testprog.cpp, to show what I mean:
#include <cstdlib>
int main (void) {
std::system ("ls -ald m*");
return 0;
}
Compiling this with:
g++ -Wall -Wextra -pedantic -o testprog testprog.cpp
and running the resultant executable testprog, this outputs (on my Ubuntu 10.04 box):
drwxr-xr-x 2 pax pax 4096 2010-12-14 09:33 myfolder
In other words, it runs the ls -ald m* command from within the program itself.