问题
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!
回答1:
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.
回答2:
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
回答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!!
回答4:
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".
回答5:
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.
回答6:
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.
回答7:
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 ..
回答8:
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.
回答9:
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.
回答10:
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
回答11:
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
来源:https://stackoverflow.com/questions/21077636/undefined-reference-to-winmain