2

I am using the G++ compiler with notepad++.

I am following a tutorial by windows, the code that I am using is the example code from the windows tutorial (https://learn.microsoft.com/en-us/windows/win32/learnwin32/your-first-windows-program) I pasted this into my notepad and attempted to compile it, but I was greeted with an error message.

Here is the code and the message:

#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, 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))
    {
        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);



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

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

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

This is the command I used to compile (powershell)

PS  g++ main.cpp

And this is the message I get when trying to compile (powershell)

c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16' collect2.exe: error: ld returned 1 exit status

if I try renaming wWinMain to WinMain it gives the error

main.cpp:9:12: error: conflicting declaration of C function 'int WinMain(HINSTANCE, HINSTANCE, PWSTR, int)' int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
In file included from c:\mingw\include\windows.h:44:0, from main.cpp:5: c:\mingw\include\winbase.h:1263:14: note: previous declaration 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)' int APIENTRY WinMain (HINSTANCE, HINSTANCE, LPSTR, int);

and if I try to use the command g++ main.cpp -municode then I get the message

g++.exe: error: unrecognized command line option '-municode'

Braiam
  • 1
  • 11
  • 47
  • 78
Ash Fire
  • 37
  • 7
  • 1
    Try renaming `wWinMain` to `WinMain` or better yet enable unicode in mingw. Related: [https://stackoverflow.com/questions/58324230/undefined-reference-to-winmain-c-mingw](https://stackoverflow.com/questions/58324230/undefined-reference-to-winmain-c-mingw) look at this answer: [https://stackoverflow.com/a/62488988/487892](https://stackoverflow.com/a/62488988/487892) – drescherjm Aug 25 '20 at 12:34
  • @drescherjm thanks for the assistance, unfortunately, if I try renaming `wWinMain` it gives the error `main.cpp:9:12: error: conflicting declaration of C function 'int WinMain(HINSTANCE, HINSTANCE, PWSTR, int)' int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) In file included from c:\mingw\include\windows.h:44:0, from main.cpp:5: c:\mingw\include\winbase.h:1263:14: note: previous declaration 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)' int APIENTRY WinMain (HINSTANCE, HINSTANCE, LPSTR, int);` – Ash Fire Aug 25 '20 at 12:48
  • @drescherjm (new comment because of character limit) and if i try to use `g++ main.cpp -municode` then i get the message `g++.exe: error: unrecognized command line option '-municode'` – Ash Fire Aug 25 '20 at 12:51
  • For the ***error: conflicting declaration of C function*** problem I believe `PWSTR` needs to be changed to `PSTR` – drescherjm Aug 25 '20 at 12:54
  • If you have an old version of mingw You may be running into this: [https://stackoverflow.com/a/11706847/487892](https://stackoverflow.com/a/11706847/487892) – drescherjm Aug 25 '20 at 12:58
  • 1
    I don't think the current duplicate is the best match for this question even though the error message is the same. – drescherjm Aug 25 '20 at 13:03
  • 1
    The current duplicate is a definitely a different problem. It tells about undefined reference, whereas this question is much more specific and details conflicting WinMains. – Jeffrey Aug 25 '20 at 13:11

1 Answers1

1

in the line

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)

I changed wWinMain to WinMain and I changed PWSTR to LPSTR and now it works. I have no clue why. Hope it helps anyone with the same problem (-:

Ash Fire
  • 37
  • 7
  • I am glad you have got your solution and thanks for your sharing, I would appreciate it if you mark them as answer and this will be beneficial to other community. – Zeus Aug 26 '20 at 01:33