Atom
Atom

Reputation: 241

C++ undefined reference to WinMain@16 (Code::Blocks)

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!

Upvotes: 9

Views: 103503

Answers (11)

Beatrice
Beatrice

Reputation: 1

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.

Upvotes: 0

Revnic Robert-Nick
Revnic Robert-Nick

Reputation: 627

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.

Upvotes: 1

Wahid
Wahid

Reputation: 149

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

Upvotes: -1

chirag
chirag

Reputation: 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

Upvotes: -1

Saleh
Saleh

Reputation: 31

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!!

Upvotes: 3

user3567012
user3567012

Reputation: 23

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.

Upvotes: 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 ..

Upvotes: 0

Milo Lu
Milo Lu

Reputation: 3366

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

Upvotes: 9

user3308043
user3308043

Reputation: 827

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.

Upvotes: 11

Sergey Dolgopolov
Sergey Dolgopolov

Reputation: 119

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.

Upvotes: 0

Sean
Sean

Reputation: 62532

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".

Upvotes: 3

Related Questions