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

C程序报LNK2019错误:invoke_main中引用的未解析外部符号_main

错误原因
  • 核心原因是你代码中main函数的定义不符合MSVC编译器的默认链接规则:C语言标准规定main函数的返回值必须为int类型,你写的void main会导致链接器无法匹配到预期的入口函数符号,因此抛出LNK2019错误。
修复方案
  • 第一步:修改main函数定义,替换原有void main()为int main(),并在函数末尾增加return 0;语句,修改后的完整可运行代码如下:
#include <stdio.h>
#include <string.h>
#include <conio.h> // 引入getch所需的非标准头文件

int main()
{
    printf("======================================================================");
    printf("\n====================================================================");

    char input[20];
    int i,j;
    
    puts("\n\nEnter the sentence to be reversed::\n");
    // 原gets函数已被标准废弃,替换为fgets避免缓冲区溢出风险
    fgets(input, sizeof(input), stdin);
    // 去掉fgets读取到的末尾换行符
    input[strcspn(input, "\n")] = '\0';
    printf("Input   addr: %p\n", input);
    printf("\nThe reversed sentence is:: \n\n");
    strrev(input);
   
    for(i=0; input[i]!='\0'; i++)
    {
       if(input[i+1]==' ' || input[i+1]==NULL)
       {
           for(j=i; j>=0 && input[j]!=' '; j--)
            printf("%c",input[j]);
       }
       else
       continue;
       printf(" ");
    }
    getch();
    return 0;
}
  • 如果修改后仍然报错,检查项目配置:
    1. 打开项目属性页 → 切换到「配置属性」→「链接器」→「系统」
    2. 确认「子系统」选项的值为 控制台 (/SUBSYSTEM:CONSOLE)
    3. 保存配置后重新编译即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 14:36:00