2

This question asks about "undefined reference" problems with code for windows. Apparently the solution was to link gdi32.lib.

I have gdi32.lib linked and still get that error:

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)

I'm trying to create Window's example from https://msdn.microsoft.com/en-us/library/bb384843.aspx, this is my code:

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

The source of the error is to some degree obvious. TextOut(...) is defined as TextOutW(...), which is declared by wingdi.h. But how do I even go about finding out where the implementation should be? (From the answer to the other question, I assume that's gdi32.lib) And how do I even find out what's wrong? (And what does the @20 mean in the error message?)

I use Win 8.1 (and Win 8.1 SDK), Eclipse CPP IDE and MingW.

sgf
  • 145
  • 6

1 Answers1

3

gdi32.lib, and other *.lib files in Windows SDK, work with Visual Studio.

Use lgdi32 instead of gdi32.lib to use these libraries with gcc.

You can also use MinGW library instead. The corresponding library files have 'lib' prefix and *.a extension:

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

Also note, you need -mwindows compiler flag for Windows programs (if you don't want to see a console window)

Example:

g++ -Wall file.cpp -mwindows c:\MinGW\lib\libgdi32.a -o app.exe    
//or
g++ -Wall file.cpp -mwindows -lgdi32 -o app.exe
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • 1
    `-lgdi32` should work (without needing to specify the full path to library), if MinGW(-w64) is installed correctly – M.M Nov 28 '18 at 01:00
  • Thanks, that was precisely it! Would you know about the @20-suffix? It's in the MingW library apparently, but how did TextOutW ever get redefined as TextOutW@20? – sgf Nov 28 '18 at 08:40
  • I am not sure about @20 suffix. `TextOutW` is a DLL import, maybe it has something to do with "compiler name mangling", the compiler adds @20 - By the way, you are making a 32-bit program, and `...\\x64\\Gdi32.Lib` is 64-bit, not that you want to use those files anyway. – Barmak Shemirani Nov 28 '18 at 09:31