C++ 对 WinMain@16 的未定义引用(代码块)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20020399/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:52:11  来源:igfitidea点击:

undefined reference to WinMain@16 (codeblocks)

c++windowscodeblocks

提问by Jefree Sujit

When I compile my secrypt.cpp program, my compiler shows the error "undefined reference to WinMain@16". my code is as follows

当我编译我的 secrypt.cpp 程序时,我的编译器显示错误“ undefined reference to WinMain@16”。我的代码如下

secrypt.h :

密码.h :

#ifndef SECRYPT_H
#define SECRYPT_H

void jRegister();

#endif

secrypt.cpp :

secrypt.cpp :

#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include "secrypt.h"

using namespace std;

void jRegister()
{
    ofstream outRegister( "useraccount.dat", ios::out );
    if ( !outRegister    ) {
    cerr << "File could not be opened" << endl;
    exit( 1 );}
    string a,b,c,d;
    cout<<"enter your username :";
    cin>>a;
    cout<<"enter your password :";
    cin>>b;
    outRegister<<a<<' '<<b<<endl;
    cout<<"your account has been created";

}

trial.cpp

试用版

#include<iostream>
#include "secrypt.h"

using namespace std;

int main()
{
    void jRegister();

    return 0;
}

Here is the image of my error: errorimage

这是我的错误图像: errorimage

When I compile my trial.cpp program, it compiles and opens the console, but didn't calls the function. Here is the image of the console screen of trial.cpp program . o/p screenCan anyone help me solving this?

当我编译我的trial.cpp 程序时,它编译并打开控制台,但没有调用该函数。这是trial.cpp 程序的控制台屏幕的图像。 o/p screen谁能帮我解决这个问题?

回答by chris

When there's no project, Code::Blocks only compiles and links the current file. That file, from your picture, is secrypt.cpp, which does not have a main function. In order to compile and link both source files, you'll need to do it manually or add them to the same project.

当没有项目时,Code::Blocks 只编译和链接当前文件。从您的图片来看,该文件是secrypt.cpp,它没有主要功能。为了编译和链接这两个源文件,您需要手动完成或将它们添加到同一个项目中。

Contrary to what others are saying, using a Windows subsystem with mainwill still work, but there will be no console window.

与其他人所说的相反,使用 Windows 子系统main仍然可以工作,但不会有控制台窗口。

Your other attempt, compiling and linking just trial.cpp, never links secrypt.cpp. This would normally result in an undefined reference to jRegister(), but you've declared the function inside maininstead of calling it. Change mainto:

您的其他尝试,只是编译和链接trial.cpp,从不链接secrypt.cpp。这通常会导致对 的未定义引用jRegister(),但您已经在内部声明了该函数main而不是调用它。更改main为:

int main()
{
    jRegister();

    return 0;
}

回答by Sourab Reddy

Well I know this answer is not an experienced programmer's approach and of an Old It consultant , but it worked for me .

好吧,我知道这个答案不是经验丰富的程序员的方法,也不是 Old It 顾问的方法,但它对我有用。

the answer is "TRY TURNING IT ON AND OFF" . restart codeblocks and it works well reminds me of the 2006 comedy show It Crowd .

答案是“尝试打开和关闭它”。重新启动代码块,它运行良好让我想起了 2006 年的喜剧节目 It Crowd。

回答by shanmuga

I was interested in setting up graphics for Code Blocks when I ran into a this error: (took me 2 hrs to solve it)

当我遇到这个错误时,我对为代码块设置图形很感兴趣:(花了我 2 小时来解决它)

I guess you need to have a bit of luck with this. In my case i just changed the order of contents in Settings menu->Compiler and Debugger->Global compiler settings->Linker settings->Other Linker Options: The working sequence is: -lmingw32 -lSDL -lSDLmain

我想你需要有一点运气。就我而言,我只是更改了设置菜单->编译器和调试器->全局编译器设置->链接器设置->其他链接器选项中的内容顺序:工作顺序是:-lmingw32 -lSDL -lSDLmain

回答by Towsif Ahmed Labib

Open the project you want to add it.

打开要添加的项目。

Right click on the name. Then select, add in the active project. Then the cppfile will get its link to cbp.

右键单击名称。然后选择,添加到活动项目中。然后该cpp文件将获得其链接到cbp.

回答by Mockingjay

  1. You need to open the project file of your program and it should appear on Management panel.

  2. Right click on the project file, then select add file. You should add the 3 source code (secrypt.h, secrypt.cpp, and the trial.cpp)

  3. Compile and enjoy. Hope, I could help you.

  1. 您需要打开程序的项目文件,它应该出现在管理面板上。

  2. 右键单击项目文件,然后选择添加文件。您应该添加 3 个源代码(secrypt.h、secrypt.cpp 和 trial.cpp)

  3. 编译并享受。希望,我可以帮助你。

回答by nullptr

You should create a new project in Code::Blocks, and make sure it's 'Console Application'.

您应该在 Code::Blocks 中创建一个新项目,并确保它是“控制台应用程序”。

Add your .cpp files into the project so they are all compiled and linked together.

将您的 .cpp 文件添加到项目中,以便将它们全部编译并链接在一起。

回答by James

I had the same error problem using Code Blocks rev 13.12. I may be wrong here since I am less than a beginner :)

我在使用 Code Blocks rev 13.12 时遇到了同样的错误问题。我可能是错的,因为我还不是初学者:)

My problem was that I accidentally capitalized "M" in Main() instead of ALL lowercase = main() - once corrected, it worked!!!

我的问题是我不小心在 Main() 中将“M”大写而不是全部小写 = main() - 一旦更正,它就起作用了!!!

I noticed that you have "int main()" instead of "main()". Is this the problem, or is it supposed to be that way?

我注意到你有“int main()”而不是“main()”。这是问题,还是应该是这样?

Hope I could help...

希望我能帮上忙...