Neel Basu
Neel Basu

Reputation: 12904

Qt linker Error on HWND, HDC

I have included windows.h. Its even working well and fine if I just comment out HWND, HDC, HBITMAP, GetBitmapBits() etc.. I think I need to include the proper LIB ?? what LIB to include ? Is it -lgdi32 ??

Error Message: :: error: collect2: ld returned 1 exit status

EDIT after adding LIBS += -lgdi32 there was no linker Error and It compiled Successfully. So Its solved I think !!!! or Is it ?

Upvotes: 1

Views: 1209

Answers (2)

Hossein
Hossein

Reputation: 4137

In addition to the answer by Tadmas about looking up MSDN to see which library you require, when using Qt you can define the libraries without making changes to the linker flags, etc.

  • If you are using Microsoft's compiler, adding e.g LIBS += Gdi32.lib line to the .pro file would be sufficient. Done!

  • If you are using GCC, you should supply the full path to the lib file, e.g LIBS += "C:/Program Files (x86)/Microsoft SDKs/Windows/v7.0A/Lib/Gdi32.lib".

  • If you have a project that targets both 32-bit and 64-bit GCC builds (and you have built Qt with a 64-bit GCC on Windows), you should use conditional scopes in the .pro file to determine if you are building for 32-bit or 64-bit and supply the path to the 64-bit lib file in 64-bit builds. e.g:

    win32-g++:!contains(QMAKE_HOST.arch, x86_64) {
        LIBS += "C:/Program Files (x86)/Microsoft SDKs/Windows/v7.0A/Lib/Gdi32.lib"
    } else {
        LIBS += "C:/Program Files (x86)/Microsoft SDKs/Windows/v7.0A/Lib/x64/Gdi32.lib"
    }
    

Upvotes: 0

Tadmas
Tadmas

Reputation: 6368

Even though you have a solution in an edit to your question, I think it might be useful to illustrate how you can fix these sorts of problems in the future.

When the linker gives an error, that likely means that you are trying to reference a piece of executable code that it can't find. There should have been a series of errors above the line you quoted that state which specific functions were causing the problem, such as:

foo.obj: error LNK2019: unresolved external symbol _GetBitmapBits@12 referenced in function...

Output may be slightly different for your particular linker. It will probably include the phrase "unresolved external". Those errors are what @Friend-Pal was actually asking for.

When you get an unresolved external reference, you fix it by linking in the appropriate library. To determine which library to include for which function, look at the documentation. For example, the documentation for GetBitmapBits states at the bottom:

Minimum supported client: Windows 2000 Professional
Minimum supported server: Windows 2000 Server
Header: Wingdi.h (include Windows.h)
Library: Gdi32.lib
DLL: Gdi32.dll

The method of adding the library reference depends on the specific linker you are using. In your case, based on the edit to your question, that should be -lgdi32 to link in the gdi32.lib library.

Repeat this process until all the references are resolved.

Data types (HWND, HDC, HBITMAP, etc.) only need the appropriate header file; they do not have any actual executable code associated with them, so they don't affect the linker.

Upvotes: 3

Related Questions