C++ 对 WinMain@16 的未定义引用(代码::块)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21500724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ undefined reference to WinMain@16 (Code::Blocks)
提问by Atom
I'm learning C++ with Code::Blocks, and everytime i try to create a new class, I get an error message saying:
我正在使用 Code::Blocks 学习 C++,每次我尝试创建一个新类时,都会收到一条错误消息:
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):
石灰类(.ccp):
#include "Lime.h"
#include <iostream>
using namespace std;
Lime::Lime()
{
cout<<"Hi!";
}
Lime Header (.h):
石灰标题 (.h):
#ifndef LIME_H
#define LIME_H
class Lime
{
public:
Lime();
};
#endif
If someone knows, how to fix it, please, tell me!
如果有人知道,如何解决它,请告诉我!
回答by user3308043
I just had the exactsame problem working with the exactsame 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.
如何解决这个问题?我发现重新启动 CodeBlocks 可以消除这个错误。它与您创建文件的方式或任何语法无关。重新启动可以解决问题。
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.
为什么会出现这种情况?如果我不得不胡乱猜测,我会认为 CodeBlocks 确实创建了头文件/cpp 文件,但是它并没有以适当的方式将它们链接到您的项目以使其可用(尽管它确实要求您将它们链接到创建它们后的项目)。这是一个猜测。
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.
我知道有些人对此发表了评论,说您正在创建 Windows GUI 控制台应用程序而不是控制台应用程序,但事实并非如此。我也在创建一个简单的控制台应用程序,正如 Bucky 在视频中所解释的那样。
回答by Milo Lu
Try this:
Settings
->Compiler
, click the tab Build options
, select the checkbox
试试这个:
Settings
-> Compiler
,单击选项卡Build options
,选中复选框
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
下次创建新类时,请File policy
确保在该部分中选中复选框
Add path to project
Header and implementation file shall be in same folder
添加项目路径
头文件和实现文件应该在同一个文件夹中
However, do NOTselect
但是,不要选择
Use relative path
使用相对路径
回答by Saleh
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!!
转到"Project" --> "Build Options . . ."
菜单栏
转到"Linker Settings"
选项卡
在"Link libraries"
面板中,单击"Add"
在即将出现的字段中写入此内容: mingw32;libSDL.a;libSDLmain.a
转到"Search directories" --> "Linker"
选项卡
在那里添加您的 MinGW 库文件夹!祝你好运!!
回答by Sean
It looks like you're building a Windows application, rather than a console application. Therefore you need a WinMainfunction as the program entry point rather than a main
.
看起来您正在构建 Windows 应用程序,而不是控制台应用程序。因此,您需要一个WinMain函数作为程序入口点,而不是一个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.
我对 CodeBlocks 不是很熟悉,但是在项目设置的某个地方会有一个设置,让你指定你的程序正在运行的子系统。它可能是“Windows”,其中一个选项应该是“控制台”,所以如果你真的想要一个控制台应用程序,你可以改变它。从您使用的事实来看,cout
我怀疑控制台应用程序正是您想要的。如果是,那么您可以离开main
作为切入点。
You can also opt for a console application when you create a new project. This screenshotand 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".
注意:我刚刚检查了更多内容,Project/target option
您可以在“构建目标”选项卡中查看您正在构建的子系统。它在选项卡上称为“类型”,下拉选项之一将是“控制台应用程序”。
回答by Sergey Dolgopolov
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.
但是如果你再次添加一个新的类,这个问题就会重复。因此,每次创建新类时只需打开/关闭设置对话框。
回答by Sherif Osama el demairy
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 ..
它非常简单并且对我有用......你所要做的就是选择“打开现有项目”,一旦你打开代码块,它就会出现在开始选项卡中,然后你选择(你的项目名称).cbp文件......程序将打开你的项目以及类名 .h 和 .cpp 文件......你构建并运行......你的代码将正常工作......
回答by user3567012
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.
我之前也遇到过这些问题,我的解决方案是如果您有很多包含头文件的文件,则创建一个项目。通过这种方式,当您编写#include "Burrito.h" 时,编辑器将在您的项目中查找头文件。特别是对于 Dev-cpp 编辑器。
回答by Revnic Robert-Nick
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.
这个问题出现在我看来是因为“main.cpp”没有包含在项目中。如果您再次看到该错误,只需确保您的项目中包含所有文件。
回答by Beatrice
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.
我也刚遇到这个问题!问题是我把我的主函数放在一个特定的命名空间下,而我的类不是这个命名空间的一部分,看起来我没有“主”给它们。一旦我删除了命名空间/将类添加到同一个命名空间,构建问题就消失了。
回答by Jawaad Asim
I was having same problem. I wrote intmain() Space was missing only.
我遇到了同样的问题。我写了 intmain() 只缺少空间。