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

    Question

  • Hi,

    I'm new to Visual C++ (or windows programming) so I pick up the Petzold's "Programming Windows, 5th eidtion" (Microsoft Press, 1999) and install Visual C++ 2005 Express & Platform SDK for Windows Server 2003 SP1.

    I create an win32 console application (empty project) and add 'HelloMsg.c" (page 14) to the project. The compilation is OK but the link fails (with the output at the end of this post).

    I'm sure that Visual C++ 2005 Express & Platform SDK are successfully installed since I can create and run a win32 window/console application if the "empty project" option is not selected.

    PS:

    source code in HelloMsg.c

    #include <windows.h>

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        PSTR szCmdLine, int iCmdShow)
    {
      MessageBox(NULL, TEXT("Hello, Windows XP!"), TEXT("HelloMsg"), 0);
     
      return 0;
    }

     

    the link output:

    ------ Build started: Project: HelloMsg, Configuration: Debug Win32 ------
    Linking...
    MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
    D:\Projects\HelloMsg\Debug\HelloMsg.exe : fatal error LNK1120: 1 unresolved externals
    Build log was saved at "
    file://d:\Projects\HelloMsg\HelloMsg\Debug\BuildLog.htm"
    HelloMsg - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

     

    Wednesday, August 02, 2006 1:20 AM

Answers

  • Don't start with the Console Application template, start with the Win32 Project template and select "Windows application".
    Wednesday, August 02, 2006 2:49 PM

All replies

  • Don't start with the Console Application template, start with the Win32 Project template and select "Windows application".
    Wednesday, August 02, 2006 2:49 PM
  • Thanks, it solve my problem.
    Friday, August 04, 2006 12:15 AM
  • I received the same error when building a Windows Console Application and then adding a Windows Form: To solve I added a main function in the auto generated .cpp file and then under properties->Linker Tab->Advanced added "main" as entry point

     

    Other solutions I saw posted were to change the entry point to "WinMainCRTStartup", and to make sure under Properties->Linker tab->System the correct subsystem was listed (i.e. Console for a console application, Windows for a Windows application), and finally some people had selected a console application when making a Windows application during setup (and vice-versa)

    Friday, June 08, 2007 11:12 PM
  • What worked for me is to set

    Properties->Linker->System->SubSystem to "not set" or "inherit from parent" (the latter was an option for me at one time, but now all I get is names of subsystems and "not set").  Caveat: I don't know what this means, but it makes the errors go away.  I thought I knew a lot about programming, but the terminology with VS is mostly new to me.
    Tuesday, June 19, 2007 4:19 AM