undefined reference to WinMain@16 (codeblocks)
Asked Answered
S

11

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?

Sauropod answered 16/11, 2013 at 15:48 Comment(7)
You never compile and link trial.cpp.Dulciana
Compile as a console mode program.Pteryla
@Max, It probably is.Dulciana
It looks for a GUI program.it doesn't look up main(), but rather WinMain. Change your project properties to Console ProgramDuckweed
i compiled my trial.cpp ...but it doesnt work .here is the screen shot link of my trial.cpp program. dropbox.com/s/ikocwnb732bp869/…Sauropod
@AlexandreTryHardLeblanc, The picture shows no project. WinMain is not needed even if it is a Win32 GUI project.Dulciana
I was having same issue on VS code. I add g++ to environment variable. you can look for how to add environment variable at https://mcmap.net/q/46645/-how-to-set-the-environment-variables-for-java-in-windowsKaree
D
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;
}
Dulciana answered 16/11, 2013 at 15:55 Comment(3)
i tried as u said... but it showed error. "undefined reference to 'jRegister()'Sauropod
@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.Dulciana
@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.Dulciana
A
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 .

Abrego answered 16/6, 2015 at 6:47 Comment(1)
+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 :)Bettyannbettye
V
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

Vesuvius answered 13/9, 2015 at 13:20 Comment(0)
C
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.

Cruelty answered 16/11, 2013 at 15:49 Comment(0)
P
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.

Planospore answered 23/11, 2014 at 1:49 Comment(0)
T
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.

Therapist answered 3/12, 2018 at 6:15 Comment(0)
E
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.

Eger answered 6/6, 2021 at 7:56 Comment(0)
D
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

Dolhenty answered 31/12, 2021 at 5:59 Comment(0)
G
-1

Saving resolved the issue in my case.

Grandmamma answered 4/6, 2022 at 20:10 Comment(1)
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.Thacher
M
-1

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

Merilee answered 12/1, 2023 at 12:47 Comment(0)
C
-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...

Clinker answered 19/6, 2014 at 17:17 Comment(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.Dulciana

© 2022 - 2024 — McMap. All rights reserved.