9

I'm learning C++ with Code::Blocks, and everytime i try to create a new class, I get an error message saying:

undefined reference to `WinMain@16'

Here's the code I've been using:

Main Class

#include "Lime.h"
#include <iostream>
using namespace std;
int main()
{
    Lime lime;
    return 0;
}

Lime Class (.ccp):

#include "Lime.h"
#include <iostream>
using namespace std;
Lime::Lime()
{
    cout<<"Hi!";
}

Lime Header (.h):

#ifndef LIME_H
#define LIME_H
class Lime
{
    public:
        Lime();
};
#endif

If someone knows, how to fix it, please, tell me!

8
  • You have to either custom link the two object files created from compiling the two .cpps or put the files in a project.
    – chris
    Jan 14, 2014 at 2:45
  • 1
    I do not know codeblocks but you need to enable subsystem CONSOLE
    – drescherjm
    Jan 14, 2014 at 2:46
  • @drescherjm, No, that has nothing to do with the problem. See this answer
    – chris
    Jan 14, 2014 at 2:46
  • 1
    all this files are made in same project already, the problem was solved only by restarting CodeBlocks, do you know maybe why is this?
    – Relopz
    Jan 14, 2014 at 3:06
  • 1
    Simply put, you needed to have created a 'Console Application', rather than a 'Win32 GUI Project'
    – enhzflep
    Jan 14, 2014 at 4:39

11 Answers 11

10

I just had the exact same problem working with the exact same tutorials.

How to solve this? I found that restarting CodeBlocks gets rid of this error. It has nothing to do with how you created the files or any of your syntax. A restart does the trick.

Why does this occur? If I had to take a wild guess, I would think that CodeBlocks does indeed create the header/cpp files, it does not however link them to your project in a proper way that makes them usable (although it does ask you to link them to the project after you create them). This is a guess.

I understand that some people have commented on this by saying that you're creating a Windows GUI console application instead of a console application, but this is not the case. I too was creating a simple console application as Bucky explains in the videos.

3
  • 1
    This was the correct answer for me. CodeBlocks started complaining about WinMain out of the blue on a project that was compiling fine so far. Restarting did the trick. Jul 3, 2014 at 18:47
  • 1
    That is so awesome. I was doing the exact same thing, going through the exact same tutorial, using the exact same program, and having the exact same issue of which this exact same solution worked. Thanks! Aug 4, 2014 at 21:23
  • Glad I could be of help! Aug 4, 2014 at 21:36
9

Try this: Settings->Compiler, click the tab Build options, select the checkbox

Explicitly add currently compiling file's directory to compiler search dirs

P.S.

Next time when you create a new class, in the File policy section, make sure you select checkboxes

  • Add path to project

  • Header and implementation file shall be in same folder

However, do NOT select

Use relative path

2
  • 1
    I had the same issue as OP and this fixed it, thanks. Apr 3, 2014 at 19:51
  • 1
    finally a straightforward headache-less answer!!
    – 842Mono
    Jan 11, 2016 at 12:51
3

Go to "Project" --> "Build Options . . ." on menu bar
Go to "Linker Settings" tab
In "Link libraries" panel, click "Add"
Write this in the coming up field: mingw32;libSDL.a;libSDLmain.a
Go to "Search directories" --> "Linker" tab
Add your MinGW library folders there! Good Luck!!

2

It looks like you're building a Windows application, rather than a console application. Therefore you need a WinMain function as the program entry point rather than a main.

I'm not that familiar with CodeBlocks, but somewhere in the project setting there will be a setting that lets you specify the subsystem your program is running in. It's probably say "Windows", and one of the options should be "Console", so you can change it to that if you really want a console application. Judging by the fact your using cout I suspect a console application is what you want. If it is then you can leave main as your entry point.

You can also opt for a console application when you create a new project. This screenshot and text should help.

NOTE: I've just checked some more, and in the Project/target option you can go to the "Build targets" tab and see the subsystem you're building for. It's called "Type" on the tab, and one of the dropdown options will be "Console application".

1
  • 2
    I did choose Console Application and in target options it says Console Applications.
    – Atom
    Feb 1, 2014 at 18:24
1

This problem appeared to me because the "main.cpp" was not included in the project. If you see that error once again, just make sure you have all the files included in your project.

0

I've fixed this by opening the compiler settings dialog and simply clicking OK. Nothing changed. And it really works!

But if you add a new class again, this problem repeats. So, just open/close settings dialog every time you create a new class.

1
  • Have just tried the advice standing next to mine "Try this: Settings->Compiler, click the tab Build options, select the checkbox" - this flag is neccessary to set Mar 3, 2014 at 18:36
0

its very simple and worked for me ... all you have to do is selecting " open an existing project " that appears in the start tab once you open code blocks and then you choose (your project name ).cbp file ... the program will open your projects as well as the class name .h and .cpp files ... you build and run ... and voila your code will work just fine ..

1
  • unfortunately, it has NOT been that simple.
    – asgs
    Nov 7, 2015 at 15:01
0

I met the problems as well before, my solution is to create a project if you have many files which include head file. By that way, when you code #include "Burrito.h" the editor will look for the head file in you project.Especially for Dev-cpp editor.

0

I just had this issue too! The problem was that I put my main function under a specific namespace while my classes were not part of this namespace and looked like I have no "main" for them. As soon as I removed the namespace/added the classes to the same namespace the build issue was gone.

-1

Try this: Settings->Compiler, click the tab Build options, select the checkbox

Explicitly add currently compiling file's directory to compiler search dirs

i did this and i works perfect now

-1

Fix: undefined reference to `WinMain@16' or undefined reference to 'class-name::class-name()' in codeblocks Error in CodeBlocks

I had same problem. Actually it was very easy to fix. On Menu bar go to

project --> build option Put check mark on Have g++ follow the C++11 ISO C++ language standard [-std=c++11]
then click ok and build and run your project [F9] . click here to see screen capture image. Separate Class files code blocks fix

Not the answer you're looking for? Browse other questions tagged or ask your own question.