Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用gcc编译win32项目时,如何链接到库?_C_Windows_Winapi_Gcc - Fatal编程技术网

在使用gcc编译win32项目时,如何链接到库?

在使用gcc编译win32项目时,如何链接到库?,c,windows,winapi,gcc,C,Windows,Winapi,Gcc,我试图在Win7上用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(HINSTAN

我试图在Win7上用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);
}

我在谷歌上搜索了一下,发现这是第一个
GetStockObject@4
位于名为Gdi32.lib的文件中。所以我搜索了我的硬盘,通过MinGW找不到它。这个函数的库在哪里?链接到它的方式是什么?我还想我必须链接到
TextOutA@20

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

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

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

gcc等人更喜欢.a文件而不是.lib,所以您应该一直在寻找
libgdi32.a
。您仍然可以通过将文件名作为参数来链接:

gcc src.c /path/to/example1.lib /path/to/libexample2.a
或使用.a文件的
-l
选项:

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

谢谢gcc在哪里找到这些函数的?不幸的是,在这种情况下,文档没有太大帮助。Windows开发中的常识是,您可以使用子系统控制台和Windows应用程序。我相信,现在大多数人在搜索“如何在我的Win32应用程序中摆脱这个控制台窗口”时都会了解到这一点。我想不出还有什么其他非常有用的模糊信息。好吧,如果您遇到与字符类型相关的问题,可能需要记住
-DUNICODE
-duunicode
gcc src.c /path/to/example1.lib -L/path/to -lexample2