2

我正在尝试在 Win 7 上使用 gcc 编译一个基本的 hello word 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 `GetStockObject@4'
C:\Users\Bobby\AppData\Local\Temp\ccT0bq97.o:myfile.c:(.text+0x1db): undefined reference to `TextOutA@20'
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 `GetStockObject@4'
C:\Users\Bobby\AppData\Local\Temp\ccylV5js.o:myfile.c:(.text+0x1db): undefined reference to `TextOutA@20'
collect2: ld returned 1 exit status
PS C:\Users\Bobby\desktop> C:\MinGW\

我做了一些谷歌搜索,我发现这首先GetStockObject@4是在一个名为 Gdi32.lib 的文件中。所以我搜索了我的硬盘驱动器并通过 MinGW 并找不到它。定位此函数的 Lib 在哪里,链接到它的方法是什么?我也认为我必须链接到TextOutA@20

4

2 回答 2

9

-mwindows在这种情况下,您应该使用子系统选项,而不是显式链接 GDI32 :

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
于 2012-09-29T10:35:22.087 回答
0

Windows DLL 需要所谓的导入库供链接器解析对其导出函数的引用。Microsoft 为Windows SDK提供导入库,包括Gdi32.lib 。此文件不应部署给最终用户。

于 2012-09-29T09:16:57.390 回答