undefined reference to `WinMain#16' with a main [duplicate] - c++

This question already has answers here:
undefined reference to `WinMain#16'
(7 answers)
Closed 8 years ago.
I have a really simple shell of of a program. The editor i use is Scite and my compiler is MingW.
The answer to this is that I'm missing a main but i do have a main().
Main.cpp
#include <iostream>
#include "Money.h"
using namespace std;
int main()
{
}
Money.cpp
#include "Money.h"
#include <iostream>
using namespace std;
Money::Money()
{
cout << "test"
}
Money.h
#ifndef MONEY_H
#define MONEY_H
class Money
{
public:
Money();
private:
};
#endif //MONEY_H
Everytime I try to compile Money.cpp it gives me the error
libmingw32.a(main.o):(.text.startup+0xa7): undefined reference to `WinMain#16'
And I'm not sure what's wrong with the program. All the files are in the same directory. I'm fairly new to programming in C++ so if you can give me a very basic answer or fix it be greatly appreciated.

WinMain is the entry point of Windows "Win32" programs.
You are probably using Visual Studio wizard to create your C++ project, but you chose a Windows C++ application. Such an application is expected to have WinMain() as an entry point (the #16 part is name mangling decorations, according to Visual C++ compiler rules), but you haven't provided that in your code.
If you want to build a C++ console-mode application (with the classical standard main() entry point), you may want to choose the Win32 Console Application option when creating a new project with Visual Studio.
E.g. this is a screenshot from Visual Studio 2010:

Related

Undefined reference in opencv with devc++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
I was trying to install opencv with devc++ on my windows system. I installed opencv and then added a new compiler in devc++.
I added the following command in calling the new compiler section
-L"C:\OpenCV\lib" -lcxcore210 -lcv210 -lcvaux210 -lhighgui210 -lml210
I added the follwing command for linker command line
-lcxcore210 -lcv210 -lcvaux210 -lhighgui210 -lml210
I added the corresponding opencv binaries,libraries and includes too and added opencv/bin to environment variables.
However when i try to run follwing opencv code,
#include <iostream>
#include <conio.h>
using namespace std;
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main()
{
cv::Mat img;
getch();
}
It gives the following error.
[Linker error]
C:\Users\user\AppData\Local\Temp\ccGQvgqG.o:filename.cpp:(.text$_ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x3f):
undefined reference to `cv::fastFree(void*)'
Please, do not use OpenCV 2.1.0.
Use OpenCV 2.4.13 or 3+.
As you can read in the documentation:
Prebuilt libraries [...] only works if you are coding with the latest Microsoft Visual Studio IDE.
If you want to use other compiler than Visual C++, you must build OpenCV libraries yourself. A good start is this tutorial. Take care, because they are "long-obsolete and contain inaccurate information".

Instantiating & using c++ class using header & implementation files [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I'm basically trying to instantiate an object from a class in c++ and use one of the member functions. This feels like a pretty standard problem, but all of the solutions I find online are either simple bracket issues, or scope resolution stuff that seems really obvious, or massively complex examples that shroud what's actually going on in over-complexity. I really appreciate anyone that Might be able to help me understand what I'm doing wrong with these files.
The errors I get are
undefined reference to Test::Test()'
undefined reference to Test::msg()'
I have three files, a main, a Test.hpp, and Test.cpp.
main.cpp
#include "Test.hpp"
#include <iostream>
using namespace std;
int main(){
Test var;
var.msg();
return 0;
}
Test.hpp
#ifndef TEST_HPP
#define TEST_HPP
class Test{
public:
Test();
void msg();
};
#endif
Test.cpp
#include "Test.hpp"
#include <iostream>
using namespace std;
Test::Test(){
cout << "instantiated\n\n";
}
void Test::msg(){
cout << "Hello\n\n";
}
Considering you use codeblocks as your IDE just go to: project settings -> project build options -> search directories -> add and locate where your .cpp and .h files are. Then it will ask you if you want to keep this as relative path. Say no.
If you using some other ide its almost the same proccess, just comment me and i will provide you the steps.
Btw there is no need to include iostream in main since you have already included it in test.

why the error "undefined reference to `_imp__pthread_create" in IDE? [duplicate]

This question already has an answer here:
How do I fix undefined reference to _imp__*?
(1 answer)
Closed 9 years ago.
I got two error's undefined reference to _imp__pthread_create and undefined reference to _imp__pthread_join in Dev C++ in windows IDE, I am using dev c++ 5.4.1. Why this error? How can I resolve this? Could somebody shed some light? Thank you verymuch for your time!!
#include<iostream>
#include<pthread.h>
using namespace std;
void *uss_thread(void*){
cout<<"hello";
}
main()
{
pthread_t tid1;
pthread_create(&tid1, NULL, &uss_thread, NULL);
pthread_join(tid1,NULL);
}
Because you didn't link with lib pthread.
Download DevPack
Install it in Dev C++
Create new Project in Dev C++
After that go to Project menu -> Project Option -> In that select
"Parameter Tab"
Select "add Library or object" option
SelectIt "libpthreadGC2.a" file from installation directory of Dev c++ : It will be in LIB directory.
Press Ok
Good Lucks !

Visual C++ can't open include file 'iostream'

I am new to C++. I just started! I tried a code on Visual C++ 2010 Express version, but I got the following code error message.
------ Build started: Project: abc, Configuration: Debug Win32 ------
ugo.cpp
c:\users\castle\documents\visual studio 2010\projects\abc\abc\ugo.cpp(3): fatal error C1083: Cannot open include file: 'iostream': No such file or directory
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This is the code:
// first.cpp -- displays a message
#include <iostream> // A PREPROCESSOR directive
int main(void) // Function header
{ // Start of a function body
using namespace std;
cout << "Come up and C++ me sometime.\n"; // Message
// Start a new line
cout << "Here is the total: 1000.00\n";
cout << "Here we go!\n";
return 0;
}
Replace
#include <iostream.h>
with
using namespace std;
#include <iostream>
Some things that you should check:
Check the include folder in your version of Visual Studio (in "C:\Program Files\Microsoft Visual Studio xx.x\VC\include", check for the file which you are including, iostream, make sure it's there).
Check your projects Include Directories in <Project Name> → Properties → Configuration Properties → VC++ Directories → Include Directories (it should look like this: $(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSdkDir)include;$(FrameworkSDKDir)\include;)
Make sure that you selected the correct project for this code
(menu File → New → Project → Visual C++ → Win32 Console Application)
Make sure that you don't have <iostream.h> anywhere in your code files, Visual Studio doesn't support that (in the same project, check your other code files, .cpp and .h files for <iostream.h> and remove it).
Make sure that you don't have more than one main() function in your project code files (*in the same project, check your other code files, .cpp and .h files for the* main()` function and remove it or replace it with another name).
Some things you could try building with:
Exclude using namespace std; from your main() function and put it after the include directive.
Use std::cout without using namespace std;.
I had this exact same problem in Visual Studio 2015. It looks like as of Visual Studio 2010 and later you need to include #include "stdafx.h" in all your projects.
#include "stdafx.h"
#include <iostream>
using namespace std;
The above worked for me. The below did not:
#include <iostream>
using namespace std;
This also failed:
#include <iostream>
using namespace std;
#include "stdafx.h"
You are more than likely missing $(IncludePath) within Properties → VC++ Directories → Include Directories.
Adding this should make iostream and others visible again. You probably deleted it by mistake while setting up your program.
If your include directories are referenced correctly in the VC++ project property sheet → Configuration Properties → VC++ directories → Include directories, the path is referenced in the macro $(VC_IncludePath).
In my Visual Studio 2015 this evaluates to:
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include"
using namespace std;
#include <iostream>
That did it for me.
It is possible that your compiler and the resources installed around it were somehow incomplete. I recommend re-installing your compiler: it should work after that.
I got this error when I created an 'Empty' console application in Visual Studio 2015. I recreated the application, leaving the 'Empty' box unchecked. It added all of the necessary libraries.
Make sure you have Desktop Development with C++ installed.
I was experiencing the same problem, because I only had Universal Windows Platform Development installed.
Microsoft Visual Studio is funny. When you're using the installer, you must checkbox a lot of options to bypass the .NET framework (somewhat) to make more C++ instead of C# applications, such as the CLR options under desktop development... in the Visual Studio installer.... the difference is the C++ Win32 console project or a C++ CLR console project.
So what’s the difference? Well, I'm not going to list all of the files CLR includes, but since most good C++ kernels are in Linux... So CLR allows you to bypass a lot of the Windows .NET framework because Visual Studio was really meant for you to make applications in C#.
Here’s a C++ Win32 console project!
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
return 0;
}
Now here’s a C++ CLR console project!
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine("Hello, World!");
return 0;
}
Both programs do the same thing .... the CLR just looks more frameworked class overloading methodology, so Microsoft can great its own vast library you should familiarize yourself with if so inclined.
Keywords (C++)
Other things you'll learn from debugging to add for error avoidance:
#ifdef _MRC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
If you created an environment variable with the name IncludePath, try renaming it to something else.
This name will override $(IncludePath) inside project properties.
Quick fix for small programs:
Add: #include <cstdlib>
In my case, my Visual Studio 2015 installed without selecting C++ package, and Visual Studio 2017 is installed with the C++ package. If I use Visual Studio 2015, opening a C++ project will show this error, and using Visual Studio 2017 will be no error.
I had this problem too. I used this code (before main();) in Visual Studio 2022, and it turned OK:
#include "pch.h"
#include <iostream>
using namespace std;
using namespace winrt;
using namespace Windows::Foundation;
In my case, the error occurred when I created a file in VS Code, without giving the .cpp extension. It resolved when I renamed it with the .cpp.
// first.cpp -- displays a message
#include <iostream> // a PREPROCESSOR directive
using namesapce std;
int main() // function header
{ // start of a function body
///using namespace std;
cout << "Come up and C++ me sometime.\n"; // message
// start a new line
cout << "Here is the total: 1000.00\n";
cout << "Here we go!\n";
return 0;
}

Visual studio for c++?

I have installed visual studio express 2010 c++. However when trying to follow a beginners book and make the hello world program, visual studio opens up a project with this in it:
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
return 0;
}
The thing is, this looks nothing like the c++ in the book i have just started reading (C++ Without Fear). In fact, if i type 'cout',even after entering using namespace std, cout is not found.
This is the books example:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout << "Never fear, C++ is here!";
return 0;
}
Am i missing something here?
Create Windows (or Win32, don't remember exactly) C++ Console Application project, don't select C++/CLI project type.
C++/CLI is very special language, which is used only for interoperability between managed and unmanaged code. It is better to forget about C+++/CLI at this stage...
As Alex said start with C++ Win32 console project, but select empty project so that the IDE don't fill things out automatically! Go to Source Files in your Solution Explorer, right click on it and add new item and choose C++ file (call it main.cpp for example).
At that point you should be ready to go.
Try this sample code that I prepared for you...
#include <iostream>
using namespace std;
int main(char *argv[], int argc) {
cout << "Hello World!" << endl;
return 0;
}
It should print out Hello World! in the console.
You want to start with a Visual C++ Win32 Console Application.
If you want to create your own files completely (i.e. no stub file with a main defined), you can check "Empty Project" in the options of the Win32 Application Wizard.
This is not C++. This is so called "managed" C++. Basically, a totally different language by Microsoft that is compatible with their .NET platform. For example, you can mix C# code and managed C++ code in single binary. This technology is very Microsoft specific and is not portable to any other compiler/OS (even Mono, which is C# for Linux, doesn't have it). See Managed Extensions for C++ for more details.