-5

I am trying to compile my classes but I am getting the following error

/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../lib64/crt1.o: In function `_start':
   (.text+0x20): undefined reference to `main'
    collect2: error: ld returned 1 exit status
    make: *** [try] Error 1

However, I do include a main function, in a class I call Hash.cpp. Here is the makefile:

C++ = g++
CFLAGS = -c -g

all: hash                                                                               

hash: Hash.o ML_hash.o
        $(C++) -o hash Hash.o ML_hash.o

%.o: %.cpp
        $(C++) $(CFLAGS) $*.cpp

When I call "make clean" and "make" though, the output executable "hash" will still be generated. If Hash.cpp is listed in the makefile and contains a main function, why I am getting the error below?

EDIT:

Sorry....I meant to say main function. And here is the main class:

#include "HashNode.h"
#include "MLH_back1.h"

#include <iostream>

using std::cout;
using std::endl;

int main()
{
    doStuff();
}

I'm sorry if I didn't post enough code. Usually when I ask questions I try to post where I think the problem is occurring, so just relevant information is listed instead of all of my classes. By saying "I have a main function" I meant to say that yes I did write a main function in a class called Hash.cpp. I was just asking to try to get a conceptual understanding of why this error would occur when I have a main function. I have looked at other questions so I'm not trying to duplicate. I didn't realize people would downvote this so much...

danielschemmel
  • 10,885
  • 1
  • 36
  • 58
Jeremy Fisher
  • 2,510
  • 7
  • 30
  • 59
  • 4
    You need to post the code that is causing the error! Also you must have a `main()` function at global scope. It cannot be a class or be inside another class. – Daniel May 03 '14 at 16:14
  • 4
    *"However, I do include a main class, in a class I call Hash.cpp.*" A main class? This is not Java, `main` is a function. A function with signature `int main()` or `int main( int argc , char* argv[])` defined in one (And exactly one) of the source files (`.cpp`s) of your project – Manu343726 May 03 '14 at 16:14
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – πάντα ῥεῖ May 03 '14 at 16:29
  • Returning 0 works. I think there might have been a problem with my Makefile because after I edited that, I didn't get the above error. Thanks. – Jeremy Fisher May 03 '14 at 16:48

1 Answers1

1

However, I do include a main class, in a class I call Hash.cpp

In C++, you need to have a main function, not class or a method in a class as it appears in other OOP languages.

That is the entry point for your program. That is not quite fair, since there is some preprocessing done for you where your main function is called, but that is your entry that you write.

The most trivial C++ program would like this:

int main() { return 0; }

If that bails out with the same error, it is likely that you are mixing up 32 and 64 bits in which case such issues can also occur with the "entry" point.

László Papp
  • 51,870
  • 39
  • 111
  • 135