C++ unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)"

zoucyn 1 Reputation point
2022-06-30T20:38:33.533+00:00

ifndef UNICODE

define UNICODE

endif

include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";

WNDCLASS wc = { };  

wc.lpfnWndProc = WindowProc;  
wc.hInstance = hInstance;  
wc.lpszClassName = CLASS_NAME;  

RegisterClass(&wc);  

// Create the window.  

HWND hwnd = CreateWindowEx(  
    0,                              // Optional window styles.  
    CLASS_NAME,                     // Window class  
    L"Learn to Program Windows",    // Window text  
    WS_OVERLAPPEDWINDOW,            // Window style  

    // Size and position  
    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,  

    NULL,       // Parent window      
    NULL,       // Menu  
    hInstance,  // Instance handle  
    NULL        // Additional application data  
);  

if (hwnd == NULL)  
{  
    return 0;  
}  

ShowWindow(hwnd, nCmdShow);  

// Run the message loop.  

MSG msg = { };  
while (GetMessage(&msg, NULL, 0, 0) > 0)  
{  
    TranslateMessage(&msg);  
    DispatchMessage(&msg);  
}  

return 0;  

}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;

case WM_PAINT:  
{  
    PAINTSTRUCT ps;  
    HDC hdc = BeginPaint(hwnd, &ps);  

    // All painting occurs here, between BeginPaint and EndPaint.  

    FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));  

    EndPaint(hwnd, &ps);  
}  
return 0;  

}  
return DefWindowProc(hwnd, uMsg, wParam, lParam);  

}

List item

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,443 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 80,911 Reputation points
    2022-06-30T21:07:06.33+00:00

    Change :
    [Linker] [System]
    Windows (/SUBSYSTEM:WINDOWS)

    0 comments No comments

  2. YujianYao-MSFT 4,266 Reputation points Microsoft Vendor
    2022-07-01T01:14:47.567+00:00

    Hi @zoucyn ,

    It seems that your code is missing the main function. After adding the main function, the program no longer reports any errors.

    #ifndef UNICODE  
    #define UNICODE  
    #endif  
      
      
    #include <windows.h>  
      
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);  
      
      
    int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)  
    {  
        // Register the window class.  
        const wchar_t CLASS_NAME[] = L"Sample Window Class";  
      
        WNDCLASS wc = { };  
        wc.lpfnWndProc = WindowProc;  
        wc.hInstance = hInstance;  
        wc.lpszClassName = CLASS_NAME;  
        RegisterClass(&wc);  
        // Create the window.  
        HWND hwnd = CreateWindowEx(  
            0,                              // Optional window styles.  
            CLASS_NAME,                     // Window class  
            L"Learn to Program Windows",    // Window text  
            WS_OVERLAPPEDWINDOW,            // Window style  
            // Size and position  
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,  
            NULL,       // Parent window      
            NULL,       // Menu  
            hInstance,  // Instance handle  
            NULL        // Additional application data  
        );  
        if (hwnd == NULL)  
        {  
            return 0;  
        }  
        ShowWindow(hwnd, nCmdShow);  
        // Run the message loop.  
        MSG msg = { };  
        while (GetMessage(&msg, NULL, 0, 0) > 0)  
        {  
            TranslateMessage(&msg);  
            DispatchMessage(&msg);  
        }  
        return 0;  
    }  
      
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)  
    {  
        switch (uMsg)  
        {  
        case WM_DESTROY:  
            PostQuitMessage(0);  
            return 0;  
      
        case WM_PAINT:  
        {  
            PAINTSTRUCT ps;  
            HDC hdc = BeginPaint(hwnd, &ps);  
            // All painting occurs here, between BeginPaint and EndPaint.  
            FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));  
            EndPaint(hwnd, &ps);  
        }  
        return 0;  
        }  
        return DefWindowProc(hwnd, uMsg, wParam, lParam);  
    }  
    int main()  
    {  
        return 0;  
    }  
    

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.