2012-09-29 99 views
1

我想在Win 7如何在使用gcc編譯win32項目時鏈接到庫?

的代碼用gcc編譯一個基本的問候詞WinForm應用程序是這樣的:

/* 

    WINHELLO.C 

    "Hello, world!", Win32 style. 

*/ 

#include <windows.h> 

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


/* WinMain(), our entry point */ 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
      LPSTR szCmdLine, int iCmdShow) { 
    static char szAppName[] = "winhello"; 
    HWND  hwnd; 
    MSG   msg; 
    WNDCLASSEX wndclass; 


    /* Fill in WNDCLASSEX struct members */ 

    wndclass.cbSize   = sizeof(wndclass); 
    wndclass.style   = CS_HREDRAW | CS_VREDRAW; 
    wndclass.lpfnWndProc = WndProc; 
    wndclass.cbClsExtra  = 0; 
    wndclass.cbWndExtra  = 0; 
    wndclass.hInstance  = hInstance; 
    wndclass.hIcon   = LoadIcon(NULL, IDI_APPLICATION); 
    wndclass.hIconSm  = LoadIcon(NULL, IDI_APPLICATION); 
    wndclass.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 
    wndclass.lpszClassName = szAppName; 
    wndclass.lpszMenuName = NULL; 


    /* Register a new window class with Windows */ 

    RegisterClassEx(&wndclass); 


    /* Create a window based on our new class */ 

    hwnd = CreateWindow(szAppName, "Hello, world!", 
      WS_OVERLAPPEDWINDOW, 
      CW_USEDEFAULT, CW_USEDEFAULT, 
      CW_USEDEFAULT, CW_USEDEFAULT, 
      NULL, NULL, hInstance, NULL); 


    /* Show and update our window */ 

    ShowWindow(hwnd, iCmdShow); 
    UpdateWindow(hwnd); 


    /* Retrieve and process messages until we get WM_QUIT */ 

    while (GetMessage(&msg, NULL, 0, 0)) { 
    TranslateMessage(&msg); /* for certain keyboard messages */ 
    DispatchMessage(&msg);  /* send message to WndProc  */ 
    } 


    /* Exit with status specified in WM_QUIT message */ 

    return msg.wParam; 
} 


/* Window procedure */ 

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { 
    PAINTSTRUCT ps; 
    HDC   hdc; 


    /* Switch according to what type of message we have received */ 

    switch (iMsg) { 
    case WM_PAINT: 

    /* We receive WM_PAINT every time window is updated */ 

    hdc = BeginPaint(hwnd, &ps); 
    TextOut(hdc, 100, 100, "Hello, world!", 13); 
    EndPaint(hwnd, &ps); 
    return 0; 

    case WM_DESTROY: 

    /* Window has been destroyed, so exit cleanly */ 

    PostQuitMessage(0); 
    return 0; 
    } 


    /* Send any messages we don't handle to default window procedure */ 

    return DefWindowProc(hwnd, iMsg, wParam, lParam); 
} 

我給編譯器的命令是gcc C:\Users\Bobby\Desktop\myfile.c

好,它尋找庫:

C:\Users\Bobby\desktop>gcc C:\Users\Bobby\Desktop\myfile.c 
C:\Users\Bobby\AppData\Local\Temp\ccT0bq97.o:myfile.c:(.text+0x88): undefined reference to `[email protected]' 
C:\Users\Bobby\AppData\Local\Temp\ccT0bq97.o:myfile.c:(.text+0x1db): undefined reference to `[email protected]' 
collect2: ld returned 1 exit status 
PS C:\Users\Scruffy\desktop> .\build.bat 

C:\Users\Bobby\desktop>gcc C:\Users\Bobby\Desktop\myfile.c -I Gdi32.lib 
C:\Users\Bobby\AppData\Local\Temp\ccylV5js.o:myfile.c:(.text+0x88): undefined reference to `[email protected]' 
C:\Users\Bobby\AppData\Local\Temp\ccylV5js.o:myfile.c:(.text+0x1db): undefined reference to `[email protected]' 
collect2: ld returned 1 exit status 
PS C:\Users\Bobby\desktop> C:\MinGW\ 

我做了一些Google搜索,以及我發現這第一個[email protected]位於名爲Gdi32.lib的文件中。所以我搜索了我的硬盤,並通過MinGW找不到它。 Lib在哪裏找到這個功能以及鏈接到它的方式是什麼?此外,我認爲我必須鏈接到[email protected]

回答

8

而是明確對鏈接的GDI32,你應該使用-mwindows子系統選項,在這種情況下:

gcc -Wall -mwindows winhello.c -o winhello.exe 

注:

GCC等。比.lib更喜歡.a文件,所以你應該一直在尋找libgdi32.a。你可以對任何給予的文件名作爲參數仍鏈接:

gcc src.c /path/to/example1.lib /path/to/libexample2.a 

或使用-l選項.a文件:

gcc src.c /path/to/example1.lib -L/path/to -lexample2 
+0

感謝。 gcc在哪裏找到這些功能? –

+0

不幸的是,在這種情況下,文檔沒有多大幫助。在Windows開發中,您可以擁有子系統控制檯和Windows窗口應用程序。我相信,大多數人在他們搜索「如何在我的Win32應用程序中擺脫這個控制檯窗口」時,第一次瞭解它。我想不出任何其他晦澀難懂的非常有用的信息。那麼,如果你有與char類型相關的問題,請記住'-DUNICODE'和'-D_UNICODE'。 – aib

0

Windows DLL需要所謂的導入庫供鏈接器解析對其導出函數的引用。 Microsoft爲Windows SDK提供導入庫,包括Gdi32.lib。這個文件不應該被部署到最終用戶。

相關問題