undefined reference to WinMain@16 (codeblocks)

Jefree Sujit picture Jefree Sujit · Nov 16, 2013 · Viewed 132.1k times · Source

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?

Answer

chris picture chris · Nov 16, 2013

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;
}