0

I'm currently having a problem with including the gdi32.lib in my C project on windows. The error occurs in the following code segment:

#include <windows.h>
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    WNDCLASSEX wcx;
    wcx.hbrBackground  = CreateSolidBrush(0x000000);
    ....
}

When I compile with the -lgdi32 option, everything works just as expected, whereas I get a compiling error with the following command:

C:\projects\cpp>c++ project.cpp
    C:\Users\LUKASW~1\AppData\Local\Temp\ccSKc5qg.o:project.cpp:(.text+0xe0):
        undefined reference to `__imp_CreateSolidBrush'
    collect2.exe: error: ld returned 1 exit status

Is there a way to link the libraries directly in a file rather than passing it to the compiler every time?


Here is the stripped down version for debugging:

#include <windows.h>
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")

static char szWindowClass[] = "debugging";
static char szTitle[] = "gdi32.lib test";

HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

void GetDesktopResolution(int& w, int& h) {
    RECT desktop;
    GetWindowRect(GetDesktopWindow(), &desktop);
    w = desktop.right;
    h = desktop.bottom;
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    WNDCLASSEX wcx;

    wcx.cbClsExtra     = 0;
    wcx.cbSize         = sizeof(WNDCLASSEX);
    wcx.cbWndExtra     = 0;
    wcx.hbrBackground  = CreateSolidBrush(0x000000);
    wcx.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcx.hIcon          = LoadIcon(hInst, IDI_APPLICATION);
    wcx.hIconSm        = LoadIcon(wcx.hInstance, IDI_APPLICATION);
    wcx.hInstance      = hInst;
    wcx.lpfnWndProc    = WndProc;
    wcx.lpszClassName  = szWindowClass;
    wcx.lpszMenuName   = NULL;
    wcx.style          = CS_HREDRAW | CS_VREDRAW;

    RegisterClassEx(&wcx);

    int x, y,
        w = 600,
        h = 600;
    GetDesktopResolution(x, y);

    HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, szWindowClass, szTitle, WS_POPUP, (x - w) / 2, (y - h) / 2, w, h, NULL, NULL, hInst, NULL);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

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

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_PAINT:
            // empty
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
            break;
    }

    return 0;
}
Wip
  • 101
  • 1
  • 10

1 Answers1

2

#pragma comment(lib, ...) is an MS Visual C/C++ pragma. You are using MinGW, which doesn't support that directive.

From what I read on this question, GCC and G++ don't have an equivalent pragma, so you will have to stick with the -l option.

Community
  • 1
  • 1
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85