c - 如何从文件中正确添加 gdi32.lib?

标签 c windows mingw

我目前在包含 gdi32.lib 时遇到问题在我的 Windows 上的 C 项目中。错误发生在以下代码段中:

#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);
    ....
}

当我使用 -lgdi32 进行编译时选项,一切都按预期工作,而我使用以下命令收到编译错误:

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

有没有办法直接在文件中链接库,而不是每次都将其传递给编译器?


这是用于调试的精简版本:

#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;
}

最佳答案

#pragma comment(lib, ...) 是 MS Visual C/C++ 编译指示。您正在使用 MinGW,它不支持该指令。

根据我读到的内容this question 、GCC 和 G++ 没有等效的编译指示,因此您必须坚持使用 -l 选项。

关于c - 如何从文件中正确添加 gdi32.lib?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40696388/

相关文章:

使用 MinGW 找不到 C++ 过程入口点

multithreading - 在32位MinGW 4.8.0中使用g++时std::thread出现问题

c++ - 我可以在 Armadillo 中实例化矩阵,然后在后面的代码行中将其设置为使用辅助内存吗?

mysql - 连接 MySQL 和 C

c - 为什么我在尝试传递数组值时收到 "incompatible pointer type"?

python - 在 Python/Django 中打印变量的值?

c++ - 在 Quick FIX 4.2 中自定义实现多边期权定单

windows - 检查 Windows API 级别的 DLL

c - fopen 文件名以奇怪的点开头

c - 针对静态库编译时遇到问题