I compile this code with Cyrillic characters:
setlocale(LC_ALL, "");
string mystr = "русские символы";
cout << mystr << endl;
And a problem arises with different compilers and different operating systems.
The most successful case with the MSVC compiler when the characters are displayed correctly in the console, no matter what the file encoding is.
The problem starts with the G++ and Clang compilers. In Windows case, they require the file encoding to be Windows 1251 (i.e. my system’s encoding) to compile and display the characters correctly. If, for example, I set the .cpp file encoding to UTF-8, then the compiled .exe file will output to the console:
"S?S?S?S?RєRёRch S?RёR?R?R?R>S<"
instead of
"русские символы
The situation is similar with G++ and Clang on Linux, only in reverse. Linux encoding is UTF-8. If the file.txt encoding is different, program will display:
??????? ???????
Do I understand correctly that the output .exe file will display characters depending on the source encoding type of the .cpp file? Does the MSVS compiler simply automatically change the encoding?
What is this connected with? Why does the MSVC compiler work correctly, but others don’t? And is it possible to somehow fix this and compile correctly using G++ and Clang?
Danek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.