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.