使用VS Code学习C/C++时出现中文乱码、窗口闪退

关于窗口闪退问题可以在程序中添加:

#include <stdlib.h>

system("pause"); 

当然也可以在.vscode文件中的配置文件中进行一些修改以解决此问题:

在launch.json中"configurations"下面进行修改:

"program": "C:\\Windows\\System32\\cmd.exe",
"args": ["/c","${fileDirname}\\${fileBasenameNoExtension}.exe","&","pause"],

可以直接赋值下面的然后粘贴到你的launch.json中:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "C:\\Windows\\System32\\cmd.exe",
            "args": ["/c","${fileDirname}\\${fileBasenameNoExtension}.exe","&","pause"],
            
            // "program":"${fileDirname}\\${fileBasenameNoExtension}.exe",
            // "args":[],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc.exe 生成活动文件"
        }
    ]
}

上面涉及到路径的地方要改为自己的路径,上面第19行的"true"表示你到使用外部终端,如果要用vscode内部自带终端的话就改了"false"就好了。

关于中文乱码需要修改tasks.json

添加上:"-fexec-charset=GBK"

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",
            "command": "C:\\mingw64\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-fexec-charset=GBK"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

如果VSCode还没安装的话,关于VSCode的下载安装也很简单:Visual Studio Code - Code Editing. Redefined

此外还需下载MinGW:

MinGW - Minimalist GNU for Windows download | SourceForge.net

 MinGW官网:MinGW-w64

在官网中点击“Downloads"-->"SourceForge

猜你喜欢

转载自blog.csdn.net/weixin_51995147/article/details/128443355
今日推荐