Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to WinMain@16 C++ [duplicate]

Tags:

c++

codeblocks

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!

like image 922
Atom Avatar asked Feb 01 '14 16:02

Atom


People also ask

How do you fix a undefined reference in C++?

When we compile these files separately, the first file gives “undefined reference” for the print function, while the second file gives “undefined reference” for the main function. The way to resolve this error is to compile both the files simultaneously (For example, by using g++).

What does undefined reference to WinMain mean?

The error means that the compiler try to find WinMain() but it didn't. Either you didn't declare int main() Or your project type is something other than Console. ( Normally Win32GUI) 20th October 2020, 3:52 AM.

How do you fix undefined references to Main?

To fix this error, correct the spelling of the main() function.

What is undefined reference?

The Undefined reference is one of those exceptions that occurred mostly in the C language, as the name suggests that it would occur when some function definition is missing from the script.


2 Answers

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.

like image 181
user3308043 Avatar answered Oct 10 '22 09:10

user3308043


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

like image 42
Milo Lu Avatar answered Oct 10 '22 08:10

Milo Lu