3

I am trying to compile the body of a class that I got from my e-book after designing the header file, but I am getting this error message:

[Linker error] c:/crossdev/src/mingw-w64-svn/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain@16'

file: C:\Users\Chuks Joe\Desktop\collect2.exe

Message:[Error] ld returned 1 exit status

The header file is in a separate file called dice.h and the class body in another separate file called called dice.cpp. The client program is in a separate file too called testdice.cpp.

How do I arrange these files and where do I put the .so file so that they can all be linked together for my program to run.

The compiler I am using is Dev-C++.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

2

Difficult to tell without seeing the code. You can have a look at how the linker works in this post. In your case, the error most likely comes from one of:

  • you are trying to use a function or method you declared in the header file, but forgot to define in the source file.
  • you are calling the linker with only the object file for testdice and forgot the object file for dice.
  • is the undefined function yours? if not, you most likely need to add external libraries to the linker.

To narrow it down, try to use other functions defined in testdice.cpp. If that results in more linker errors, you are most likely not linking everything together. If it doesn't add more errors, you most likely forgot to define the function, or the prototype is slightly different in the source file and the header file.

Community
  • 1
  • 1
Thibaut
  • 2,400
  • 1
  • 16
  • 28
0

For your code to be linkable as an executable it must have either a main() (or WinMain() for GUI programs) entry point. A class on its own is not a complete program in C++ - execution starts from the main() function.

For some reason, even for non-GUI apps the MinGW linker will complain about absence of WinMain() rather than main() but either will resolve the link.

You must normally provide the main() or WinMain() function, the exception being when you are using an application framework (usually GUI) that provides it internally.

In most cases to use a class you must instantiate an object of that class and invoke its member functions through that instance. Static member functions do not need an object instance.

Example:

// main.cpp
#include <iostream>
#include "dice.h"

int main()
{
    cDice myDie ;
    std::cout << "Throw = " << myDie.roll() ; 
}
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • thanks pals for your assistance so far. the class is now working. but the new problem am having now is that the class implementation and the program am trying to run must be in the same file before it can work. so please, what should I do? – Chukwunonso Orjiakor Mar 27 '13 at 20:06
  • @ChuksJoeOrjiakor: What you should do perhaps is post a new question. However you might avoid that by searching on *"separate compilation and linking"*. – Clifford Mar 28 '13 at 15:40
  • Hi boss, I have done just what you suggested and I will so much appreciate your comment on that post. Thanks – Chukwunonso Orjiakor Apr 21 '13 at 18:28