-1

我有函数 findId(const QString& name) 在编译过程中引发错误:

错误 LNK2019:未解析的外部符号 __imp__FindWindowW@8 在函数“private: unsigned int__thiscall MainClass::findId(class QString const &)”(findId@MainClass@@AAEIABVQString@@@Z) 中引用

主类.cpp:

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

我不知道问题出在哪里,因为我之前在我的另一个项目中使用过这个代码并且它在那里工作。也许我错过了什么。

4

2 回答 2

3

解决方案资源管理器中,您有几个选项卡。其中一个选项卡名为“Property Manager”,打开此选项卡。在此选项卡中,您将找到您的项目及其配置。它实际包含的是Property Sheets,其中之一是“Core Windows Libraries”。如果你右击这个,然后去 Linker->Input,你会发现 Windows 库 user32.lib 等等。这些属性是通过你的项目通过 %(AdditionalDependencies) 继承的。

其中一件事情没有在您当前的项目中正确设置。

于 2014-10-22T08:54:03.010 回答
3

链接器正在尝试编译您的应用程序,但无法编译,因为它不知道FindWindow指的是什么,因为您没有使用user32函数所需的库FindWindow

下面的代码将修复它。

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

这是根据您提供的代码工作的,可能还有更多代码。如果是这样,只需#pragma comment(lib, "user32.lib")在您的#include块之后,但在您的任何成员或命名空间之前。

MSDN 知识库文章中有关此问题的以下示例将保证出现LNK2019错误:

// LNK2019.cpp
// LNK2019 expected
extern char B[100];   // B is not available to the linker
int main() {
   B[0] = ' ';
}
于 2014-10-22T09:02:58.370 回答