尝试使用 DirectXTK 创建 GamePad 时出现错误 LNK2019

error LNK2019 when trying to create a GamePad using DirectXTK


我正在关注如何将 GamePad 整合到我的应用程序中的 DirectXTK GamePad tutorial,但我在尝试创建 GamePad 时收到 errorLNK2019

到目前为止我已经:

当我添加最后一行代码时出现错误(很长):

DirectXTK.lib(GamePad.obj) : error LNK2019: unresolved external symbol __imp_RoGetActivationFactory referenced in function "long __cdecl ABI::Windows::Foundation::GetActivationFactory<struct ABI::Windows::Gaming::Input::IGamepadStatics>(struct HSTRING__ *,struct ABI::Windows::Gaming::Input::IGamepadStatics * *)" (??$GetActivationFactory@UIGamepadStatics@Input@Gaming@Windows@ABI@@@Foundation@Windows@ABI@@YAJPEAUHSTRING__@@PEAPEAUIGamepadStatics@Input@Gaming@12@@Z)
DirectXTK.lib(GamePad.obj) : error LNK2019: unresolved external symbol WindowsCreateStringReference referenced in function "private: void __cdecl Microsoft::WRL::Wrappers::HStringReference::CreateReference(wchar_t const *,unsigned int,unsigned int)" (?CreateReference@HStringReference@Wrappers@WRL@Microsoft@@AEAAXPEB_WII@Z)
fatal error LNK1120: 2 unresolved externals

在我的应用程序中,我已经将 SpriteBatch without any problems so I'm guessing that I've done all the right things when I included DirectXTK 用于我的解决方案。

我还尝试使用

创建 GamePad
mGamePad.reset( new GamePad() );

我在教程的 older version 中找到的。

还有..控制器插好了.. 关于我做错了什么的任何建议?谢谢!

您必须 link 您的项目 RuntimeObject.lib

当 DirectX 工具包的 GamePad class 是为 Windows 10(即 _WIN32_WINNT=0x0A00)构建时,它使用新的 Windows.Gaming.Input Windows运行时 classes 而不是 XINPUT。

如果您正在为通用 Windows 平台构建,那么您已经 Windows 运行时初始化,_WIN32_WINNT=0x0a00 并且可能 link 针对 RuntimeObject.lib.

如果您正在构建 Windows 桌面(又名 Win32 或 "classic" 应用程序),那么首先确保您真的打算只为 Windows 10 制作它。有一个 plethora of different vcxproj files 用于 DirectX 工具包,具体取决于您的目标平台。如果您确实希望 Windows 10 只针对 Windows 桌面应用程序,那非常酷,但您需要为您的应用程序显式初始化 Windows 运行时。

TL;DR: 如果您制作的 Windows 桌面应用程序需要 Windows 10,则 link 需要 RuntimeObject.lib 并将其添加到您的应用程序初始化(替换 CoInitializeCoInitializeEx):

Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
if (FAILED(initialize))
    return 1;

如果您不制作需要 Windows 10 的 Windows 桌面应用程序,并且您不制作通用 Windows 平台 (UWP) 应用程序,则使用不同的版本为 Windows 7 兼容性而构建的 DirectX 工具包的一部分。

this post