C++ - undefined reference to `WinMain#16 - c++

I got a .h and .cpp codes on a website and I am trying to compile them. This is the website that contains the codes: http://raphaelcruzeiro.com/blog/implementing-a-sha1-algorithm-in-c/
I am creating a new project, selecting "console application" and adding the .h and the .cpp files to the project, but when I try to compile I always get this error "undefined reference to `WinMain#16".
I tried DevC++ and Codeblocks both of them got the same error.
Please I want all details on how to compile these files, is there a #include to use? If you guys please make a simple step by step I would be very glad.
On this website http://raphaelcruzeiro.com/blog/implementing-a-sha1-algorithm-in-c/ you will find the .h and the .cpp codes.
Thanks in advance

All C++ programs require some sort of main() function, which is where they start executing. The code on the website has no main(), causing the compiler to complain (and rightly so - the program wouldn't know where to start).
You'll need to add something like the following to your .cpp file:
int main(int argc, char **argv) {
/* code to do SHA1 stuff */
return 0;
}

Related

C++ header file in Visual Studio

Okay. So I've got a simple question. If I ask it in the wrong place, please correct me. What I want to ask is, why Visual Studio gives me this:
#include "stdafx.h"
int main()
{
return 0;
}
everytime I create a new project? (I know I can select Empty Project, and add mine .cpp file by myself, but I'm just curious. It says #include <stdio.h> and #include <tchar.h>. So what is it for? Are you all using it or something?
And P.S - why there is no (int argc, char** argv) in main declaration? (on my coding course in college I've learned that there may be _tmain(int argc, _TCHAR* argv), when creating something in VS)
Okay. So I've got a simple question. If I ask it in the wrong place, please correct me. What I want to ask is, why Visual Studio gives me this:
...
Well, it depends a bit on the project type you've been choosing from the wizard. Looks like the standard template for a console project.
#include "stdafx.h"
is prepended for any type of translation unit by the wizard. It supports the precompiled header optimizaton mechanism.
why there is no (int argc, char** argv) in main declaration?
Because the template provides the minimum for a valid main() entry routine definition.
When creating a new Win32 project, Visual Studio automatically adds a precompiled header "stdafx.h" to your project, even if you unchecked the 'Empty Project' checkbox.
If you want to disable this, go to your project configuration properties -> C/C++ -> Precompiled Headers and select 'Not Using Precompiled Headers'.
Working with precompiled headers see: https://stackoverflow.com/a/4726838.
And P.S. look at -> https://stackoverflow.com/a/4207223
The precompiled header is for speeding up compile times by compiling the contents of your most frequently included headers only once and then reusing the compile results. You can change its name if you want.

Why does "cout" keep giving me error C1083?

I was using Visual C++ 6.0 just now, and I keep getting this error:
fatal error C1083: Cannot open include file: 'streambuf': No such file or directory
My code is just a simple hello world program.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
cout<<"Hello World.";
return 1;
}
Then I went and checked my INCLUDE folder and found a file called STREAMBF, but the compiler was looking for STREAMBUF. Notice that the file that is present is missing the U, between the B and the F. This was from a good copy of VC++6.0, directly from the actual CD, not a pirated copy. So there should be all the files needed. But it appears that a file is missing! Is this MS being stupid again, and yet making another big mistake, and forgetting to include an important file on their CDs? I'd hate to think that every single CD for VC++6.0 that was pressed that came out of MS factories had this problem. And I know that it is a missing file, not just a misnamed file, as renaming STREAMBF to STREAMBUF just led to more errors.
Anybody know where I can find a copy of the file STREAMBUF? Or am I just overlooking something here? Is this exact error a known problem with running old copies of VC++ on modern OS's like Windows 7? Is it possible that the only reason that it's looking for STREAMBUF is that this is a newer file associated with Win7, and that if it was running in a different environment (an older OS), it would actually be looking for the correct file, STREAMBF? Can somebody help me here?
Your installation is either broken, deprecated or interpretes your code in wrong way.
You should only use older compiles if you are trying to build project developed entirely for this version.
Try to compile same code with new compiler, if you want to use VS then you should look for Visual Studio Express 2013.
Your code does not have any errors.
Modify your program to, you should be able to see it okay.
#include <iostream.h>
using namespace std;
int main()
{
cout<<"Hello World.";
return 1;
}
However,
your compiler is pretty old. You need to an upgrade.
There are C++ compilers for Windows from Microsoft Express Visual Studios Link and Info VS2013 to
some other non-Microsoft like GCC for Windows.
If you don't have installation access there are some portable c++ compilers.
Finally there are some online compilers for simple test. web based online compilers.
For my win 10 installation of VC 6.0, I had the same problem ... fatal error C1083: Cannot open include file: 'streambuf': No such file or directory
Replacing with <iostream.h> does not solve the problem.
I have checked the header file installation folder (Program files\VS98\VC98\INCLUDE). For some (unknown) reason, some file names have been changed during installation. Restoring the original name has solved the problem, in my case, in example:
Turn STREAMBF into STREAMBUF, STDXCEPT into STDEXCEPT, XCEPTION into EXCEPTION, FCTIONAL into FUNCTIONAL.
Notice: other header file names might be wrong. I have listed above the file names wrong in my installation.
I hope this may help.

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?
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;
}
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 .
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
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.
You need to open the project file of your program and it should appear on Management panel.
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)
Compile and enjoy. Hope, I could help you.
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.
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.
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
Saving resolved the issue in my case.
If you're using VS code, then toggle on save before running the code option in the settings. It worked for me.
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...

undefined reference to function code blocks

main.cpp
#include <iostream>
#include <string>
using namespace std;
void echo(string);
int main()
{
echo("hello");
cout << "Hello world!" << endl;
return 0;
}
print.cpp
#include <iostream>
#include <string>
void echo(string code){
cout << code;
}
After compiling the code in code blocks 12.11, it gives me that error:
undefined reference to `echo(std::string)
I use windows 7 x64.
I have added the directory; Project>build options > search directories and added the current working directory.
All the files are in one console project in code blocks
I believe you should read up a bit more on namespaces usage. You are missing std in print.cpp.
Generally, while starting to learn cpp or getting a grip of the language you should always try writing full names of the classes along with the namespaces. Eventually with practice and some oversights (like now) you will learn why you really need them. In a nutshell namespaces are great:
When you are writing code over multiple files
Compartmentalize your code into separate blocks.
Also, using namespace std; should be used within cpp files mostly (otherwise headers get mangled up.
Anyways, try changing your code to this:
#include <iostream>
#include <string>
void echo(std::string code){
std::cout << code;
}
Then your results will look like this:
> g++ main.cpp print.cpp -o a.out
> ./a.out
helloHello world!
You should get more than that linker error, since you use string without any namespace in your print.cpp file. And if that source file doesn't compile it can't be linked with, and you will get the linker error you have.
Change to e.g.
void echo(std::string code) { ... }
And you do try to link with the object file created from print.cpp ?
I know this is old, but for anyone looking to solve this issue, the following may be a solution for you. If you have g++ follow c++ 11 under project->build options (check your options anyway) then you must check that box for all files you make in the project for the error to be cleared up. I had that annoying undefined reference thing too but now it is gone!
Try "Project/Properties/Build Targets tab". There you should find "Build target files" field. In that filed find "print.cpp" and click the checkbox (now the compiler will build print.cpp).
Some usefull information on Project management in CB
http://www.codeblocks.org/docs/main_codeblocks_en.html
When dealing with strings in C++ its best to sue std::string and your code seems to be wrong with a changes like using std::cout instead of plain cout another thing you need to be careful is linking your files especially files in different directories you need to tell code blocks were to find this print.cpp by going to build option and go for the search tab directory and point to where print.cpp is other wise the other approach is to just build a project which will have the main.cpp and and then add print.cpp class to current project I hope this will be of some help

getting the right compiler for C++

I am trying to learn c++ but most of the tutorials and books I have read or looked up teaches you this...
(I am assuming like most tutorials, they are teaching in the beginning to code either in win32 console or CLR console. In either case the following does not work.)
#include <iostream>
int main( )
{
std::cout << "Hello World\n";
return (0);
}
The IDE that i have is Visual C++ 2008 Express edition and they accept code like this
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Or like this
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
return 0;
}
Honestly I do not no the difference in none of these and I am not sure if I should just download a older compiler so that it works. If someone can tell me what the difference in these are and where to go from there. That will help tremendously. Thanks
[Edited]
I am trying to do a simple hello world. But I get the error "system can not find path specified." I have screenshot that shows what the error looks like. It also is saying that my project is out of date when I clearly save the file before I build it. Apparently it can not find the executable file. I went to the debug fold and did not see any .exe file.
[Edited]
Ok, now When I try to build the project I get the following errors
1>------ Rebuild All started: Project: test, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'test', configuration 'Debug|Win32'
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>test.cpp
1>c:\users\numerical25\desktop\test\test\test.cpp(1) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\numerical25\desktop\test\test\test.cpp(6) : error C2653: 'std' : is not a class or namespace name
1>c:\users\numerical25\desktop\test\test\test.cpp(6) : error C2065: 'cout' : undeclared identifier
1>Build log was saved at "file://c:\Users\numerical25\Desktop\test\test\Debug\BuildLog.htm"
1>test - 2 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Here is the code I used
#include <iostream>
#include "stdafx.h"
int main( )
{
std::cout << "Hello World\n";
return (0);
}
Note: I tried using it with and without the #include "stdafx.h" When I tried it without the #include "stdafx.h", it said I might be missing it.
Not sure what you're asking. The first two examples you gave are valid C++ programs that should (will) compile with VC++. The third example is a C++/CLI program that must be compiled with the /CLR compiler switch (this is called Managed C++).
EDIT: Adding more specific information (from a comment below):
The first two examples are standard (native) C++ (albeit, the second example has MS-proprietary macros). They compile to native code. The third is C++/CLI (a "managed" extension to C++). It compiles to managed (.NET) code. Only the third snippet interacts with the .NET framework in any way. All three are absolutely buildable and runnable using the appropriate projects in VS 2008 (no command line necessary)!
Based on your latest update, it looks like you have probably modified some project properties and changed some paths. The app is building, but when you try to run it via VS (you should do this with <Ctrl>+F5, by the way), the executable cannot be found (there are several ways you could have messed this up by changing or playing with various settings).
Please note the difference between building and running. Building is the process of compiling and linking your source code. Running is launching the resulting executable. You seem to be confused between these (judging from your complaints about the "...out of date" dialog box). It is normal to get the "...out of date" dialog box if you try to run without rebuilding after you have made a change to the project (even if that change is saved). Just make sure you click "yes." You need to build the project before you can run it.
My recommendation is to completely delete your project and solution. Create a new empty project, as suggested elsewhere in this now-very-heavyweight thread, and don't modify any project settings. If this doesn't work, something is seriously wrong!
ANOTHER EDIT: Just for completion, since this question kept changing:
As others have already pointed out, your ultimate problem with the first snippet is the use of precompiled headers (PCH). PCH are turned on by default in new VS C++ projects. Their purpose is to speed compilation when many implementation files include the same set of headers -- preventing the compiler from having to parse the header files for each compilation unit.
You have three options:
(Recommended) Disable PCH -- Project Properties --> Configuration Properties --> C/C++ --> Precompiled Headers: Set Create/Use Precompiled Header to Not Using Precompiled Headers. (You don't need to do anything with the "stdafx.h" file or the #include for it.)
Place your commonly used #includes in "stdafx.h". In your case, you would put #include <iostream> in "stdafx.h".
Place your #includes after `#include "stdafx.h". Microsoft requires that the "stdafx.h" be the first included file in a compilation unit.
A minor point, which I don't see elsewhere in the answers: When using precompiled headers, such as your stdafx.h, you need to include them first. Change it to:
#include "stdafx.h"
#include <iostream>
and that should fix the errors about it.
Alternatively, it may be easier to simply switch off precompiled headers: Project > Properties > Configuration Properties > C/C++ > Precompiled Headers > Switch first option to "Not using precompiled headers". They can be useful for big projects but will just be awkward and annoying while you're learning, since they have extra rules (like this "must be included first") which aren't requirements of standard C++ .
The "difference" is pedantic. The latter are just Microsoft-specific entry points.
As you are learning C++, I recommend you use a compiler, and preferably an operating system that lets you focus on C++, and not the platform. For this I recommend g++, on an Linux distribution such as Ubuntu.
Try this tutorial, there are many others that are similar that quickly let you overcome being tied to the tools, and focus on C++.
int main();
int main(int argc, char* argv[]);
These are standard C++.
int _tmain(int argc, _TCHAR* argv[]);
int wmain(int argc, wchar_t* argv[]);
These are Windows-specific to handle Unicode arguments. See What is the difference between _tmain() and main() in C++?.
int main(array<System::String^>^ args);
This is not C++. This is C++/CLI.
For best portability, always use the first form.
Also,
int main(int argc, char** argv, char** envp);
This is a usually seen POSIX extension. Windows supports this form of main too. The envp means (pointer to) environment variables.
int main(int argc, char** argv, char** envp, char** apple);
This is for Mac only, obviously.
void main();
And this is wrong (nonstandard, some compilers (e.g. gcc) will reject it).
Visual C++ Express will compile the first example just fine.
However, you need to ensure the proper project settings:
Create an "Empty Project"
"Add a new item..." to the project via the "Project" menu. Select C++ (.cpp) file.
Copy/Paste code into new file
Press F5 to compile and run.
When "Project is out of date" dialog appears, press "Yes" (build the project)
The steps above ensure VC++ Express does not treat your file as a special Win32/Windows console application.
EDIT: added additional step 5 to prevent "Can't find..." dialog.
I managed to get the same dialog by making sure the exe file does not exist, and answering "No" to the build dialog. With a clean, empty project the exe file does not exist yet. It must be built first. If you answer "no" don't build it, VC++ dutifully does not build the exe and later complains about not being able to find it when it tries to run it later.
As STingRaySC pointed out, all three of your examples will compile in VC2008 express; it's just that examples 2 and 3 are what VC2008 Express will load up initially when you create a project (one of the examples is for Managed C++, as STingRaySC mentioned).
You can just delete the code in your second example (the C++ Win32 Console Application project) and paste in the more standard hello world program from your first example. It should compile and run just fine in VC2008 Express - it did for me.
I. Precompiled header
#include "stdafx.h"
is some kind of tricky stuff that comes your way.
If you create a project VC will normally switch on precompiled header.
This means that one header stdafx.h is created which is compiled only once.
This is done to speed up compile time in big environments. If you start C++
it will confuse you.
If you use stdafx.h it has to be the first header in the cpp file.
II. Unicode (Utf16)
int _tmain(int argc, _TCHAR* argv[])
Microsoft uses UTF16 to implement unicode strings.
This means you get two versions of main.
int main(int argc, char* argv[])
int main(int argc, wchar_t* argv[])
This is also confusing if you start.
To simply start you can use whatever editor you want.
Create the file.
Open a Visdual studio 2008 command prompt
cl main.cpp
main.exe
and you will see Hello World using code from books.
Afterwards try to understand some of the settings of VC.
But you should always use an empty project.
Else you have to care about stdafx, UNICODE, ...
_tmain with the _TCHAR argv is the way the C runtime allows you to handle unicode. If _UNICODE is defined, then _tmain will expand to wmain, and the _TCHAR argument will be of type wchar_t. If _UNICODE is not defined, then _tmain will expand to main, which will be the ANSI standard.
Therefore, so long as _UNICODE is not defined, the second snippet you posted is compliant with the standard.
Lots of waxing lyrical and some misinformation for you sift through already, but I suggest following wonsungi's advice. But to clarify his advice:
File->New->Project
Select Project Type "Win32", then Template "Win32 Console Project"
Give the project a name and location
OK
Select "Application Settings"
Check "Empty Project"
In the "Solution Explorer", right click the "Sources" folder, then Add->New Item
Type the name of the file, in the "name" box using a .cpp extension (you can ignore the templates if you wish).
Enter your code in the new file.
Woot!! I figured it out!!! Below is my original code
#include <iostream>
int main( )
{
std::cout << "Hello World\n";
return (0);
}
It was missing the header file #include "stdafx.h" . So I had to include it in there so I added it like this
#include <iostream>
#include "stdafx.h"
int main( )
{
std::cout << "Hello World\n";
return (0);
}
I was still getting an error like what you see in my edited question at the bottom. So What I did is I took #include and added it in my header file and then it worked!!!!!
Even the the books and alot of tutorials show to add #include to the actual cpp, for some reason in express edition I had to add it to header for it to work. I don't know WHY but it's a solution and now it works.
Download and install Dev-C++ on your system. If the code doesn't work on Visual C++, try it out on Dev-C++ (which uses the GCC compiler). You may get the same results or a different error message. Whenever you get an error message you don't understand, do a Internet search for the error message.