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

zsh执行报错:.out文件不存在,Kali及Windows下无法运行C/C++程序求助

C/C++代码在VS Code中运行失败的解决办法

先排查编译器本身是否正常

不管是Windows还是Kali,先脱离VS Code用终端手动编译,确认工具链没问题:

  • Kali Linux:
    1. 打开终端输入gcc --version,没输出就先装编译器:sudo apt update && sudo apt install gcc g++
    2. 写个简单的test.c文件:
      #include <stdio.h>
      int main() {
          printf("Hello\n");
          return 0;
      }
      
    3. 编译:gcc test.c -o test,运行./test,能输出Hello就说明编译器正常,问题出在VS Code配置。
  • Windows:
    1. 打开cmd输入gcc --version,提示找不到命令的话,把MinGW的bin目录(比如C:\mingw64\bin)加到系统环境变量PATH,重启cmd和VS Code再试。
    2. 同样写test.c,编译:gcc test.c -o test.exe,运行test.exe,能输出就说明编译器正常。

修正VS Code的配置文件

你的JSON配置大概率是跨系统没改对,重点调整这两个文件:

tasks.json(编译任务)

  • Kali:编译命令用gcc,输出文件不带.exe:
    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "gcc build active file",
                "type": "shell",
                "command": "gcc",
                "args": [
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "problemMatcher": ["$gcc"]
            }
        ]
    }
    
  • Windows:编译命令用gcc.exe,输出带.exe:
    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "gcc build active file",
                "type": "shell",
                "command": "gcc.exe",
                "args": [
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}\\${fileBasenameNoExtension}.exe"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "problemMatcher": ["$gcc"]
            }
        ]
    }
    

launch.json(调试配置)

  • Kali:program指向无后缀的可执行文件:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "gcc - Build and debug active file",
                "type": "cppdbg",
                "request": "launch",
                "program": "${fileDirname}/${fileBasenameNoExtension}",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${fileDirname}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "gcc build active file",
                "miDebuggerPath": "/usr/bin/gdb"
            }
        ]
    }
    
  • Windows:program指向.exe文件,miDebuggerPath指向MinGW里的gdb.exe:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "gcc.exe - Build and debug active file",
                "type": "cppdbg",
                "request": "launch",
                "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${fileDirname}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "gcc build active file",
                "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe"
            }
        ]
    }
    

其他注意事项

  • 别把Windows的配置直接复制到Kali用,两个系统的路径分隔符(Windows是\,Linux是/)、可执行文件后缀完全不同。
  • 提示“找不到.exe”时,先看VS Code终端的错误输出,很多时候是编译没成功,根本没生成文件,错误信息会告诉你是语法问题还是编译器没找到。
  • 确保VS Code的C/C++扩展是最新版,旧版本可能有兼容性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 21:05:25