【问题标题】:How to fix compilation error "WinMain" in a C program?如何修复 C 程序中的编译错误“WinMain”?
【发布时间】:2019-06-24 16:33:47
【问题描述】:

有 3 个文件- 1.atm.c(源文件) 2.transactions.h(函数声明) 3.transactions.c(定义函数) 当我编译(GCC)时,我收到WinMain 错误。

我尝试了所有我知道的方法,我可以编译程序。 例 1: gcc -o atm.c transactions.c transactions.h //atm.c 正在以这种方式被删除。

Ex 2:因为我已经在源代码中包含了文件(.h),所以我没有在编译时给出 .h: gcc -o atm.c transactions.c //在这种情况下,文件没有被删除,而是出现WinMain 错误。

** 输出:**

gcc -o atm.c transactions.c transactions.h
C:/crossdev/src/mingw-w64-v4-git/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

atm.c:

#include "transactions.h"
int main(void) {

    initializeAccount();
    getBalance();

    //Perform first transaction
    askCustomer();
    updateAccount(amount);
    getBalance();

    //Perform second transaction
    askCustomer();
    updateAccount(amount);
    addGift(5.0);
    getBalance();

    //Perform third transaction
    askCustomer();
    updateAccount(amount);
    addGift(2.0);
    getBalance();
    thankYou();
    return EXIT_SUCCESS;
}

transactions.h:

#include <stdio.h>
#include <stdlib.h>
#ifndef TRANSACTIONS_H_
#define TRANSACTIONS_H_

float accountBalance, amount;

void initializeAccount();
void getBalance(void);
void askCustomer(void);
void updateAccount(float value);
void addGift(float giftAmount);
void thankYou(void);

#endif 

transactions.c:

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


float accountBalance, amount;


void initializeAccount(void){
    accountBalance = 0.0;
}

void addGift(float giftAmount){
    accountBalance += giftAmount;
    printf("A gift of $%.2f has been added to your \n",giftAmount);      
}

void askCustomer(void){
    printf("Next transaction please...\n");
    printf("Enter amount to credit (positive) or debit (negative):");
    scanf("%f",&amount);
}

void getBalance(void){
    printf("\ncurrent balance is $%.2f\n",  accountBalance);
}

void updateAccount(float amount){
    accountBalance += amount;
    printf("The account was updated with $%.2f\n",amount);
}

void thankYou(void){
    printf("------  Thank you!  ------");
}

【问题讨论】:

  • WinMain 是 Windows GUI 程序的入口点。您必须为使用main 作为程序入口点的控制台程序进行编译。
  • 您应该将标头中的变量声明为extern:extern float accountBalance, amount;。仅在标头 (.h) 文件中。
  • @PaulOgilvie 你到底建议我做什么?当你说控制台程序时。?我该如何编译请帮助我被困在这个问题上。
  • 包含防护也应该包含整个文件;所以,#include &lt;whatever.h&gt; 应该放在#ifndef 里面,因为包含一些无论如何都不会使用的标题是无稽之谈。
  • 很抱歉在这里帮不了你,因为我不懂gcc。我添加了 gcc 标签。也许大师可以回应。

标签: c gcc linker mingw-w64 winmain


【解决方案1】:

-o 用于命名作为程序输出的二进制可执行文件。它后面应该跟一个文件名。

你告诉 gcc 链接的可执行文件应该命名为 atm.c。这是不正确的,但也会导致该文件无法编译或链接。

正确编译的一种方法:

gcc -std=c99 atm.c transactions.c -o atm.exe

【讨论】:

    猜你喜欢
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 2021-11-16
    相关资源
    最近更新 更多