1

I write small console programs in C++. I work on the 'Windows 10' operating system and through WSL I work on the 'Ubuntu' operating system. I output UTF-8 encoded text to the console (English and Cyrillic letters, Chinese hieroglyphs, emojis) using narrow char characters via std::cout.

When debugging programs in the 'VS Code' editor and the 'Visual Studio 2022' IDE, I need to switch the code page to UTF-8 in the debugging console beforehand. (When working with the final versions of programs in 'Windows 10', I switch the code page manually in the shell, but in 'Ubuntu' this is not required, there is UTF-8 encoding by default.)

I have a problem: in the 'VS Code' editor and in the 'Visual Studio 2022' IDE, it is difficult for me to find how to pass preliminary commands to the debugging console.

Important! I know that this can be done programmatically using Windows API functions, but I want to keep the source code cross-platform and not clutter.

I've also seen people prescribe preliminary commands for the shell 'cmd.exe' in the Windows registry, but I don't want to make changes to the registry.

I found an acceptable way in the 'VS Code' editor (I have a Microsoft extension 'ms-vscode.cpptools' installed to work with C++). In the launch.json file I have specified an option

"console": "integratedTerminal"

Since PowerShell is used in the integrated terminal of the 'VS Code' editor, I was able to use the profile to transmit preliminary commands:

PS C:\> $profile
C:\Users\Илья\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

To a file Microsoft.PowerShell_profile.ps1 I wrote the following:

[System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8

After that, everything turned out the way I needed it: enter image description here

My questions: How do I configure debugging of console programs in C++ in the 'Visual Studio 2022' IDE so that the text in UTF-8 encoding is reflected correctly in the debugging console? (Without changing the registry or changing the source code.) Is it possible to change the debugging console in the 'Visual Studio 2022' IDE with 'cmd.exe' on PowerShell? (Then I would be able to use a profile.) Is it possible to pass preliminary commands or parameters to the debugging console of 'Visual Studio 2022' IDE?

Ilya Chalov
  • 149
  • 2
  • 9
  • Duplicates: [1](https://stackoverflow.com/questions/1371012/how-do-i-print-utf-8-from-c-console-application-on-windows), [2](https://stackoverflow.com/questions/45575863/how-to-print-utf-8-strings-to-stdcout-on-windows), [3](https://stackoverflow.com/questions/50179492/output-utf8-in-console-with-visual-studio-wide-stream), [4](https://stackoverflow.com/questions/10882277/properly-print-utf8-characters-in-windows-console), etc. – Hans Passant Jul 10 '23 at 20:52
  • @HansPassant In the links you suggested, they talk about the correct output of UTF-8 encoded text to the console on Windows systems. I don't have a problem with it, I know how to do it. – Ilya Chalov Jul 11 '23 at 12:59
  • @HansPassant You were probably misled by the title of my question. I cannot formulate my question briefly enough so that all the conditions fit into the title of the question. Is there anything in your links about cross-platform, about the Visual Studio debugging console? They offer `#include ` everywhere, it doesn't suit me. – Ilya Chalov Jul 11 '23 at 13:07
  • By the way, this only works in VSCode. It doesn't work if run the exe directly. – Minxin Yu - MSFT Aug 01 '23 at 07:56
  • @MinxinYu-MSFT What do you mean `It doesn't work if run the exe directly`? If I work with the final version of the program (not debugging) in PowerShell, the code page switches in the profile. In the shell `cmd.exe` I switch the code page manually using the `chcp 65001` command. In Ubuntu, switching the code page is not required, there is UTF-8 by default. Running the executable file directly works for me. – Ilya Chalov Aug 01 '23 at 11:53

1 Answers1

1

Thanks for the clarification. I thought you were looking for the fully automatic way. If manually input chcp 65001 is allowed, Visual Studio can achieve what you want.

There is an extension: Microsoft Child Process Debugging Power Tool 2022 You need to install and enable the extension. Tool bar :Debug-> Other debug targets-> Child process: enter image description here

And, add /utf-8 flag in your project -> Properties -> C/C++ -> Command Line.

Open a CMD process. Attach the CMD using Debug-> Attach to Process. Input chcp 65001 first. Then run your exe in CMD and the break points will be hit, the text output correctly.

It also works for Win11's terminal->command prompt, but not able to debug termianl->powershell

enter image description here enter image description here

Minxin Yu - MSFT
  • 2,234
  • 1
  • 3
  • 14
  • Yes, thank you, this method works! By the way, my source code is stored in UTF-8 encoding **without BOM**. In this case, you don't need to add the `/utf-8` key (the environment somehow determines by itself that the `execution-charset` should be UTF-8, even though it contradicts the documentation). – Ilya Chalov Aug 03 '23 at 15:23