Jefree Sujit
Jefree Sujit

Reputation: 1586

undefined reference to WinMain@16 (codeblocks)

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

secrypt.h :

#ifndef SECRYPT_H
#define SECRYPT_H

void jRegister();

#endif

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

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 screen Can anyone help me solving this?

Upvotes: 14

Views: 196287

Answers (11)

Ujjwal Puri
Ujjwal Puri

Reputation: 1

If you're using VS code, then toggle on save before running the code option in the settings. It worked for me.

Upvotes: -1

Ewa
Ewa

Reputation: 19

Saving resolved the issue in my case.

Upvotes: -1

Dastagir Shaikh
Dastagir Shaikh

Reputation: 1

undefined reference to WinMain@16" Visual Studio if you are using Visual Studio Code then 1)Stetting 2)Search for save 3)scroll down and search for code runner 4)check the checkbox 'Save File Before Run 5)Now run

Upvotes: 0

Shubham Rohila
Shubham Rohila

Reputation: 411

Hey I had a similar problem, all the files were in same project but still it did not compile them together. Here is what I did

On the left panel, in the Workspace area you can see your project name and files in it. Right click on the project name and hit rebuild. This alone helped me, rebuild builds your project again from scratch.

Upvotes: 0

Towsif Ahamed Labib
Towsif Ahamed Labib

Reputation: 686

Open the project you want to add it.

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

Upvotes: 0

shanmuga
shanmuga

Reputation: 21

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

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

Upvotes: 2

Sourab Reddy
Sourab Reddy

Reputation: 353

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

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

Upvotes: 2

Mockingjay
Mockingjay

Reputation: 1

  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.

Upvotes: 0

James
James

Reputation: 1

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

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

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

Hope I could help...

Upvotes: -3

Qaz
Qaz

Reputation: 61900

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.

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

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 main instead of calling it. Change main to:

int main()
{
    jRegister();

    return 0;
}

Upvotes: 16

nullptr
nullptr

Reputation: 11058

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

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

Upvotes: 0

Related Questions