Undefined reference to `WinMain@16'

I have been trying out classes on different files now, but I don't seem able to compile it. Here are the files.

Classes.cpp (the main file)
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "Burrito.h"
using namespace std;


int main () {

    cout << "\nThis class is in the header/implementation files\n";
    Burrito bu;

    cin.get();
    return 0;
}


Burrito.h (Header file)
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef BURRITO_H
#define BURRITO_H


class Burrito
{
    public:
        Burrito();
    private:
        int theName
};

#endif // BURRITO_H 

(I am still not sure what the #ifnder BURRITO_H and #define BURRITO_H lines do, they came with the Classes template from Code::Blocks)

And Burrito.cpp (Implementation file)
1
2
3
4
5
6
7
8
9
#include "Burrito.h"
#include <iostream>

using namespace std;

Burrito::Burrito()
{

}


It is a really basic, empty program. But every time I try to compile it it gives me the same error "undefined reference to `WinMain@16'"

I am using Code::Blocks 12.11

Thank you for your help.
Last edited on
closed account (3qX21hU5)
(I am still not sure what the #ifnder BURRITO_H and #define BURRITO_H lines do, they came with the Classes template from Code::Blocks)


These are called include guards. Basically they make it so that the header file can't be included more then once in a program (which would cause problems).

Lets say you have these three files Burrito.h, Taco.h and Chips.h.

Now lets say Taco.h is the first header file to be loaded in your program and Taco.h includes Burrito.h in its file like this.

Taco.h
1
2
3
#include "Burrito.h"

// Other stuff below here 


Then after Taco.h is loaded the next file to load is Chips.h and it also includes Burrito.h like so

Chips.h
1
2
3
#include "Burrito.h"

// Other stuff 


Now if we didn't have header guard on Burrito.h it would try and load the header file twice in the same program which would cause all kinds of errors. But since we did include header guards Taco.h is the first one to be called so it loads the Burrito.h header. Then when Chips.h is called it checks to see if the Burrito.h header is already load and since it is it skips loading it again.

To sum it up always use include guards in your header files and if you want a more in depth reading on the subject you can google it or this thread might help http://stackoverflow.com/questions/8020113/c-include-guards






As for you error code it seems like you are trying to compile a console program as a win32 GUI program.

Since you are on codeblock follow these steps to check and make sure.

1) Click on Project then Properties.

2) Click on the build targets tab.

3) Where it says "type" make sure it says console and not GUI. If it does say GUI change it so that it says console.

4) On the left where it says build targets select release (If you are in debug mode otherwise select debug if you are in release mode) and then repeat steps 1-3.
Last edited on
It is a really basic, empty program. But every time I try to compile it it gives me the same error "undefined reference to `WinMain@16'"


Your project is set up to be a WinAPI program (ie, one with actual windows) rather than a console program. WinAPI programs have a different entry point called WinMain. If you are using the regular main(), you probably meant to have a console program.

I'm not exactly sure where this setting is in C::B... but go in your project settings and look around for an option in the linker settings that specifies the build target, or what kind of program is generated, or something like that. Switch it to a console program. Then rebuild.
Thank you @Zereo and @Disch

The instructions that Zereo gave me was correct, that fixed my problem. And thanks for the explanation on Include Guards.
Topic archived. No new replies allowed.