How to set vscode terminal charset(for decoding) to GBK, or set vscode gcc build task use "-fexec-charset=UTF-8"

85 views Asked by At

When i use the button "run C/c++ file" to run a ".c" file, and i got wrong characters in my terminal(default by powershell).

  1. all is on windows 11
  2. the .c file is utf-8 format
  3. after run task finished, i inputed "chcp" in the named "cppdbg" terminal, and i got "65001"(UTF-8), so i gusses the ordinary outputs is encoded by "936"(GBK).

in other words, the ordinary outputs is encoded by "936"(GBK) and the "cppdbg" terminal decode with "65001"(UTF-8), so they are not matched correctly.

I know there is two ways to fix this problem:

  1. Set "gcc" to complile with arg "-fexec-charset=UTF-8", then i set it in task.json like
"args": [
                "-fdiagnostics-color=always",
                "-fexec-charset=UTF-8",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ]

but it does not work. and i use gcc to compile and link directly rather than using the vscode button "run C/c++ file", Nodoubtedly it works.

  1. Set the "cppdbg" terminal decoding to "936", i tred to set it in setting.json like
"terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "icon": "terminal-powershell",
            "args": ["-NoExit","\C","chcp 936"]
        }
}

but it also does not work, "chcp 936" executed absolutely, but there is something hided in the button "run C/c++ file", still show wrong character and input "chcp" will get "65001" not "936"

Finally i use a extension named "code runner", and set gcc arg "-fexec-charset=UTF-8", it works,and i want to know why both solution 1 and 2 which i mentioned above are not work.

0

There are 0 answers