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

运行C代码触发LNK2019错误,无法定位问题根源

解决LNK2019: 未解析的外部符号 _main 错误

可能的原因及修复步骤

1. 项目子系统配置错误(Visual Studio环境)

若使用Visual Studio编译,项目默认可能设置为Windows子系统,但你的代码是控制台程序,需调整为控制台子系统:

  • 右键项目 → 属性 → 配置属性 → 链接器 → 系统 → 子系统,选择控制台 (/SUBSYSTEM:CONSOLE)
  • 确保配置匹配当前使用的Debug/Release模式

2. 代码拼写错误引发编译异常

代码中存在明显拼写错误:else分支里的print应为printf,该错误会导致编译失败,间接触发链接阶段异常。修复后else分支代码:

else {
    total_exp = qty * price;
    printf("total_exp: %d\n", total_exp);
}

3. 缺失必要头文件

getch()函数需要依赖<conio.h>头文件,未包含会导致函数未定义,间接影响链接过程。在代码开头添加:

#include <conio.h>

另外,代码中引入的<math.h>未被使用,可删除以减少不必要依赖。

4. 入口函数定义问题

确保main函数定义正确,未误写为WinMain或首字母大写的Main,你的int main()是标准控制台入口,无需修改。

修复后的完整代码

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
    int qty, price, total_exp, discount = 10;

    printf("\nenter qty: ");
    scanf_s("%d", &qty);
    printf("Entered qty: %d\n", qty);

    printf("enter the price: \n");
    scanf_s("%d", &price);
    printf("Entered price: %d\n", price);

    if (qty > 1000) {
        total_exp = (qty * price * discount) / 100;
        printf("total_exp: %d\n", total_exp);
    }
    else {
        total_exp = qty * price;
        printf("total_exp: %d\n", total_exp);
    }
    getch();
    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 05:20:28