vscode使用g++编译代码中含有中文字符的程序运行后乱码
环境:
系统编码:不知道
mingw版本:https://nuwen.net/mingw.html 的 mingw-19.0.exe (60.5 MB)
vscode版本:VSCodeUserSetup-x64-1.93.1
问题出现:
task.json里未配置任何编码转换:
——乱码
task.json 配置 "-fexec-charset=GBK”
——报错:cc1plus.exe: error: no iconv implementation, cannot convert from UTF-8 to GBK
百度了一圈,网友说gcc未链接libiconv
——太懒,算了
还有网友说,使用msys2里面的g++.exe
——尝试过替换g++.exe(之前装过https://nuwen.net/mingw.html 的 mingw-19.0.exe (60.5 MB),没删)、将task.json的g++.exe的路径改到msys2搞出来的mingw里面的g++.exe、将task.json与launch.json里面的g++.exe替换、修改环境变量等
——乱搞一通没解决问题也没搞明白,算了
问题解决(暂时):
1.将vscode(要编译的cpp文件)的编码(save with encoding)改为Simplified Chinese(GBK)
2.在task.json 配置中添加上
——解决
后续:
不在task.json中额外添加转码配置,只修改cpp文件的编码为GBK也行
——不知道我做的其他乱七八糟操作对这个有没有影响

参考文献:
主要:
https://blog.csdn.net/hackzzh/article/details/104505454/
次要:
https://blog.51cto.com/mlxia/6345831
http://blog.chinaunix.net/uid-1835494-id-2831815.html
vscode环境配置参考:
1.mingw及vscode下载与配置:
https://blog.csdn.net/weixin_41180859/article/details/141159350
2.json生成:
https://blog.csdn.net/qq_43556680/article/details/142352666
当前json留档:
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "task g++", "command": "MinGW\\bin\\g++.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe", "-finput-charset=UTF-8",//输入编译器文本编码 默认为UTF-8 "-fexec-charset=UTF-8"//编译器输出文本编码 自行选择 ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "presentation": { "panel": "shared" } } ] }
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) 启动", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": true, //改为true "preLaunchTask": "task g++", //新增项 "MIMode": "gdb", "miDebuggerPath": "\\bin\\gdb.exe", //MinGW位置 "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
关键部分截图留档:

