LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup error when using WinMain

Ryan Butt picture Ryan Butt · May 13, 2013 · Viewed 17.8k times · Source

I've been following this tutorial to learn some DirectX programming: http://www.rastertek.com/dx11tut02.html

I'd gotten to the end, tried to build and it threw this error when I compiled:

MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

So, I googled extensively to try to find a solution, things I've tried included:

-Setting entry points as WinMainCRTStartup, wWinMainCRTStartup and mainCRTStartup (probably others too)

-Making sure that subsystem is set to Windows rather than console, even tried setting it as 'console' and as 'not set' just in case.

-Add existing item for the file containing the main method.

-Downloaded the provided code in the tutorial to make sure it wasn't a coding error on my part, still didn't work.

Now the interesting bit:

I tried downloading the tutorial code to my Desktop (which I usually don't have access to, being away) which has a copy of Visual C++ Express 2010 (my laptop is running Visual Studio 2010 Professional). It worked absolutely fine on the desktop, first time, didn't have to change a single setting.

So the problem must be to do with the install of VS on my laptop, or something else to do with my laptop.

I've tried resetting all VS settings to default, uninstalling VS, doing a repair install, installing Visual C++ Express, installing the Windows SDK v7.1, everything I could think of and it still doesn't work.

Just to be clear, the code I'm using for the main is:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow)

Please help, I've spent 2 days messing around with this and not gotten anywhere, I'm on the verge of trying to reinstall Windows completely.

Answer

billmcc picture billmcc · Sep 20, 2013

I ran into a similar problem today.

My main.cpp wasn't being included in the build correctly.

Go to the solution explorer, right click on the file and select "Properties".

Make sure that

  1. the file is included in the build and
  2. the Tool is set to C/C++ compiler tool.