VS Code默认的运行任务对MSYS2的 MinGW不起作用

编程语言 2026-07-07

我安装了用于编译C 和C++的 MSYS2 UCRT工具。GCC和 G++在 UCRT shell中可以工作,但当我尝试通过VS Code运行一个C 程序时,它们就不起作用。

我把UCRT shell设置为默认终端和默认任务外壳,但它仍然不起作用。

"terminal.integrated.profiles.windows": {
    "ucrt64": {
        "path": "C:\\msys64\\usr\\bin\\bash.exe",
        "args": [
            "--login",
            "-i"
        ],
        "env": {
            "MSYSTEM": "UCRT64",
            "CHERE_INVOKING": "1",
            "MSYS2_PATH_TYPE": "inherit",
        }
    }
},
"terminal.integrated.automationProfile.windows": {
    "path": "C:\\msys64\\usr\\bin\\bash.exe",
    "args": [
        "--login",
        "-i"
    ],
    "env": {
        "MSYSTEM": "UCRT64",
        "CHERE_INVOKING": "1",
        "MSYS2_PATH_TYPE": "inherit",
    }
},
"terminal.integrated.defaultProfile.windows": "ucrt64",

默认的C/C++扩展任务:

{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: gcc.exe build active file",
      "command": "C:\\msys64\\ucrt64\\bin\\gcc.exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
  ],
  "version": "2.0.0"
}

运行时中止,错误信息为 "The preLaunchTask 'C/C++ gcc.exe build active file' terminated with exit code -1." 终端输出:

Executing task: C/C++: gcc.exe build active file 
Starting build...
cmd /c chcp 65001>nul && C:\msys64\ucrt64\bin\gcc.exe -fdiagnostics-color=always -g C:\Users\Dan\Documents\programming\C\test\main.c -o C:\Users\Dan\Documents\programming\C\test\main.exe
Build finished with error(s).
 *  The terminal process failed to launch (exit code: -1).

解决方案

cppbuild 任务类型始终使用 cmd /c,无论你的shell设置为何,你的终端输出也证实了这一点。切换到 type: "shell",并在任务本身中显式指定bash:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "C/C++: gcc.exe build active file",
      "command": "/ucrt64/bin/gcc",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "${fileDirname}",
        "shell": {
          "executable": "C:\\msys64\\usr\\bin\\bash.exe",
          "args": ["--login", "-i", "-c"]
        },
        "env": {
          "MSYSTEM": "UCRT64",
          "CHERE_INVOKING": "1",
          "MSYS2_PATH_TYPE": "inherit"
        }
      },
      "problemMatcher": ["$gcc"],
      "group": { "kind": "build", "isDefault": true }
    }
  ]
}

需要注意两点:

  • 使用 /ucrt64/bin/gcc(Unix路径),因为现在的外壳是bash
  • CHERE_INVOKING=1 可以在登录时防止bash将工作目录cd出

编辑: 关于路径问题,请将 commandargs 字段替换为:

"command": "/ucrt64/bin/gcc",
"args": [
    "-fdiagnostics-color=always",
    "-g",
    "$(cygpath '${file}')",
    "-o",
    "$(cygpath '${fileDirname}')/${fileBasenameNoExtension}.exe"
],

Bash在将路径传给gcc之前会先展开 $(cygpath ...),把Windows路径转换为Unix格式。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章