locked
MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void) RRS feed

  • Question

  • Hello! I have been getting this error message upon building my program : LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ). I have already researched about this error, but I have found little helpful results towards solving it. I have also tested out some changes in the properties, but many lead to the same error or more. Here is the code:

    #include <Windows.h>

    int ScreenWidth, ScreenHeight;
    int Interval = 100;

    LRESULT CALLBACK Melter(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
    switch (Msg)
    {
    case WM_CREATE:
    {
    HDC Desktop = GetDC(HWND_DESKTOP);
    HDC Window = GetDC(hWnd);

    BitBlt(Window, 0, 0, ScreenWidth, ScreenHeight, Desktop, 0, 0, SRCCOPY);
    ReleaseDC(hWnd, Window);
    ReleaseDC(HWND_DESKTOP, Desktop);

    SetTimer(hWnd, 0, Interval, 0);
    ShowWindow(hWnd, SW_SHOW);
    break;
    }
    case WM_PAINT:
    {
    ValidateRect(hWnd, 0);
    break;
    }
    case WM_TIMER:
    {
    HDC Window = GetDC(hWnd);
    int X = (rand() % ScreenWidth) - (150 / 2),
    Y = (rand() % 15),
    Width = (rand() % 150);
    BitBlt(Window, X, Y, Width, ScreenHeight, Window, X, 0, SRCCOPY);
    ReleaseDC(hWnd, Window);
    break;
    }
    case WM_DESTROY:
    {
    KillTimer(hWnd, 0);
    PostQuitMessage(0);
    break;
    }
    return 0;
    }
    return DefWindowProc(hWnd, Msg, wParam, lParam);
    }

    int APIENTRY WinMain(HINSTANCE Inst, HINSTANCE Prev, LPSTR Cmd, int showcmd)
    {
    // Get the width & height of current display
    ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
    ScreenHeight = GetSystemMetrics(SM_CYSCREEN);

    WNDCLASS wndClass = { 0, Melter, 0,0, Inst, 0, LoadCursorW(0, IDC_ARROW), 0, 0, L"ScreenMelter" };

    if (RegisterClass(&wndClass))
    {
    // Create the "melter" overlapping window.
    HWND hWnd = CreateWindowExA(WS_EX_TOPMOST, "ScreenMelter", 0, WS_POPUP,
    0, 0, ScreenWidth, ScreenHeight, HWND_DESKTOP, 0, Inst, 0);
    if (hWnd)
    {
    // seed for randomization
    srand(GetTickCount());
    MSG Msg = { 0 };
    // Run the melter loop
    while (Msg.message != WM_QUIT)
    {
    if (PeekMessage(&Msg, 0, 0, 0, PM_REMOVE))
    {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
    }
    }
    }
    }
    return 0;
    }

    With many regards, Ben.


    • Edited by Lxnely Monday, May 14, 2018 7:46 PM
    Monday, May 14, 2018 7:45 PM

Answers

  • If you are creating a project for a window based application, you shouldn't use the console template.

    The console template expects the entry point function to be called main or wmain. If you check the Visual Studio 2015 documentation, it tells you to use the Win32 Application project type. This project type will expect the WinMain or wWinMain entry point.

    While you can change it using RLWA32's suggestion, for future projects it is much easier to use the correct project type.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Marked as answer by Lxnely Thursday, May 17, 2018 7:43 PM
    Tuesday, May 15, 2018 10:19 AM

All replies

  • Just a side note, I was using Visual Studio 2015 C++ on Win32 Console Application

    Monday, May 14, 2018 8:00 PM
  • Just a side note, I was using Visual Studio 2015 C++ on Win32 Console Application

    You should set the project's Configuration->Linker->System->Subsystem property to Windows (/SUBSYSTEM:WINDOWS).

    The code posted above uses WinMain, which is the standard entry point for a Windows GUI application, not a console app.

    • Edited by RLWA32 Monday, May 14, 2018 8:25 PM
    • Proposed as answer by Baron Bi Tuesday, May 15, 2018 2:19 AM
    Monday, May 14, 2018 8:24 PM
  • If you are creating a project for a window based application, you shouldn't use the console template.

    The console template expects the entry point function to be called main or wmain. If you check the Visual Studio 2015 documentation, it tells you to use the Win32 Application project type. This project type will expect the WinMain or wWinMain entry point.

    While you can change it using RLWA32's suggestion, for future projects it is much easier to use the correct project type.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Marked as answer by Lxnely Thursday, May 17, 2018 7:43 PM
    Tuesday, May 15, 2018 10:19 AM
  • Quite a rookie mistake, thank you very much though!
    Tuesday, May 15, 2018 3:19 PM
  • Hi,

    If your case has been solved, please help to mark answers. If not, just feel free to contact us.

    Your understanding and cooperation will be grateful.

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Lxnely Thursday, May 17, 2018 7:43 PM
    • Unmarked as answer by Lxnely Thursday, May 17, 2018 7:43 PM
    Thursday, May 17, 2018 5:44 AM
  • Will do!

    Thursday, May 17, 2018 7:44 PM