You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

preLaunchTask 'C/C++:gcc.exe build active file' 退出码-1报错求助

问题说明

你遇到的The preLaunchTask 'C/C++:gcc.exe build active file' terminated with exit code -1报错和你贴的矩阵加法代码无关,这段C代码语法、逻辑均无问题,可正常编译运行。该报错是VS Code中C/C++扩展的调试前置编译任务执行失败导致的,属于环境配置问题。

对应排查&解决步骤
  • 第一步:验证gcc编译器是否可用
    打开系统终端,输入gcc -v执行:
    • 如果终端提示找不到命令,说明你未安装MinGW编译环境,或者MinGW的bin目录未添加到系统PATH环境变量,先完成MinGW安装和环境变量配置,重启VS Code后再尝试调试。
    • 如果能正常输出gcc版本信息,输入where gcc记录返回的gcc.exe完整绝对路径,后续配置需要用到。
  • 第二步:检查代码文件路径
    确认你存放C代码的文件夹路径、代码文件名均为纯英文,不含中文、空格、特殊符号,不要放在中文用户目录、带特殊符号的文件夹下,建议放到类似D:\c_practice这类纯英文路径中,文件名改成matrix_add.c这类纯英文名称。
  • 第三步:修正tasks.json编译配置
    打开项目目录下.vscode文件夹中的tasks.json文件:
    1. 找到command字段,将值替换为之前where gcc查到的gcc.exe完整绝对路径,示例:
      "command": "C:\\mingw64\\bin\\gcc.exe"
      
    2. 找到args字段,替换为标准编译参数,避免参数错误导致编译失败:
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ]
      
  • 第四步:排查文件占用问题
    如果之前运行过程序出错,可能导致生成的exe文件被系统占用无法覆盖编译,打开任务管理器找到和你C文件同名的exe进程,结束进程后再重试。
配置验证

完成上述步骤后,先在VS Code终端中手动执行编译命令验证:

gcc matrix_add.c -o matrix_add.exe

如果命令执行无报错,且目录下生成了对应的exe文件,运行exe可正常实现矩阵加法功能,再点击调试按钮就不会触发该报错。

附你提供的可正常运行的矩阵加法代码参考:

#include <stdio.h>
int main() 
{
  int r, c, a[100][100], b[100][100], sum[100][100], i, j;
  printf("Enter the number of rows (between 1 and 100): ");
  scanf("%d", &r);
  printf("Enter the number of columns (between 1 and 100): ");
  scanf("%d", &c);

  printf("\nEnter elements of 1st matrix:\n");
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j) {
      printf("Enter element a%d%d: ", i + 1, j + 1);
      scanf("%d", &a[i][j]);
    }

  printf("Enter elements of 2nd matrix:\n");
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j) {
      printf("Enter element b%d%d: ", i + 1, j + 1);
      scanf("%d", &b[i][j]);
    }

  // adding two matrices
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j) {
      sum[i][j] = a[i][j] + b[i][j];
    }

  // printing the result
  printf("\nSum of two matrices: \n");
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j) {
      printf("%d   ", sum[i][j]);
      if (j == c - 1) {
        printf("\n\n");
      }
    }

  return 0;
}

内容的提问来源于stack exchange,提问作者KitchenThugJr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 16:57:23