Search code examples
c++referenceundefined

Undefined reference to CreateCompatibleDC, BitBlt, etc.?


I already linked to gdi32.lib. In desperation, I also linked to two different versions of gdi32.lib, as well as gdiplus.lib, and included all the gdi headers I could find, to no avail. However, this problem began before the extra links, so I think I can safely assume my problem isn't collisions between libraries or any sort of interference like that.

Inclusions/definitions:

#define WINVER 0x0500
#define _WIN32_WINNT 0x0500
#include <iostream>
#include <process.h>
#include <windows.h>
#include <winuser.h>
#include <gdiplus.h>
#include "UVMap.h"

Related code:

case WM_PAINT:{
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
        HDC hdcMemory = CreateCompatibleDC(hdc);
        BITMAP bmp = screen.getBitmap(hdcMemory);
        BitBlt(hdc,0,0,w,h,hdcMemory,0,0,SRCCOPY);
        EndPaint(hwnd,&ps);
        DeleteDC(hdc);
    }break;

Every result I found when searching for this problem claimed that you just had to link to gdi32. Here are the links I added:

  • C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib\x64\Gdi32.Lib
  • C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib\Gdi32.Lib
  • C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib\GdiPlus.lib

Edit: the errors appear like this:

.../Testing.cpp:50: undefined reference to `CreateCompatibleDC@4'
.../Testing.cpp:52: undefined reference to `BitBlt@36'

Like that? I did the linking in Eclipse, via Project > Properties > Path Variables. There are no entries under the Linked Resources tab, nor does it appear editable.


Solution

  • You need to link to coredll.lib for CreateCompatibleDC.

    You need to link to for Gdi32.lib for BitBlt.

    For reference, when you get a link error involving a Windows function, just google the function and go to the MSDN documentation for that function (I've linked to the two pages for these two specific functions), and then scroll down to the Requirements section and make sure you're linking to the specified library.