解决在VS Code中写C++可能遇到的中文乱码问题

前言:我的开发环境

宇宙安全声明:我只是个刚学C++没多久的小白,发这个文章是记录并分享我处理中文错误的经验,可能有很多处理得不好或者描述有错误的地方,请大佬轻喷!不妨在评论区留言让我学习一下各位大佬的经验!()

我使用的环境是Windows+VS Code+G++(Mingw),并在VS Code中安装了Code Runner插件,所以以下内容均以此环境为基础

在VS Code中开发C++,新手几乎一定会遇到中文乱码问题(反正我身边很多人都是这样),最常见的是中文输出乱码,还有中文路径报错问题

中文输出乱码

当你配置好了环境,并在VS Code中新建了第一个cpp文件,并写下了下面的代码:

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello World" << endl;
    return 0;
}

然后一运行,多半会出现乱码

这是由于在VS Code当中,新建文件默认使用的编码是UTF-8,但是终端使用的却是GBK,编码不一致就自然会出现乱码。

解决办法有很多

在系统设置中勾选使用Beta版UTF-8(推荐)

这是我现在使用的,也是我目前找到的最简单最好的办法

按下Win+I打开设置

依次点击:时间和语言>语言和区域

点击:管理语言设置,更改系统区域设置

勾选下面的:Beta 版:使用 Unicode UTF-8 提供全球语言支持

点击完成,重启电脑。

现在重新运行,应该就会正常了,通过这个设置,系统终端就会默认使用UTF-8(可以通过chcp命令查看是不是65001)

虽据说勾选这个Beta版可能会导致某些软件乱码或者无法正常运行,但我还没遇到过这个情况,都好好的

修改注册表,每次都自动运行chcp 65001(推荐)

按下Win+R,输入regedit,打开注册表

输入这个路径:

计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor

在Command Processor中右键新建一个字符串值,重命名为autorun,值为chcp 65001

这样就可以实现每次运行终端时候都会自动运行chcp 65001这个命令,把终端的编码改成UTF-8

也可以把值改为chcp 65001 > nul,这样就不会显示Active code page: 65001的提示了

直接使用GBK编码写代码(不推荐)

这个是我之前一直使用的,很简单粗暴的办法,但是我不推荐这个办法,我现在也不用了,尽量还是使用UTF-8去写代码,用GBK总感觉很不方便

在VS Code中,打开设置,在搜索框中搜索encoding

图片[1]-解决在VS Code中写C++可能遇到的中文乱码问题-Abyss的小屋

把这个UTF-8改成GBK

之后,你在VS Code新建的文件就会默认使用GBK了(只对新建的文件有效,之前的文件仍然需要手动更改)

手动更改的办法也很简单

图片[2]-解决在VS Code中写C++可能遇到的中文乱码问题-Abyss的小屋

点击这个UTF-8

图片[3]-解决在VS Code中写C++可能遇到的中文乱码问题-Abyss的小屋

点击通过编码重新打开,选择GBK即可

中文路径报错

刚刚提到我之前一直是使用GBK编码去写C++的,昨天改成了UTF-8去写,但这一改,就让我的运行也出问题了

我暂时也不知道为啥,之前不需要进行什么多余的设置,也可以正常使用中文路径

但改了之后,g++ cpp文件路径 -o 输出文件路径中,输出文件路径如果含有中文就会报错

折腾了一天之后(被Deepseek的愚蠢折磨一天之后),总算找到了解决办法:

在Code Runner的设置中,在这里点击在settings.json中编辑

图片[4]-解决在VS Code中写C++可能遇到的中文乱码问题-Abyss的小屋

把里面的内容全部删掉,输入:

{
    "code-runner.executorMap": {
        "javascript": "node",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd /d \"$dir\" && (if not exist output mkdir output) && gcc \"$fileName\" -o \"output\\program.exe\" && \"output\\program.exe\"",
        "cpp": "cd /d \"$dir\" && (if not exist output mkdir output) && g++ \"$fileName\" -o \"output\\program.exe\" && \"output\\program.exe\"",
        "zig": "zig run",
        "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "php": "php",
        "python": "python -u",
        "perl": "perl",
        "perl6": "perl6",
        "ruby": "ruby",
        "go": "go run",
        "lua": "lua",
        "groovy": "groovy",
        "powershell": "powershell -ExecutionPolicy ByPass -File",
        "bat": "cmd /c",
        "shellscript": "bash",
        "fsharp": "fsi",
        "csharp": "scriptcs",
        "vbscript": "cscript //Nologo",
        "typescript": "ts-node",
        "coffeescript": "coffee",
        "scala": "scala",
        "swift": "swift",
        "julia": "julia",
        "crystal": "crystal",
        "ocaml": "ocaml",
        "r": "Rscript",
        "applescript": "osascript",
        "clojure": "lein exec",
        "haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
        "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
        "racket": "racket",
        "scheme": "csi -script",
        "ahk": "autohotkey",
        "autoit": "autoit3",
        "dart": "dart",
        "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
        "haskell": "runghc",
        "nim": "nim compile --verbosity:0 --hints:off --run",
        "lisp": "sbcl --script",
        "kit": "kitc --run",
        "v": "v run",
        "sass": "sass --style expanded",
        "scss": "scss --style expanded",
        "less": "cd $dir && lessc $fileName $fileNameWithoutExt.css",
        "FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "sml": "cd $dir && sml $fileName",
        "mojo": "mojo run",
        "erlang": "escript",
        "spwn": "spwn build",
        "pkl": "cd $dir && pkl eval -f yaml $fileName -o $fileNameWithoutExt.yaml",
        "gleam": "gleam run -m $fileNameWithoutExt"
    },
    "editor.fontFamily": "JetBrains Mono, Consolas, 'Courier New', monospace",
    "editor.fontLigatures": false,
    "files.autoSave": "afterDelay",
    "code-runner.runInTerminal": true,
    "code-runner.fileDirectoryAsCwd": true,
    "code-runner.clearPreviousOutput": true,
    "code-runner.saveAllFilesBeforeRun": true,
    "code-runner.preserveFocus": false,
    "terminal.integrated.defaultProfile.windows": "Command Prompt"
}

但需要注意,里面有些内容需要根据自己的实际情况去改,比如autoSave,FontFamily之类的

原理其实也很简单粗暴,就是每次编译生成的exe文件都写死为program.exe

(if not exist output mkdir output)的意思就是,如果当前目录下不存在output文件夹,就自动创建(少了这句话会报错)

需要注意的是,这段设置是完全禁用 VS Code 的默认构建任务,只用 Code Runner的(不知道啊,反正Deepseek是这么说的)

需要使用Ctrl+Alt+N去运行(而不是原先用的F6)

如果习惯了F6,那也可以像我一样改一下快捷键:

  • 按 Ctrl + Shift + P
  • 输入 Preferences: Open Keyboard Shortcuts (JSON)
  • 选择这个命令

添加以下配置:

{
    "key": "f6",
    "command": "code-runner.run",
    "when": "editorTextFocus && resourceExtname in ['.c', '.cpp']"
}

这样就可以用F6对应Code Runner的运行指令,同时只对C和C++生效

© 版权声明
THE END
喜欢就支持一下吧
点赞0支持一下吧? 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容