链接器错误与DirectX C ++

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了链接器错误与DirectX C ++相关的知识,希望对你有一定的参考价值。

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

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

我将这些标题的链接添加到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项目,唯一生成的lib是DirectXTK.lib。

因此,我将DirectXTK.lib添加到链接器>输入>附加依赖项。但是,我仍然有以下错误:

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. 在Debug和Release中构建库。您将获得每个(在不同目录中)的DirectXTK.lib文件。
  2. 在主程序中,选择Debug作为构建配置,转到Linker> Input> Additional Dependencies并添加刚刚构建的Debug DirectXTK.lib文件。确保将DirectXTK.lib文件所在的目录添加到VC ++目录>库目录。
  3. 对释放模式重复步骤2。确保您一次只为一个配置更改这些配置属性。

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

以上是关于链接器错误与DirectX C ++的主要内容,如果未能解决你的问题,请参考以下文章

链接器错误 2019 C++,VS2008

关于工作C ++ 03代码的G ++(C ++ 14)链接器错误

无法将 c# .Net Core 3.0 与 directx 9.0 依赖项链接

GLSL/C++:自定义着色器的默认行为

Apple Mach-O链接器(id)错误 - 链接器命令失败,退出代码为1(使用-v查看调用)C ++ SFML

C 文件中的 C++ 文件中的链接器错误调用函数