• 【编译器】2022-11-VSCode配置编译与调试C++程序(含输入输出重定向)


    1、最终效果

    • VSCode中运行c++程序,可以采用Coderunner(安装插件并配置)
      https://gwj1314.blog.csdn.net/article/details/100607554

    • 在VSCode中运行c++程序,也可以采用CPH(安装插件即可)
      在这里插入图片描述
      在这里插入图片描述

    • 也可以采用官方的C++插件(安装插件并配置)
      在这里插入图片描述
      在这里插入图片描述

    2、环境配置(官方C++插件支持)

    • 首先安装微软官方的C++插件支持
      在这里插入图片描述

    • 然后在工作区文件夹的.vscode目录中进行配置(4个文件)
      在这里插入图片描述

    c_cpp_properties.json(修改对应的目录,c++版本和编译模式)

    {
        "configurations": [
            {
                "name": "Win32",
                "includePath": [
                    "${workspaceFolder}/**"
                ],
                "defines": [
                    "_DEBUG",
                    "UNICODE",
                    "_UNICODE"
                ],
                "windowsSdkVersion": "10.0.19041.0",
                "compilerPath": "C:/Development/x86_64-8.1.0-release-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe",
                "cStandard": "c17",
                "cppStandard": "c++17",
                "intelliSenseMode": "gcc-x64"
            }
        ],
        "version": 4
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    launch.json(修改输入输出重定向的文件in1.txt, out1.txt)

    {
        // 使用 IntelliSense 了解相关属性。 
        // 悬停以查看现有属性的描述。
        // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name": "C++调试", // 这个调试任务的名字,当你开始调试的时候待选项中的名字
                "type": "cppdbg",
                "request": "launch",
                "program": "${fileDirname}/${fileBasenameNoExtension}",
                "args": [ //文件重定向功能,如果不需要使用请移除下面所有参数
                    "<",
                    "${fileDirname}/in1.txt",
                    ">",
                    "${fileDirname}/out1.txt",
                ],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": false, // 这个选项控制是否在新窗口中运行
                "MIMode": "gdb",
                "miDebuggerPath": "gdb", // 你的MinGW编译器下gdb的目录,请根据实际情况调整
                "setupCommands": [
                    {
                        "description": "为 gdb 启用整齐打印",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "C/C++: g++.exe 生成活动文件" // task.json中的任务名称,请根据实际情况调整
            }
        ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    tasks.json(修改g++编译器的位置)

    {
        "tasks": [
            {
                "type": "cppbuild",
                "label": "C/C++: g++.exe 生成活动文件",
                "command": "C:\\Development\\x86_64-8.1.0-release-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
                "args": [
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}\\${fileBasenameNoExtension}.exe"
                ],
                "options": {
                    "cwd": "${fileDirname}"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "detail": "调试器生成的任务。"
            }
        ],
        "version": "2.0.0"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    settings.json(一般不用改)

    {
        "files.associations": {
            "iostream": "cpp",
            "array": "cpp",
            "atomic": "cpp",
            "*.tcc": "cpp",
            "bitset": "cpp",
            "cctype": "cpp",
            "cfenv": "cpp",
            "charconv": "cpp",
            "chrono": "cpp",
            "cinttypes": "cpp",
            "clocale": "cpp",
            "cmath": "cpp",
            "codecvt": "cpp",
            "complex": "cpp",
            "condition_variable": "cpp",
            "csetjmp": "cpp",
            "csignal": "cpp",
            "cstdarg": "cpp",
            "cstddef": "cpp",
            "cstdint": "cpp",
            "cstdio": "cpp",
            "cstdlib": "cpp",
            "cstring": "cpp",
            "ctime": "cpp",
            "cuchar": "cpp",
            "cwchar": "cpp",
            "cwctype": "cpp",
            "deque": "cpp",
            "forward_list": "cpp",
            "list": "cpp",
            "unordered_map": "cpp",
            "unordered_set": "cpp",
            "vector": "cpp",
            "exception": "cpp",
            "algorithm": "cpp",
            "functional": "cpp",
            "iterator": "cpp",
            "map": "cpp",
            "memory": "cpp",
            "memory_resource": "cpp",
            "numeric": "cpp",
            "optional": "cpp",
            "random": "cpp",
            "ratio": "cpp",
            "regex": "cpp",
            "set": "cpp",
            "string": "cpp",
            "string_view": "cpp",
            "system_error": "cpp",
            "tuple": "cpp",
            "type_traits": "cpp",
            "utility": "cpp",
            "fstream": "cpp",
            "future": "cpp",
            "initializer_list": "cpp",
            "iomanip": "cpp",
            "iosfwd": "cpp",
            "istream": "cpp",
            "limits": "cpp",
            "mutex": "cpp",
            "new": "cpp",
            "ostream": "cpp",
            "scoped_allocator": "cpp",
            "shared_mutex": "cpp",
            "sstream": "cpp",
            "stdexcept": "cpp",
            "streambuf": "cpp",
            "thread": "cpp",
            "typeindex": "cpp",
            "typeinfo": "cpp",
            "valarray": "cpp"
        },
        "java.dependency.syncWithFolderExplorer": false,
        "jupyter.jupyterServerType": "local"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    最后推荐一个在线C++编译器
    在这里插入图片描述

  • 相关阅读:
    2023 年 数维杯(A题)国际大学生数学建模挑战赛 |数学建模完整代码+建模过程全解全析
    数据响应式原理
    Dubbo: 基于SpringBoot+Dubbo的Provider/Consumer的实践
    GTX312L比TSM12更具优势的智能门锁触摸芯片方案
    专精特新是指的哪些企业?专精特新通过有什么补贴?
    Tair的使用
    Ubuntu 搭建 STM32 开发环境
    不得不说,还是这款开源工作流表单设计器较合心意!
    安全渗透测试之网络基础知识(IP地址)
    ROS-TCP-Connector and ROS-TCP-Endpoint
  • 原文地址:https://blog.csdn.net/qq_33957603/article/details/128074601