SFML linker error: unresolved external symbol _WinMain#16, Visual Studio 2012 - c++

I was able to get tutorial #1 to compile fine. But I can't get the 2nd one to compile.
When you do new -> Project, maybe one of those settings are interfering? Pretty sure I did empty project, else console.
What's wrong?
compile error:
Error 1 error LNK2019: unresolved external symbol _WinMain#16 referenced in function
___tmainCRTStartup C:\...\02-videomode-iterate\MSVCRTD.lib(crtexew.obj) 02-videomode-iterate
Error 2 error LNK1120: 1 unresolved externals C:\...\Debug\02-videomode-iterate.exe 02-videomode-iterate
entire source:
#include <SFML/Window.hpp>
int main()
{
sf::Window App(sf::VideoMode(800, 600, 32), "SFML-tut: 02");
bool Running = true;
while (Running)
{
App.Display();
}
return EXIT_SUCCESS;
}
Project settings:
include dir, lib: dir set correctly.
c++ -> preprocessor -> preprocessor definitions:
SFML_DYNAMIC
linker -> input
tried: sfml-window.lib and sfml-window-d.lib ( visual studio seems to always use debug mode at start? but tutorial #1 only worked when I didn't use -d version.
subsystem:
/SUBSYSTEM:WINDOWS

When you set the /SUBSYSTEM:WINDOWS flag, the linker will look for a WinMain function rather than the conventional main. You have two options:
Change to /SUBSYSTEM:CONSOLE. You will get an annoying (or perhaps helpful) console window, which you can get rid of with FreeConsole.
Change main to WinMain with the following signature:
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
);
Unless you need to access argc and argv, this change shouldn't cause too much trouble.
Edit: Perhaps this is worth a look too (copied from the second tutorial):
Under Windows operating systems, you may have created a "Windows
Application" project, especially if don't want the console to show up.
In such case, to avoid replacing main by WinMain, you can link with
SFML_Main static library and keep a standard and portable main entry
point.
So, I suppose that boils down to adding sfml-main.lib (or similar) to the list of libraries to link with.

Related

What does creating a Solution using a template in Visual Studio actually do versus an empty project?

I am following the Intro to C tutorial by Molly Rocket but am running into an issue running the following code in the Debugger in VisualStudio 2019 while using the Empty Project option.
I am using the Empty Project option instead of loading a template because that is what Molly used, while I could just load a template and run the code there instead I would like to know why I am getting this error so I can better understand Visual Studio and C++.
#include <Windows.h>
void learnC(void) {
OutputDebugStringA("Test\n");
}
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
learnC();
}
Recieving the error LNK2019 which doesn't help at all because it just a generic catch all error code.
error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
I have tried updating the compatiliblity settings of VS2019 as well as other settins that people suggested to use but the error still remains.
Again, when I created the Solution I used the blank document instead of a template becuase that is what Molly Rocket used when writing this code.
When I do use a template (I used the Windows Desktop Application template) and delete all the code and paste in what I had in my original document I do not recieve the Error, and I would like to know why.
Thank you for your time!
When you create an Empty Project, the value of SubSystem defaults to Console.
According to the Doc
An application that uses /SUBSYSTEM:CONSOLE; calls main (or wmain)
An application that uses /SUBSYSTEM:WINDOWS; calls WinMain (or
wWinMain), which must be defined to use __stdcall
As far as I'm concerned you should Set up SubSystem to windows(Properties -> Linker -> System -> SubSystem)
For more details about /SUBSYSTEM (Specify Subsystem), I suggest you could refer to the link: https://learn.microsoft.com/en-us/cpp/build/reference/subsystem-specify-subsystem?view=vs-2019

How do I set WinMain as entry point?

I've deleted the _tmain() method which the IDE generated because I find no sense having two entry points after adding my WinMainentry. And yes, this is my first ever C++ application and I am a newbie, but please be nice.
So that's all I got:
// Included headers:
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
// Shortened namespaces:
using namespace std;
// The main entry of the application:
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
MessageBox( NULL, L"Hello World!", L"Just another Hello World program!", MB_ICONEXCLAMATION | MB_OK );
return 0;
}
// End of file.
When I try to build and run I get this error:
error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
error LNK1120: 1 unresolved externals
I realise the entry point is missing, But where can I set WinMain as the entry point? I just took a look in the properties of the project itself and found nothing.
Note that I've started the project as a console application, But now I am trying to turn it into a regualr windows application.
Thanks.
You need to change the subsystem to Windows.
Project->Properties->Linker->System change Subsystem to /SUBSYSTEM:WINDOWS
If you have further trouble with it then start all over again, selecting Win32 Project instead of Win32 Console.
It sounds you are trying to build a console application with code that you imported from a windows application.
Console applications use a main (or _tmain) entry point, whereas windows applications use a WinMain (or _tWinMain) entry point.
Edit: Indeed changing the linker option as Benjamin told you will solve your immediate problem but you are likely to meet other issues later with such hybrid projects. For example you may include some code relying on the _CONSOLE preprocessor symbol.
You can of course adjust this latter setting yourself too but better restart from a clean win32 template as Scott and Hans suggested.

Error LNK2019: unresolved external symbol _wWinMain#16 referenced in function ___tmainCRTStartup

I have started with DirectX 11 and get these compilation errors:
Error 1 error LNK2019: unresolved external symbol wWinMain#16 referenced in function __tmainCRTStartup
Error 2 error LNK1120: 1 unresolved externals(I figure that this error is because of the error just above this one)
I have searched around for fixes to this problem for quite a while, yet I cannot find any fixes to this problem on this site or any other sites via google.
I am using Visual Studio 2012 and c++ for this project. To make my testing project I created an empty project via New Project->Visual C++->Empty Project.
I am using the standard compiler for this project.
This is my wWinMain header in my main.cpp file:
int wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
 LPWSTR cmdLine, int cmdShow)
IntelliSense also keeps throwing up "4 IntelliSense: '#' not expected here" errors on some include lines at the start of the file, these lines: (EDIT: the errors keep disappearing and reappearing, repeatedly)
#include <Windows.h>
#include <memory>
#include "BlankDemo.h"
#include <tchar.h>
I put that in my post as I thought that this may be effected by, or have something to do with the error, it could just be the include files. BlankDemo.h is the header file for a test demo that just produces a blank DirectX 11 window.
All the code I have used is from a book on DirectX; as I am not used to DirectX yet, I have tried a number of fixes and none seem to get rid of this error, the fixes being:
Going to Properties->Configuration Properties->General->Character Set and changing that to "Use Unicode Character Set".
After changing the character set the error still remains.
Going to Properties->Linker->System and changing SubSystem to Windows (/SUBSYSTEM:WINDOWS).
I have also tried changing this to Console (/SUBSYSTEM:CONSOLE), neither of these changes seem to fix the problem.
I have also gone to Properties->Linker->Command Line and added /ENTRY:"wWinMainCRTStartup" to "Additional Options", this does not fix the problem either.
I have still left changes to the project as detailed above in the project. I have only put in the few lines of code as the errors seem to be about the code I have put in this post, also when I copy and paste the code, it does not seem to format correctly, but please tell me if you need to see more code.
EDIT: I have changed the function to int WINAPI __stdcall wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
 LPWSTR cmdLine, int cmdShow)
Even using __stdcall or WINAPI in the function name does not work it seems. At least, in the way I have used them, please tell me if this is incorrect.
Sorry guys, it seems that I made mistakes here, as in; I did not use Unicode to start off with, under Properties->Configuration Properties->General->Character Set. It seems that by using the Multi-Byte Character set to start off with, this confused Visual Studio 2012 into thinking that I was using the Multi-Byte Character set instead. This caused it to throw up the error, even though the entry point under Properties->Linker->Advanced was set to wWinMainCRTStartup. Makes sense, as the characters were not recognised properly.

Win32 Application LNK2001: unresolved external symbol _main

I have been trying to make a WIN32 Application with a GUI (NOT a simple console application) on VS2012, and I'm stuck with this one error. My project is on Release configuration. My main function looks like this:
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
}
What I've tried:
1) Creating a new WIN32 Project with these settings:
Application Type -> Windows application
Additional options -> Empty Project (With NO SDL!)
No other options checked
2) Changing between Unicode and Multi-Byte Character Sets
3) Changing the SubSystem (from Windows (/SUBSYSTEM:WINDOWS) to Not Set)
4) Adding entry points (main ; _main ; WinMain)
5) Adding #undef _ATL_MIN_CRT (I'm pretty sure it was disabled anyway, but still did it for extra insurance...)
NONE of these solved my issue. I've searched the Internet and haven't found anyone with a similar problem (creating a WIN32 Application) who has solved their issue.
EDIT: I'm also using Allegro 5 library and I'm building the program with /MT .
Any recommendations are welcome.
The problem is that you're using the wrong calling convention for your main function. As the documentation for Name Decoration explains, a symbol with a leading underscore and no other decorations indicates C calling convention.
The signature for your main function should be:
extern "C" __cdecl void main();
Note that neither the return value nor the argument list are part of the name decoration, since the caller is responsible for cleanup. This means that any function signature can be used to satisfy the linker.
After some searching I've found the answer.
All I needed to do was to define ALLEGRO_NO_MAGIC_MAIN .

How to solve the "unresolved external symbol" link error in visual c++?

I Have the Visual Studio C++ 2008 Express Edition.
I am trying to compile a program but I am getting the following Link Error:
1>MSVCRT.lib(wcrtexew.obj) : error LNK2001: unresolved external symbol _wWinMain#16
What I tried to do:
I found this on google:
For Visual C++ .NET: In the Advanced category of the Linker folder in the Project Properties dialog box, set the Entry Point to wWinMainCRTStartup.
It was intended to work but didn't. How do I compile this app?
The code is stupidly simple:
#include "stdafx.h"
int main( int argc, char ** argv )
{
}
There are multiple ways to solve this:
Create a console application
Change the Subsystem console now in the linker settings ( Project Settings -> Linker -> System -> SubSystem (select Console))
Change the entry point in the linker settings to mainCRTStartup (Project Settings -> Linker -> Advanced -> Entry Point)
Define int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow ); instead of int main(int argc, char const ** argv)
Change the Character Set to Use Unicode Character Set (Project Settings -> General->Character Set )
It looks like when you created your project you selected a GUI (Win32, MFC, etc) program.
Those programs have a WinMain() instead of a main().
What you want is a Console project.
According to similar questions, that error happens when main isn't defined.
Maybe for some reason it's compiling a different file?
This answer suggests that maybe your flags are inconsistent.