0

I'm currently learning OOP in C++, but I keep running into a "undefined reference to 'WinMain'" error. I'm not sure what I did wrong, and if it is the header file or cpp file. Please help.

Error message:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function 'main':
    C:/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to 'WinMain'
    collect2.exe: error: ld returned 1 exit status

header file:

#include <iostream>
#include <string>
using namespace std;
#pragma once

enum COLOR {Green, Blue, White, Black, Brown};

class Animal{
    private:
        string _name;
        COLOR _color;
    public:
        Animal();
        Animal(string n, COLOR c);
        ~Animal();
        virtual void speak() const;
        virtual void move() = 0;
        string getName();
        COLOR getColor();
};

class Mammal: public Animal{
    public:
        Mammal();
        Mammal(string n, COLOR c);
        ~Mammal();
        virtual void eat() const;
        virtual void move() const;
};

.cpp file:

#include <iostream>
#include <string>
#include "Animal.h"
using namespace std;

/*___________________Animal class_______________________*/
Animal::Animal() : _name("Unknown"){
    cout << "constructing Animal object "<< _name << endl;
}
Animal::Animal(string n, COLOR c) : _name(n), _color(c){
    cout << "constructing Animal object "<< _name << endl;
}
Animal::~Animal(){
    cout << "destructing Animal object "<< _name << endl;
}

void Animal::speak() const{
    cout << "Animal speaks "<< endl;
}

string Animal::getName(){
    return _name;
}

COLOR Animal::getColor(){
    return _color;
}

/*___________________Mammal class______________________*/
Mammal::Mammal() : Animal(){
    cout << "constructing Mammal object"<< Mammal::getName() << endl;
}

Mammal::Mammal(string n, COLOR c) : Animal(n, c){
    cout << "constructing Mammal object"<< Mammal::getName() << endl;
}

Mammal::~Mammal(){
    cout << "destructing Mammal object "<< Mammal::getName() << endl;
}

void Mammal::eat() const{
    cout << "Mammal eats"<< endl;
}

void Mammal::move() const{
    cout << "Mammal moves" << endl;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Xavier
  • 1
  • 4
  • 4
    Where is your `int main()`? With MinGW if the linker does not find an int main() it looks for WinMain() if that is not present it ends with an error. – drescherjm Oct 24 '22 at 03:23
  • You wrote all that code before testing any of it? Here's a tip which for some reason they never teach in school: when you write code, start with something small and simple that works perfectly, then add complexity a little at a time, testing at every step. – Beta Oct 24 '22 at 03:25
  • @Beta i wrote the code in a single .cpp file and it worked perfectly. But when i split the code into multiple header and implementation files, i ran into this error. – Xavier Oct 24 '22 at 03:31
  • @drescherjm i split my main program and class into 2 implementation files – Xavier Oct 24 '22 at 03:33
  • Then you did not compile both files into your executable. If you are using VSCode remember that by default it builds only the active file and ignores all other source files. The documentation tells you about that here and how to build all source files: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Oct 24 '22 at 03:38
  • Ah, so it's a linking error. I'm sorry that I made a false assumption. Whatever method you use to build the executable, you are failing to incorporate the source file containing `main()`. – Beta Oct 24 '22 at 03:39
  • If you're building a cpp file and don't immediately want to produce an executable use the `-c` flag to tell gcc you want an object instead – Alan Birtles Oct 24 '22 at 05:55

0 Answers0