C++ Undefined reference to `main (.text+0x24)

I have some problems with compiling my program since adding .h files to it.

The error I'm getting looks like this.

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status

I've tried adding int main() to minesClass.cpp but it didn't fix the problem

Program contains three files: main.cpp, minesClass.h, minesClass.cpp

main.cpp

#include "minesClass.h"
#include <iostream> 
#include <time.h>

#ifndef MINESBOARD_H__
#define MINESBOARD_H__

int main()
{
  MinesweeperBoard board(10, 10, GameMode::NORMAL);

  board.getMines();

  board.debug_display();

}

#endif

minesClass.cpp

#include "minesClass.h"

#define MINESBOARD_H__
#ifndef MINESBOARD_H__

MinesweeperBoard::MinesweeperBoard(int width, int height, GameMode mode)
{
  for (int column = 0; column < height; column++) 
  {
    for (int row = 0; row < width; row++)
    {
      board[column][row].hasFlag = false;
      board[column][row].isRevealed = false;
    }
  }
}

void MinesweeperBoard::getMines(GameMode mode)  
{
    srand(time(NULL));
    
    switch (mode)
    {
    case 1:
        EASY;

        for (int column = 0; column < height; column++)
        {
            for (int row = 0; row < width; row++)
            {
                int minePropability = RAND_MAX * 0.1;
                int num = rand();

                if (num <= minePropability)
                {
                    board[column][row].hasMine = 1;
                }
                else
                    board[column][row].hasMine = 0;
            }
        }

        break;
    case 2:
        NORMAL;

        for (int column = 0; column < height; column++)
        {
            for (int row = 0; row < width; row++)
            {
                int minePropability = RAND_MAX * 0.2;
                int num = rand();

                if (num <= minePropability)
                {
                    board[column][row].hasMine = 1;
                }
                else
                    board[column][row].hasMine = 0;
            }
        }

        break;
    case 3:
        HARD;

        for (int column = 0; column < height; column++)
        {
            for (int row = 0; row < width; row++)
            {
                int minePropability = RAND_MAX * 0.3;
                int num = rand();

                if (num <= minePropability)
                {
                    board[column][row].hasMine = 1;
                }
                else
                    board[column][row].hasMine = 0;
            }
        }

        break;
    case 4:
        DEBUG;

        break;
    default:

        break;
    }
    
}

void MinesweeperBoard::debug_display() const
{

for (int column = 0; column < height; column++)
  {
    for (int row = 0; row < width; row++)
    {
      std::cout << "[";
      if (board[column][row].hasMine)
        std::cout << "M";
      else
        std::cout << ".";
      if (board[column][row].isRevealed)
        std::cout << "o";
      else
        std::cout << ".";
      if (board[column][row].hasFlag)
        std::cout << "f";
      else
        std::cout << ".";
      std::cout << "]";
    }
    std::cout << width<<height<<std::endl;
  }

}

#endif

minesClass.h

#include <iostream>

#define MINESBOARD_H__
#ifndef MINESBOARD_H__

enum GameMode
{
  DEBUG,
  EASY,
  NORMAL,
  HARD
};

struct Field
{
  bool hasMine;
  bool hasFlag;
  bool isRevealed;
};

class MinesweeperBoard
{

  Field board[100][100];
  int width;
  int height;
  
public:

  MinesweeperBoard(int width, int height, GameMode mode);

  void debug_display() const;
  
  void getMines(GameMode mode);
};

#endif

I've looked through similar questions asked with "undefined reference to `main" but most of them were based on missing int main().


Solution 1:

Your include guards are wrong.

Usually .cpp won't need include guards, so you should remove the lines with #ifndef, #define, and #endif from your .cpp files.

Then, you should change

#define MINESBOARD_H__
#ifndef MINESBOARD_H__

in your minesClass.h to

#ifndef MINESBOARD_H__
#define MINESBOARD_H__

Otherwise, using #ifndef after using #define will always prevent it from using lines after that.