Search code examples
c++gcccompiler-errorsgdimingw-w64

Missing wingdi functions when exporting using gcc


So i'm trying to compile this simple piece of c++ code using gcc:

#include <windows.h>
#include <wingdi.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HWND hwnd = GetDesktopWindow();
    HDC hdc = GetWindowDC(hwnd);
    RECT rekt;
    GetWindowRect(hwnd, &rekt);
        for (int i = 0; i < 50000; i++) {
            int x = rand() % 1920;
            int y = rand() % 1080;
            SetPixel(hdc, x, y, RGB(255, 255, 255));
        }
    ReleaseDC(hwnd, hdc);
}

and i'm using the following console command to (try to) build the executable:

.\g++.exe -lgdi32 -o output WindowsProject1.cpp

But i'm getting the following error:

WindowsProject1.cpp:(.text+0xcd): undefined reference to `SetPixel@16'
collect2.exe: error: ld returned 1 exit status

I'm guessing this is some linking issue? I'm pretty new to compiling c++, and it runs fine in Visual Studio, so i'm not quite sure what to do. As mentioned before, the command I'm using to build the executable does contain "-lgdi32" and this was an attempt of linking the library, but this still did not resolve the issue.


Solution

  • Putting -lgdi32 at the end as suggested by @user4581301 fixed this issue.