下载安装mingw配置C++编译环境 及C环境

发布于:2025-04-01 ⋅ 阅读:(16) ⋅ 点赞:(0)

一.mingw相关操作

1.1.mingw官网下载

1.2.配置环境变量

1.2.1.复制路径配置新增系统变量

1.2.2.验证MinGW是否可以正常使用 

  • 环境变量配置:确保 E:/APP/VScode/mingw64/bin 已添加到系统 Path 变量中。
  • 编译器验证:在终端输入 g++ -v,若输出版本信息(如 g++ (x86_64-posix-seh-rev0) 8.1.0),则配置正确。

二.设置VSCode相关

2.1.下载安装好VSCode后先汉化

2.2.再C/C++安装相关插件

2.3.按需求补充插件

三.配置相关json(根据自身安装位置等配置)

3.1 C++相关的配置

3.1.1 c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "E:/APP/VScode/mingw64/bin/g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

3.1.2 launch.json

{
  "version": "0.2.0",
  "configurations": [
      {
          "name": "g++.exe - 生成和调试活动文件",
          "type": "cppdbg",
          "request": "launch",
          "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${workspaceFolder}",
          "environment": [],
          "externalConsole": false,
          "MIMode": "gdb",
          "miDebuggerPath": "E:/APP/VScode/mingw64/bin/gdb.exe",
          "setupCommands": [
              {
                  "description": "为 gdb 启用整齐打印",
                  "text": "-enable-pretty-printing",
                  "ignoreFailures": true
              },
              {
                  "description": "将反汇编风格设置为 Intel",
                  "text": "-gdb-set disassembly-flavor intel",
                  "ignoreFailures": true
              }
          ],
          "preLaunchTask": "build"
      }
  ]
}

3.1.3 tasks.json

{
  "version": "2.0.0",
  "tasks": [
      {
          "type": "cppbuild",
          "label": "build",
          "type": "shell",
          "command": "E:/APP/VScode/mingw64/bin/g++.exe",
          "args": [
              "-g",
              "${file}",
              "-o",
              "${fileDirname}/${fileBasenameNoExtension}"
          ],
          "group": {
              "kind": "build",
              "isDefault": true
          },
          "problemMatcher": [
              "$gcc"
          ]
      }
  ]
}

常见问题排查:
1.编译器路径错误:检查tasks.json和launch.json中的路径是否包含\bin\
2.中文路径问题:工程路径不要包含中文或特殊字符
3.旧进程占用:若修改后不生效,删除.vscode目录重新配置

3.1.4 settings.json

某些情况下可能需要修改settings.json。例如,设置IntelliSense模式或编译器路径。

需要确认用户是否需要在settings.json中添加特定设置,比如文件关联或插件配置。

{
    "files.associations": {
        "iostream": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "optional": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "typeinfo": "cpp"
    }
}

3.2 C相关的配置

3.2.1 c_cpp_properties.json

{
  "configurations": [
      {
          "name": "Win32",
          "includePath": [
              "${workspaceFolder}/**",
              "E:/APP/VScode/mingw64/include",
              "E:/APP/VScode/mingw64/x86_64-w64-mingw32/include"
          ],
          "defines": [
              "_DEBUG",
              "UNICODE",
              "_UNICODE"
          ],
          "windowsSdkVersion": "10.0.19041.0",
          "compilerPath": "E:/APP/VScode/mingw64/bin/gcc.exe",
          "cStandard": "c11",
          "cppStandard": "c++17",
          "intelliSenseMode": "windows-gcc-x64"
      }
  ],
  "version": 4
}

3.2.2 launch.json

{
  "version": "0.2.0",
  "configurations": [
      {
          "name": "gcc.exe - 生成和调试活动文件",
          "type": "cppdbg",
          "request": "launch",
          "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${workspaceFolder}",
          "environment": [],
          "externalConsole": false,
          "MIMode": "gdb",
          "miDebuggerPath": "E:/APP/VScode/mingw64/bin/gdb.exe",
          "setupCommands": [
              {
                  "description": "为 gdb 启用整齐打印",
                  "text": "-enable-pretty-printing",
                  "ignoreFailures": true
              },
              {
                  "description": "将反汇编风格设置为 Intel",
                  "text": "-gdb-set disassembly-flavor intel",
                  "ignoreFailures": true
              }
          ],
          "preLaunchTask": "build"
      }
  ]
}

3.2.3 tasks.json

{
  "version": "2.0.0",
  "tasks": [
      {
          "label": "build",
          "type": "shell",
          "command": "E:/APP/VScode/mingw64/bin/gcc.exe",
          "args": [
              "-g",
              "${file}",
              "-o",
              "${fileDirname}/${fileBasenameNoExtension}"
          ],
          "group": {
              "kind": "build",
              "isDefault": true
          },
          "problemMatcher": [
              "$gcc"
          ]
      }
  ]
}

整理不易,诚望各位看官点赞 收藏 评论 予以支持,这将成为我持续更新的动力源泉。若您在阅览时存有异议或建议,敬请留言指正批评,让我们携手共同学习,共同进取,吾辈自当相互勉励!