14

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?

ʇolɐǝz ǝɥʇ qoq
  • 717
  • 1
  • 15
  • 30
Jefree Sujit
  • 1,546
  • 5
  • 22
  • 38

11 Answers11

16

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;
}
chris
  • 60,560
  • 13
  • 143
  • 205
  • i tried as u said... but it showed error. "undefined reference to 'jRegister()' – Jefree Sujit Nov 16 '13 at 15:57
  • @JefreeSujit, You have to put both source files into the same project in order for them to both link. Alternatively, compile and link them yourself from the command line. That was my first paragraph. – chris Nov 16 '13 at 16:00
  • @JefreeSujit, Click new project, you probably want a console application project, add your existing files to that project, and build the project. More detailed information can be found on the C::B wiki. Detailed information of how to do it manually can be found in the GCC (or whichever compiler you're using) documentation. – chris Nov 16 '13 at 16:04
2

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 .

Sourab Reddy
  • 353
  • 2
  • 14
  • +1 at first sight I thought this was really a dumb thing to say, but eventually for me, restarting codeblocks fixed my problem! should try this before anything else :) – Chanhee Jeong Aug 03 '15 at 07:24
2

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

shanmuga
  • 21
  • 2
0

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.

nullptr
  • 11,008
  • 1
  • 23
  • 18
0
  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.

0

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.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
0

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.

Shubham Rohila
  • 366
  • 2
  • 12
0

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

-1

Saving resolved the issue in my case.

Ewa
  • 19
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 05 '22 at 00:24
-1

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

-3

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...

James
  • 1
  • It's clear from the OP's post that it's `main` with a lowercase 'm'. Also, `main()` without the `int` is just plain wrong **in C++**. It's not recommended in C, either. – chris Jun 19 '14 at 20:01