Prakhar
Prakhar

Reputation: 325

Unresolved external symbol error while using CImg library

I am trying to use CImg library for some image processing task. Using VS 2012 on Windows 7 x64, I am building a project to create dll that I need for my application. I have included the only header file CImg.h from the library. But as soon as I initialize an CImg object, I get bunch of unresolved external symbol errors. One sample error is as follows:

layer.obj : error LNK2019: unresolved external symbol __imp_SetDIBitsToDevice referenced in function "public: struct cimg_library::CImgDisplay & __cdecl cimg_library::CImgDisplay::paint(void)" (?paint@CImgDisplay@cimg_library@@QEAAAEAU12@XZ)

Can anyone explain to me what am I doing wrong and how to fix it? (I am a newbie when it comes to C++ terminologies.)

Upvotes: 2

Views: 1491

Answers (2)

Cody Gray
Cody Gray

Reputation: 244782

there is nothing other than the header file in the CImg library to link to.

You cannot link to a header file. If it is a header-only library, then you do not need to link anything. You include the header file and the functions it defines are compiled directly. That appears to be the case for CImg; the documentation says it is a self-contained template library that consists of only a single header file. So indeed, all you need to do is include it and you're off to the races.

The unresolved external symbol errors are coming from somewhere else. You have to read the error messages and look at the function names to see where.

A couple of hints:

  1. The __imp_ prefix suggests that you're looking at a Windows API function.
  2. If you didn't know that, you could always ignore the prefix and Google the readable part of the name, in this case, SetDIBitsToDevice. Chances are very good you'll turn up the documentation or at least something that points you in the right direction.

Indeed, in this case, you get right to Microsoft's SDK documentation for the SetDIBitsToDevice function. It's a Windows API function alright, and Microsoft's documentation always tells you what library you need to link to in order to consume it. Look at the bottom of the page:

Header: Wingdi.h (include Windows.h)
Library: Gdi32.lib
DLL: Gdi32.dll

The CImg library header file has obviously already included the Windows.h header file, or you'd have gotten a compile-time error. You're getting a linker error, which means that you have not told the linker to link in the Gdi32.lib library. This is what will allow you to call GDI functions. It is a stub that facilitates calling functions exported from Gdi32.dll.

In general, when building a Windows application, you will want to link with, at minimum, kernel32.lib, user32.lib, and gdi32.lib.

This question contains more information on dealing with undefined symbol errors, and also how to configure your linker. In Visual Studio, go to Project Properties → C/C++ → Linker → Input → Additional Dependencies. Or add #pragma comment(lib, "gdi32.lib") to a source file (your precompiled header is a good place, usually named StdAfx.h).

Upvotes: 5

Photon
Photon

Reputation: 3222

This function is part of the win32 API, specifically in GDI. You need to change your project settings to link with Gdi32.lib

https://msdn.microsoft.com/en-us/library/windows/desktop/dd162974(v=vs.85).aspx

Upvotes: 1

Related Questions