0

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!  ------");
}
heeat
  • 128
  • 2
  • 9
  • 1
    `WinMain` is the entry point for a Windows GUI program. You must compile for a console program which uses `main` as program entry point. – Paul Ogilvie Jan 31 '19 at 09:01
  • You should declare the variables in the header as `extern`: `extern float accountBalance, amount;`. Only in the header (`.h`) files. – alx - recommends codidact Jan 31 '19 at 09:10
  • @PaulOgilvie What exactly are you suggesting me to do? when you say console program .?How do i compile please help i am stuck at this. – heeat Jan 31 '19 at 10:38
  • Also the include guards should be including the whole file; so, the `#include ` should go inside the `#ifndef`, because it's nonsense to include some headers that are not going to be used anyway. – alx - recommends codidact Jan 31 '19 at 10:44
  • Sorry that I can't help you here because I don't know gcc. I added the gcc tag. Maybe a guru can respond. – Paul Ogilvie Jan 31 '19 at 10:51

1 Answers1

3

-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
Lundin
  • 195,001
  • 40
  • 254
  • 396