c++ - DirectX C++ 的链接器错误

标签 c++ linker directx linker-errors

我正在处理一个 Direct X11 项目,我试图在我生成的窗口中绘制一个矩形(整个窗口 directX 初始化工作正常,因为它之前编译正确)。 为此,我包含了以下标题:

#include "CommonStates.h"
#include "SpriteBatch.h"

我将这些 header 的链接添加到 C/C++>General>Additional Include Directories,我认为它确实找到了它,因为我没有关于包含它的错误。

之后,我尝试编译我的项目。以下几行导致了一些链接器错误:

RECT *try1 = new RECT();
try1->bottom = 0; try1->left = 0; try1->right = 50; try1->bottom = 50;

CommonStates states(d3dDevice);
sprites->Begin(SpriteSortMode_Deferred, states.NonPremultiplied());
sprites->Draw(textureRV, XMFLOAT2(50, 50), try1, Colors::Black);
sprites->End();

错误:

1>Framework.obj : error LNK2019: unresolved external symbol "public: __thiscall DirectX::CommonStates::CommonStates(struct ID3D11Device *)" (??0CommonStates@DirectX@@QAE@PAUID3D11Device@@@Z) referenced in function "public: void __thiscall Framework::Draw2(unsigned long *)" (?Draw2@Framework@@QAEXPAK@Z)
1>Framework.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall DirectX::CommonStates::~CommonStates(void)" (??1CommonStates@DirectX@@UAE@XZ) referenced in function "public: void __thiscall Framework::Draw2(unsigned long *)" (?Draw2@Framework@@QAEXPAK@Z)
1>Framework.obj : error LNK2019: unresolved external symbol "public: struct ID3D11BlendState * __cdecl DirectX::CommonStates::NonPremultiplied(void)const " (?NonPremultiplied@CommonStates@DirectX@@QBAPAUID3D11BlendState@@XZ) referenced in function "public: void __thiscall Framework::Draw2(unsigned long *)" (?Draw2@Framework@@QAEXPAK@Z)
1>Framework.obj : error LNK2019: unresolved external symbol "public: void __vectorcall DirectX::SpriteBatch::Begin(enum DirectX::SpriteSortMode,struct ID3D11BlendState *,struct ID3D11SamplerState *,struct ID3D11DepthStencilState *,struct ID3D11RasterizerState *,class std::function<void __cdecl(void)>,struct DirectX::XMMATRIX)" (?Begin@SpriteBatch@DirectX@@QAQXW4SpriteSortMode@2@PAUID3D11BlendState@@PAUID3D11SamplerState@@PAUID3D11DepthStencilState@@PAUID3D11RasterizerState@@V?$function@$$A6AXXZ@std@@UXMMATRIX@2@@Z) referenced in function "public: void __thiscall Framework::Draw2(unsigned long *)" (?Draw2@Framework@@QAEXPAK@Z)
1>Framework.obj : error LNK2019: unresolved external symbol "public: void __cdecl DirectX::SpriteBatch::End(void)" (?End@SpriteBatch@DirectX@@QAAXXZ) referenced in function "public: void __thiscall Framework::Draw2(unsigned long *)" (?Draw2@Framework@@QAEXPAK@Z)
1>Framework.obj : error LNK2019: unresolved external symbol "public: void __vectorcall DirectX::SpriteBatch::Draw(struct ID3D11ShaderResourceView *,struct DirectX::XMFLOAT2 const &,union __m128)" (?Draw@SpriteBatch@DirectX@@QAQXPAUID3D11ShaderResourceView@@ABUXMFLOAT2@2@T__m128@@@Z) referenced in function "public: void __thiscall Framework::Draw2(unsigned long *)" (?Draw2@Framework@@QAEXPAK@Z)
1>Framework.obj : error LNK2001: unresolved external symbol "private: static struct DirectX::XMMATRIX const DirectX::SpriteBatch::MatrixIdentity" (?MatrixIdentity@SpriteBatch@DirectX@@0UXMMATRIX@2@B)

我认为这是一些链接器错误,所以我将 DirectXTK.lib 文件的路径添加到链接器>常规>附加依赖项。我没有找到 CommonStates.lib 和 SpriteBatch.lib 文件,即使我编译了 DirectXTK-master 项目,唯一生成的库是 DirectXTK.lib。

因此,我将 DirectXTK.lib 添加到 Linker>Input>Additional Dependencies。 但是,我仍然出现以下错误:

1>DirectXTK.lib(CommonStates.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in AddForceMessage.obj
1>DirectXTK.lib(CommonStates.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in AddForceMessage.obj
1>DirectXTK.lib(SpriteBatch.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in AddForceMessage.obj
1>DirectXTK.lib(SpriteBatch.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in AddForceMessage.obj
1>DirectXTK.lib(pch.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in AddForceMessage.obj
1>DirectXTK.lib(pch.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in AddForceMessage.obj
1>DirectXTK.lib(VertexTypes.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in AddForceMessage.obj
1>DirectXTK.lib(VertexTypes.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in AddForceMessage.obj
1>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library

所以我真的不明白这是什么意思,你能告诉我我做错了什么以及我应该怎么做才能让它发挥作用吗? 非常感谢。

最佳答案

mismatch detected for '_ITERATOR_DEBUG_LEVEL'

这意味着您要链接到的库是在与用于构建当前程序的配置不同的配置(调试或发布)中构建的。

修复步骤:

  1. 在调试和发布中同时构建库。您将为其中的每一个(在不同的目录中)获得一个 DirectXTK.lib 文件。
  2. 在您的主程序中,选择 Debug 作为构建配置,转到 Linker>Input>Additional Dependencies 并添加您刚刚构建的 Debug DirectXTK.lib 文件。确保将 DirectXTK.lib 文件所在的目录添加到 VC++ Directories>Library Directories。
  3. 对 Release模式重复第 2 步。确保您一次只更改一个配置的这些配置属性。

现在,无论何时在 Debug 模式下构建程序,它都将链接到 Debug 库,对于 Release 也是如此。

关于c++ - DirectX C++ 的链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48010742/

相关文章:

c++ - 未解析的外部符号

c++ - 为什么 std::make_unique 等不存在 std::initializer_list 重载?

macos - 在 Mac OS X 上的 Fortran 中链接 LAPACK

c++ - 在 C++ 中将 DXVA2_Fixed32 类型转换为 Float

c++ - 使用 WinAPI 的应用程序设置

C++ 在使用 .o 和使用 .a 文件链接之间存在差异 : different behavior, 为什么?

创建不需要 DLL 或库的 .exe

c++ - 在 GPU 上测量时间

c++ - DirectX 拉伸(stretch)像素

c++ - 使用二维数组制作图形