簡體   English   中英

如何在C程序中修復編譯錯誤“ WinMain”?

[英]How to fix compilation error “WinMain” in a C program?

有3個文件- 1. atm.c (源文件)2. transactions.h (函數聲明)3. transactions.c (定義函數)當我編譯(GCC)此我得到一個WinMain錯誤。

我嘗試了所有我知道的方式,以便可以編譯程序。 例1: gcc -o atm.c transactions.c transactions.h //以這種方式刪除atm.c

例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 

transaction.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!  ------");
}

-o用於命名二進制可執行文件,它是程序的輸出 后面應有一個文件名。

您告訴gcc,鏈接的可執行文件應命名為atm.c。 這是不正確的,但也會導致該文件無法編譯或鏈接。

正確編譯的一種方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM