cgcclinkermingw-w64winmain

How to fix compilation error "WinMain" in a C program?


Have 3 files- 1.atm.c(Source file) 2.transactions.h(function declarations) 3.transactions.c(defining the functions) when i compile(GCC) this i am getting a WinMain Error.

And i tried all the ways i know, that i can compile the program. Ex 1: gcc -o atm.c transactions.c transactions.h //the atm.c is getting deleted in this way.

Ex 2:as i already included the file(.h) in source so i didn't give the .h in compile time : gcc -o atm.c transactions.c //in this case file is not getting deleted but getting the WinMain Error.

** OUTPUT:**

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

Solution

  • -o is used to name the binary executable which is the output of the program. It should be followed by a file name.

    You tell gcc that the linked executable should be named atm.c. Which is incorrect, but also causes that file to not get compiled or linked.

    One way to correctly compile:

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