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?

huangapple go评论75阅读模式
英文:

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?

问题

我在C++中编写小型控制台程序。我在“Windows 10”操作系统上工作,并通过WSL在“Ubuntu”操作系统上工作。我使用窄字符char通过std::cout将UTF-8编码的文本输出到控制台(包括英文字母、西里尔字母、中文汉字和表情符号)。

在“VS Code”编辑器和“Visual Studio 2022”集成开发环境中调试程序时,我需要事先将代码页切换到UTF-8以在调试控制台中正确显示文本。(在“Windows 10”上处理程序的最终版本时,我会在Shell中手动切换代码页,但在“Ubuntu”上则不需要,因为默认使用UTF-8编码。)

我遇到了一个问题:在“VS Code”编辑器和“Visual Studio 2022”IDE中,我很难找到如何传递预先命令给调试控制台。

**重要!**我知道可以使用Windows API函数来以编程方式完成此操作,但我希望保持源代码跨平台,并且不添加多余的复杂性。

我也看到一些人在Windows注册表中为Shell“cmd.exe”编写了预先命令,但我不想修改注册表。

我在“VS Code”编辑器中找到了一种可接受的方式(我安装了Microsoft扩展“ms-vscode.cpptools”以处理C++)。在launch.json文件中,我指定了一个选项

"console": "integratedTerminal"

由于“VS Code”编辑器的集成终端使用PowerShell,我可以使用profile来传递预先命令:

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

Microsoft.PowerShell_profile.ps1文件中,我写入了以下内容:

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

之后,一切都按我所需的方式运行了:
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?

我的问题:如何配置在“Visual Studio 2022”IDE中调试C++控制台程序,以便UTF-8编码的文本在调试控制台中正确显示?(不更改注册表或源代码。)是否可以将“Visual Studio 2022”IDE中的调试控制台更改为“cmd.exe”或PowerShell?(这样我就可以使用配置文件。)是否可以传递预先命令或参数给“Visual Studio 2022”IDE的调试控制台?

英文:

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:
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?

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?

答案1

得分: 1

感谢您的澄清。我以为您正在寻找全自动的方法。如果允许手动输入 chcp 65001,则可以使用Visual Studio实现您想要的效果。

有一个扩展程序:Microsoft Child Process Debugging Power Tool 2022,您需要安装并启用该扩展。工具栏:调试->其他调试目标->子进程
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?

然后,在您的项目->属性->C/C++->命令行中添加 /utf-8 标志。

打开一个CMD进程,使用调试->附加到进程附加CMD。首先输入 chcp 65001,然后在CMD中运行您的exe,断点将被触发,文本输出将正确显示。

这也适用于Windows 11 的终端->命令提示符,但无法调试终端->PowerShell。
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?
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?

英文:

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:
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?

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

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?
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?

huangapple
  • 本文由 发表于 2023年7月10日 23:19:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定