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.

never_ever
  • 185
  • 1
  • 4
  • 13
  • You will get better answers if you retag your question. What language? What OS? What technology? What compiler? `lnk2019` isn't a useful tag. – Jeroen Mostert Oct 22 '14 at 08:16
  • 2
    FindWindow requires Windows.h and User32.lib – Gunther Van Butsele Oct 22 '14 at 08:26
  • 2
    The linker is not able to find the definition of `::FindWindow(...)`. Are you sure there is one? – CinCout Oct 22 '14 at 08: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?? – never_ever Oct 22 '14 at 08:35
  • 1
    Linker->Input->Additional Dependencies: `%(AdditionalDependencies)` should be in there – Gunther Van Butsele Oct 22 '14 at 08:47
  • Duplicate of (too many to put here!): http://stackoverflow.com/questions/2122397/error-lnk2019-unresolved-external-symbol?rq=1, http://stackoverflow.com/questions/9220829/lnk2019-error-unresolved-external-symbol?rq=1, http://stackoverflow.com/questions/12297952/error-lnk2019-unresolved-external-symbol?rq=1, http://stackoverflow.com/questions/13318965/error-lnk2019-unresolved-external-symbol?rq=1. You want this: http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – AStopher Oct 22 '14 at 08:56
  • possible duplicate of [error LNK2019: unresolved external symbol "public:](http://stackoverflow.com/questions/4790751/error-lnk2019-unresolved-external-symbol-public) – David Heffernan Oct 22 '14 at 08:57

2 Answers2

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.

3

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] = ' ';
}
AStopher
  • 4,207
  • 11
  • 50
  • 75