0

I have function findId(const QString& name) that throws me an error during coompilation:

error LNK2019: unresolved external symbol __imp__FindWindowW@8 referenced in function "private: unsigned int__thiscall MainClass::findId(class QString const &)"(findId@MainClass@@AAEIABVQString@@@Z)

mainclass.cpp:

WId MainClass::findId(const QString& name) { return (WId) ::FindWindow(NULL, (TCHAR*)name.utf16()); } 

I don't know where the problem is because I have used this code before in my other project and there it worked. Maybe I missed something.

7
  • You will get better answers if you retag your question. What language? What OS? What technology? What compiler? lnk2019 isn't a useful tag. Commented Oct 22, 2014 at 8:16
  • 2
    FindWindow requires Windows.h and User32.lib Commented Oct 22, 2014 at 8:26
  • 2
    The linker is not able to find the definition of ::FindWindow(...). Are you sure there is one? Commented Oct 22, 2014 at 8:26
  • I read that I have to use user32.lib, but do you know why in my other project that code works without linking it in ConfigurationProperties->Linker->Input?? Commented Oct 22, 2014 at 8:35
  • 1
    Linker->Input->Additional Dependencies: %(AdditionalDependencies) should be in there Commented Oct 22, 2014 at 8:47

2 Answers 2

4

The linker is trying to compile your application, but cannot as it doesn't know what FindWindow refers to, because you haven't used the user32 library, which is required for the FindWindow function.

The below code will fix it.

#pragma comment(lib, "user32.lib") WId MainClass::findId(const QString& name) { return (WId) ::FindWindow(NULL, (TCHAR*)name.utf16()); } 

This is worked from the code you provided and there's probably more code to it. If so, simply #pragma comment(lib, "user32.lib") after your #include block, but before any of your members or namespaces.

The following sample from the MSDN KB article on this issue will guarantee a LNK2019 error:

// LNK2019.cpp // LNK2019 expected extern char B[100]; // B is not available to the linker int main() { B[0] = ' '; } 
Sign up to request clarification or add additional context in comments.

Comments

3

In the Solution Explorer, you have several tabs. One of the tabs is called "Property Manager", open this tab. In this tab you will find your project and its configurations. What it actually contains are Property Sheets, one of them being "Core Windows Libraries". If you right-click this one, and go to Linker->Input, you will find the Windows libraries user32.lib, etc. These properties are inherited by your project throught %(AdditionalDependencies).

One of these things is not properly set up in your current project.

2 Comments

A pragma comment is better for this case IMO.
By mistake I deleted %(AdditionalDependencies) while adding my libraries. Now it works. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.