-3
Error: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

Code:

#include <windows.h>

HWND hwnd;
COLORREF dotColor = RGB(0, 0, 0); 

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void PaintDot(HWND hwnd, int x, int y);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    WNDCLASSEX wc;
    MSG msg;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "PaintDots";
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if (!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Window Registration Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        "PaintDots",
        "Paint Dots",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
        NULL, NULL, hInstance, NULL);

    if (hwnd == NULL) {
        MessageBox(NULL, "Window Creation Failed!", "Error", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

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

    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);
                // Handle paint event here
                EndPaint(hwnd, &ps);
            }
            break;
        case WM_LBUTTONDOWN:
            {
                int x = LOWORD(lParam);
                int y = HIWORD(lParam);
                PaintDot(hwnd, x, y);
            }
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

void PaintDot(HWND hwnd, int x, int y) {
    HDC hdc = GetDC(hwnd);
    SetPixel(hdc, x, y, dotColor);
    ReleaseDC(hwnd, hdc);
}

Build Command:

gcc GPTpaint.c -o paintdots.exe -mwindows 

Compiler: GCC (installed using MSYS2)

Note: I've tried entering in the MSYS2 terminal 'pacman -Su' which would update all packages, including GCC and it seems to have no need to do that. I also notice that wWinMain is not an accepted entry point even though the only difference between them is ASCII and Unicode. There seems to be an issue regarding w_char_t and LPSTR (one of the entry point parameters which is a type) and I've had code that had successfully surpassed this error but then gained those aforementioned errors.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 2
    Since you obviously do have a `WinMain` the error is likely something to do with the way you are building the code, not a problem with the code itself. Could you describe something about that, like what compiler you are using, what options you picked, etc, etc. – john Aug 18 '23 at 12:24
  • @john Thank you for response. I have added this note: I've tried entering in the MSYS2 terminal 'pacman -Su' which would update all packages, including GCC and it seems to have no need to do that. I also notice that wWinMain is not an accpeted entry point even though the only difference between them is ASCII and Unicode. There seems to be an issue regarding w_char_t and LPSTR (one of the entry point parameters which is a type) and I've had code that had successfully surpassed this error but then gained those aforemention errors. – Sclera Left Aug 18 '23 at 12:51
  • Since you're getting a linker error you need to reproduce the linker command line you're passing in the question. – IInspectable Aug 18 '23 at 13:06
  • I took you code, dropped into a new visual studio project, it build and linked fine. here is the linker command I used: OUT:"C:\Project1.exe" /MANIFEST /NXCOMPAT /PDB:"C:\Project1.pdb" /DYNAMICBASE "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG /MACHINE:X86 /INCREMENTAL /PGD:"Project1.pgd" /SUBSYSTEM:WINDOWS /MANIFESTUAC:"level='asInvoker' uiAccess='false' /LTCGOUT:"Debug\Project1.iobj" /ERRORREPORT:PROMPT /ILK:"Debug\Project1.ilk" /NOLOGO /TLBID:1 – thurizas Aug 18 '23 at 13:45
  • @IInspectable Thank you for your recommendation. Added. – Sclera Left Aug 18 '23 at 14:03
  • @thurizas I used the same command (on vs code with mingw-w64) and it seems you are using visual c++ which I think could be the reason why it isnt linking properly (because it uses libraries only present in visual c++ compiler). – Sclera Left Aug 18 '23 at 14:08
  • In the error message, your executable is "**collect2.exe**" and in your command line "**paintdots.exe**". – CGi03 Aug 18 '23 at 15:23
  • using msys2 I was able to compile your code (with minor modification) using "gcc -D UNICODE -D _UNICODE -c paintdots.c -o paintdots.o" and the link the resulting object file with "gcc -o paintdots.exe paintdots.o -s -lcomctl32 -luser32 -l gdi32 -Wl,--subsystem,windows". Both these command worked without error or warning. – thurizas Aug 18 '23 at 16:31

0 Answers0