Author Topic: [SOLVED] Resolved but why? - undefined reference to '_WinMain@16'  (Read 25606 times)

Offline Rapster

  • Single posting newcomer
  • *
  • Posts: 3
Hi all!

I've just started learning c++ (so you could call me a novice!). Anyway, I had an issue that I have now resolved. But what I'd like to know is why the error happened in the first place.

Let me explain:
I was following a tutorial for a console application. There was only one page of code named main.cpp. I hadn't created a project in Code::Blocks, I just created a new empty file, and coded away. When I compiled and ran the code, it worked just fine.

Then, as the tutorial instructed, I split the code into main.cpp and GradeBook.h. Again, after compiling and running, it worked just fine.

Then the tutorial dictated that the GradeBook.h file should be itself split into 2 parts: GradeBook.h containing the class's interface (prototypes), and  GradeBook.cpp, which contained the member function definitions for the GradeBook class in the header file.

This time, however, when I compiled and ran the files it didn't work. Instead, I got the following error message: undefined reference to '_WinMain@16'.

Of course, at first I thought I had miscoded something somewhere, but after 30 minutes of thorough checking the code was fine. The next morning, I decided to create a console project and I added the same files that I had already written into the new project (without changing a single line of code!).
This time, when compliled and run, it worked just fine.

So, I haven't really understood what or why the error is/happened. Can someone explain any underlying issues, please?

Many thanks in advance,
Tony
« Last Edit: December 04, 2009, 09:33:25 am by Rapster »

zabzonk

  • Guest
Re: Resolved but why? - undefined reference to '_WinMain@16'
« Reply #1 on: December 02, 2009, 10:23:47 am »
If you compile a single file to an executable with CB, that file must contain a main() function. WinMain is the windows version of main() that MinGW supplies if it sees a main() function in your code. If it does not, you will get the linker error.

If you create a project, then all files (including the one that actually contains main()) are linked, so there is no problem. CB is really not intended for compiling single files - you should always prefer to use a project.

Offline Rapster

  • Single posting newcomer
  • *
  • Posts: 3
Re: Resolved but why? - undefined reference to '_WinMain@16'
« Reply #2 on: December 02, 2009, 02:49:02 pm »
Hi Zabzonk!

I don't want to argue with someone who obviously knows better than me, but like I said in my original post, it compiled fine when there was just the one file (main.cpp) that contained the function int main() and a class.

It even compiled fine when I split the file into main.cpp (int main()) and GradeBook.h (class).

It was only when I split GradeBook.h into 2 files (GradeBook.h for the interface (prototypes), and GradeBook.cpp for the member function definitions) that Code::Blocks didn't like it. So, my question remains: why did Code::Blocks 'suddenly' decide that it wouldn't compile the files, when all along my files weren't contained within a 'project'?

Still puzzled,
Tony

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Resolved but why? - undefined reference to '_WinMain@16'
« Reply #3 on: December 02, 2009, 03:05:58 pm »
Hi Zabzonk!

I don't want to argue with someone who obviously knows better than me, but like I said in my original post, it compiled fine when there was just the one file (main.cpp) that contained the function int main() and a class.

It even compiled fine when I split the file into main.cpp (int main()) and GradeBook.h (class).

It was only when I split GradeBook.h into 2 files (GradeBook.h for the interface (prototypes), and GradeBook.cpp for the member function definitions) that Code::Blocks didn't like it. So, my question remains: why did Code::Blocks 'suddenly' decide that it wouldn't compile the files, when all along my files weren't contained within a 'project'?

Still puzzled,
Tony

Your error is not a compiler, but a linker error.
That means C::B still compiles fine, but fails to link.

A header file gets included (if it can be found in the includepath the compiler uses to find headers), so this works fine.

But for linking two (object-)files together C::B has to know which files should be linked. And how should it decide which files to use ?

That's the cause to create projects in C::B (and other IDE's) or to create makefiles.

If you turn on full commandline-logging in "Settings -> Compiler and debugger... -> Global compiler settings -> [the_compiler_you_use] -> Other settings (rightmost tab) -> Compiler loggin" you can see which commands are send to the compiler/linker and where the difference is between a single file and a project with multiple files.

By the way: your book/tutorial should explain the steps needed to compile and link files, what happens if headers are included and how to use prebuild libraries/dlls.

Offline Rapster

  • Single posting newcomer
  • *
  • Posts: 3
Re: Resolved but why? - undefined reference to '_WinMain@16'
« Reply #4 on: December 03, 2009, 10:56:06 am »
Hi Jens!

I think that all makes sense now. I'll check out your suggestion and take a close look.

Cheers,
Tony

PS: how do I mark your post as 'answer'?
« Last Edit: December 03, 2009, 11:00:25 am by Rapster »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Resolved but why? - undefined reference to '_WinMain@16'
« Reply #5 on: December 03, 2009, 04:34:16 pm »
Hi Jens!

I think that all makes sense now. I'll check out your suggestion and take a close look.

Cheers,
Tony

PS: how do I mark your post as 'answer'?

Most People just edit the title of the first and last post and add the prefixed [SOLVED] to it.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

zabzonk

  • Guest
Re: Resolved but why? - undefined reference to '_WinMain@16'
« Reply #6 on: December 03, 2009, 04:47:18 pm »
PS: how do I mark your post as 'answer'?


I think you may be mistaking this site for Stack Overflow :-)