8

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.

Community
  • 1
  • 1
David von Tamar
  • 797
  • 3
  • 12
  • 29
  • How can you "see no sense" when you've never done this before ? The answer is, by the way, you can't -- because that would make no sense :) – Ernest Friedman-Hill Apr 25 '14 at 00:35
  • What compiler and IDE are you using? – Benjamin Lindley Apr 25 '14 at 00:43
  • @BenjaminLindley, I am using VS2013. – David von Tamar Apr 25 '14 at 00:45
  • 1
    IIRC, `WinMain` isn't the actual entry point from the operating system's point of view. It's called by the runtime environment (or RT, as in CRT), which needs to be initialized (or started up, as in Startup). So, yeah, that `_tmain` is there for a reason. It's the actual entry point. `WinMain` is only an entry point 'by convention'. –  Apr 25 '14 at 00:49
  • 3
    The simplest way to get all the settings correct is by using the Win32 Project template instead of the Win32 Console Application template. Next best thing after you got started wrong is Project + Properties, Linker, System, change the SubSystem setting to "Windows". – Hans Passant Apr 25 '14 at 00:52
  • @Rhymoid, Okay, I brought back the `_tmain` method and everything works fine, But I really have no idea where from to get the parameters for `WinMain`, So I just call it from `_tmain` with NULLs. Is that really supposed to be like that? – David von Tamar Apr 25 '14 at 00:53
  • 1
    I guess I misunderstood what you misunderstood. Like the others are saying, use the proper template and you'll be fine. –  Apr 25 '14 at 00:55

3 Answers3

15

You need to change the subsystem to Windows.

enter image description here

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
1

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.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
0

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.

luciole75w
  • 1,109
  • 6
  • 12