链接了Gdi32.lib的对“ TextOutW @ 20”的未定义引用

时间:2018-11-27 14:38:05

标签: c++ linker

This question询问有关Windows代码的“未定义参考”问题。显然,解决方案是链接gdi32.lib

我已链接gdi32.lib并仍然收到该错误:

14:26:24 **** Incremental Build of configuration Debug for project SomeWindows ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\SomeWindows.o" "..\\src\\SomeWindows.cpp" 
g++ -o SomeWindows.exe "src\\SomeWindows.o" "C:\\Program Files (x86)\\Windows Kits\\8.1\\Lib\\winv6.3\\um\\x64\\Gdi32.Lib" 
src\SomeWindows.o: In function `Z7WndProcP6HWND__jjl@16':
C:\Users\Mxx\eclipse-workspace\SomeWindows\Debug/../src/SomeWindows.cpp:78: undefined reference to `TextOutW@20'
collect2.exe: Fehler: ld gab 1 als Ende-Status zurück

14:26:26 Build Failed. 1 errors, 0 warnings. (took 2s.139ms)

我正在尝试从https://msdn.microsoft.com/en-us/library/bb384843.aspx创建Window的示例,这是我的代码:

#define UNICODE
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>

LRESULT CALLBACK WndProc(
     HWND hwnd,
     UINT uMsg,
     WPARAM wParam,
     LPARAM lParam);


static TCHAR a[] = _T("My first C++ program");
static TCHAR b[] = _T("My first wstring");

int main () {

    HINSTANCE hInstance = GetModuleHandle(0);

    WNDCLASSEX wcex;

    wcex.cbSize         = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, IDI_APPLICATION);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = a;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, IDI_APPLICATION);

    RegisterClassEx(&wcex);

    HWND hWnd = CreateWindow(
       a,
       b,
       WS_OVERLAPPEDWINDOW,
       CW_USEDEFAULT, CW_USEDEFAULT,
       500, 100,
       NULL,
       NULL,
       hInstance,
       NULL
    );

    ShowWindow(hWnd,3);
    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){
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("I just write stuff here.");
    switch (message)
       {
       case WM_PAINT:
          hdc = BeginPaint(hWnd, &ps);

          // Here your application is laid out.
          // For this introduction, we just print out "Hello, Windows Desktop!"
          // in the top left corner.
          TextOut(hdc,
             5, 5,
             greeting, _tcslen(greeting));
          // End application-specific layout section.

          EndPaint(hWnd, &ps);
          break;
       case WM_DESTROY:
          PostQuitMessage(0);
          break;
       default:
          return DefWindowProc(hWnd, message, wParam, lParam);
          break;
       }
    return 0;
}

错误的根源在某种程度上是显而易见的。 TextOut(...)定义为TextOutW(...),由wingdi.h声明。但是,我什至怎么去寻找实现的位置呢? (从答案到另一个问题,我假设这是gdi32.lib)而且我怎么才能找出问题所在? (错误消息中的@ 20是什么意思?)

我使用Win 8.1(和Win 8.1 SDK),Eclipse CPP IDE和MingW。

1 个答案:

答案 0 :(得分:2)

gdi32.lib和Windows SDK中的其他* .lib文件可与Visual Studio一起使用。

使用lgdi32而非gdi32.lib来将这些库与gcc一起使用。

您也可以改用MinGW库。相应的库文件具有'lib'前缀和*.a扩展名:

gdi32.lib -> c:\MinGW\lib\libgdi32.a

还请注意,Windows程序需要-mwindows编译器标志(如果您不想看到控制台窗口)

示例:

g++ -Wall file.cpp -mwindows c:\MinGW\lib\libgdi32.a -o app.exe    
//or
g++ -Wall file.cpp -mwindows -lgdi32 -o app.exe