2016-11-19 134 views
0

我目前在將windows項目中的gdi32.lib添加到我的C項目時遇到問題。如何從文件內正確添加gdi32.lib?

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

回答

1

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

從我在this question上看到的,GCC和G ++沒有等價的編譯指示,所以你必須堅持使用-l選項。

+0

感謝您的回答,您是對的。我會堅持用'-l'選項。 – Wip