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

VSCode编译C代码出现Undefined reference to `WinMain@16'错误求助

解决C语言编译时“undefined reference to `WinMain@16'”错误

问题场景

刚接触C语言,在VSCode中运行以下代码:

#include<stdio.h>

int main()
{
    char personName[50];
    printf("What is your name?\n");
    scanf("%s",personName);

    return 0;
}

执行编译命令时出现错误:

C:\Users\1\Desktop\HTMLCC\C> cd "c:\Users\1\Desktop\HTMLCC\C\" ; if ($?) { gcc main.c -o main } ; if ($?) { .\main }
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status

重装MinGW和VSCode的C/C++扩展后问题依旧。

解决方法

1. 检查编译命令是否包含GUI模式参数

这个错误最常见的原因是编译时不小心指定了Windows GUI程序的编译选项-mwindows,该选项会让编译器寻找WinMain作为程序入口,而非标准的main函数。

  • 打开VSCode的tasks.json(可通过快捷键Ctrl+Shift+B,选择“创建tasks.json文件”进入),确认编译参数中没有-mwindows。正确的编译配置示例如下:
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "gcc build active file",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": ["$gcc"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

2. 确认MinGW环境变量配置正确

  • 打开命令提示符,输入gcc --version,如果能正常输出gcc版本信息,说明环境变量配置无误;如果提示“找不到命令”,需将MinGW的bin目录(通常是C:\MinGW\bin)添加到系统PATH环境变量中,添加完成后重启VSCode和命令提示符。

3. 验证代码入口正确性

你的代码中已经正确定义了int main()函数,没有拼写错误(比如写成Main或返回值类型错误),这部分无需修改。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 05:32:25