我试图构建自己的Direct2D库,但是尝试使用它时会遇到很多链接器错误
我写了一个用于使用direct2d的静态库。它很好地编译了,没有警告;但是,当我尝试在示例项目中实现它时,我会收到大量的链接器错误:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol __imp_TranslateMessage referenced in function "private: int __cdecl Ice2D::Ice2DApp::ProcessMessages(void)" (?ProcessMessages@Ice2DApp@Ice2D@@AEAAHXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_DispatchMessageW referenced in function "private: int __cdecl Ice2D::Ice2DApp::ProcessMessages(void)" (?ProcessMessages@Ice2DApp@Ice2D@@AEAAHXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_PeekMessageW referenced in function "private: int __cdecl Ice2D::Ice2DApp::ProcessMessages(void)" (?ProcessMessages@Ice2DApp@Ice2D@@AEAAHXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_DefWindowProcW referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_PostQuitMessage referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_RegisterClassExW referenced in function "public: long __cdecl Ice2D::Ice2DApp::Initialize(void)" (?Initialize@Ice2DApp@Ice2D@@QEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_CreateWindowExW referenced in function "public: long __cdecl Ice2D::Ice2DApp::Initialize(void)" (?Initialize@Ice2DApp@Ice2D@@QEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_ShowWindow referenced in function "public: long __cdecl Ice2D::Ice2DApp::Initialize(void)" (?Initialize@Ice2DApp@Ice2D@@QEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_SetCapture referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_ReleaseCapture referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_UpdateWindow referenced in function "public: long __cdecl Ice2D::Ice2DApp::Initialize(void)" (?Initialize@Ice2DApp@Ice2D@@QEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_SetForegroundWindow referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_SetWindowTextW referenced in function "protected: void __cdecl Ice2D::Ice2DApp::SetWindowTitle(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >)" (?SetWindowTitle@Ice2DApp@Ice2D@@IEAAXV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_GetClientRect referenced in function "private: long __cdecl Ice2D::Ice2DApp::CreateDeviceResources(void)" (?CreateDeviceResources@Ice2DApp@Ice2D@@AEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_AdjustWindowRect referenced in function "public: long __cdecl Ice2D::Ice2DApp::Initialize(void)" (?Initialize@Ice2DApp@Ice2D@@QEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_GetWindowLongPtrW referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_SetWindowLongPtrW referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_LoadCursorW referenced in function "public: long __cdecl Ice2D::Ice2DApp::Initialize(void)" (?Initialize@Ice2DApp@Ice2D@@QEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK1120 18 unresolved externals Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\x64\Debug\Ice2D_Sample.exe 1
此外,如果我使用代码而不将其制成库(即将所有源文件复制到项目中),则可以正常工作。我尝试编译的示例项目非常简单,只有一个.cpp文件:
#include <Ice2DApp.h>
class SampleApp : public Ice2D::Ice2DApp
{
public:
void Settings() override;
void Setup() override;
void Update() override;
void Draw() override;
private:
Ice2D::TextFormat font1;
Ice2D::LinearBrush linBrush;
Ice2D::RadialBrush radBrush;
unsigned short int fps;
};
void SampleApp::Settings()
{
// Settings for window creation
SetWindowTitle(L"Sample Ice2D Window");
SetWindowSize(600, 300);
}
void SampleApp::Setup()
{
// Fill pointers and initialize game logic
CreateTextFomat(L"arial", 36.0f, font1);
Ice2D::GradientStop linStops[3] =
{
{0.0f, Ice2D::Color(Ice2D::Color::Red)},
{0.5f, Ice2D::Color(Ice2D::Color::Yellow)},
{1.0f, Ice2D::Color(Ice2D::Color::Green)}
};
Ice2D::GradientStop radStops[2] =
{
{0.0f, Ice2D::Color(Ice2D::Color::Yellow)},
{1.0f, Ice2D::Color(Ice2D::Color::Purple)}
};
CreateLinearBrush(linStops, 3u, linBrush, { 10, 150 }, { 254, 150 });
CreateRadialBrush(radStops, 2u, radBrush, { 0, 0 }, { 0, 0 }, { 30, 30 });
SetLinearBrush(linBrush);
SetRadialBrush(radBrush);
}
void SampleApp::Update()
{
// Update game logic
if (FrameCount() % 60 == 0)
{
fps = (unsigned short int)(1.0f / DeltaTime());
SetTextColor(fps <= 60 ? Ice2D::Color::Red : Ice2D::Color::Black);
}
radBrush->SetCenter(MakePoint(mouse.GetPos()));
}
void SampleApp::Draw()
{
// Execute Draw Commands
BeginDraw();
Clear(Ice2D::Color::CornflowerBlue);
FillRect(10, 100, fps * 2 + 10, 200, BrushMode::Linear);
DrawRect(10, 100, 500, 200, 2.0f);
FillEllipse(MakePoint(mouse.GetPos()), 30, 30, BrushMode::Radial);
RenderText(L"FPS: " + std::to_wstring(fps), font1, 10, 10, 300, 50);
EndDraw();
}
int WINAPI WinMain(
HINSTANCE /* hInstance */,
HINSTANCE /* hPrevInstance */,
LPSTR /* lpCmdLine */,
int /* nCmdShow */)
{
HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
if (SUCCEEDED(CoInitialize(NULL)))
{
SampleApp app1;
if (SUCCEEDED(app1.Initialize()))
{
app1.Start();
}
CoUninitialize();
}
return 0;
}
我认为问题要么与我汇编的方式,要么将其包括在示例项目中。我确保他们使用的是相同的编译器版本和运行时库。我尝试了动态链接,但这给了我其他一组问题(.dll文件损坏)。顺便说一下,我正在使用Visual Studio 2022。
I wrote a static library for using Direct2D. It compiled fine, with no warnings; but when I tried to implement it in a sample project, I get loads of linker errors:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol __imp_TranslateMessage referenced in function "private: int __cdecl Ice2D::Ice2DApp::ProcessMessages(void)" (?ProcessMessages@Ice2DApp@Ice2D@@AEAAHXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_DispatchMessageW referenced in function "private: int __cdecl Ice2D::Ice2DApp::ProcessMessages(void)" (?ProcessMessages@Ice2DApp@Ice2D@@AEAAHXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_PeekMessageW referenced in function "private: int __cdecl Ice2D::Ice2DApp::ProcessMessages(void)" (?ProcessMessages@Ice2DApp@Ice2D@@AEAAHXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_DefWindowProcW referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_PostQuitMessage referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_RegisterClassExW referenced in function "public: long __cdecl Ice2D::Ice2DApp::Initialize(void)" (?Initialize@Ice2DApp@Ice2D@@QEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_CreateWindowExW referenced in function "public: long __cdecl Ice2D::Ice2DApp::Initialize(void)" (?Initialize@Ice2DApp@Ice2D@@QEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_ShowWindow referenced in function "public: long __cdecl Ice2D::Ice2DApp::Initialize(void)" (?Initialize@Ice2DApp@Ice2D@@QEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_SetCapture referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_ReleaseCapture referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_UpdateWindow referenced in function "public: long __cdecl Ice2D::Ice2DApp::Initialize(void)" (?Initialize@Ice2DApp@Ice2D@@QEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_SetForegroundWindow referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_SetWindowTextW referenced in function "protected: void __cdecl Ice2D::Ice2DApp::SetWindowTitle(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >)" (?SetWindowTitle@Ice2DApp@Ice2D@@IEAAXV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_GetClientRect referenced in function "private: long __cdecl Ice2D::Ice2DApp::CreateDeviceResources(void)" (?CreateDeviceResources@Ice2DApp@Ice2D@@AEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_AdjustWindowRect referenced in function "public: long __cdecl Ice2D::Ice2DApp::Initialize(void)" (?Initialize@Ice2DApp@Ice2D@@QEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_GetWindowLongPtrW referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_SetWindowLongPtrW referenced in function "private: __int64 __cdecl Ice2D::Ice2DApp::HandleMsg(struct HWND__ *,unsigned int,unsigned __int64,__int64)" (?HandleMsg@Ice2DApp@Ice2D@@AEAA_JPEAUHWND__@@I_K_J@Z) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK2019 unresolved external symbol __imp_LoadCursorW referenced in function "public: long __cdecl Ice2D::Ice2DApp::Initialize(void)" (?Initialize@Ice2DApp@Ice2D@@QEAAJXZ) Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\Ice2D.lib(Ice2DApp.obj) 1
Error LNK1120 18 unresolved externals Ice2D_Sample C:\Users\jkjt1\source\repos\C++\Ice2D_Sample\x64\Debug\Ice2D_Sample.exe 1
Additionally, if I use the code without making it a library (i.e. copying all the source files into the project), it works just fine. The sample project I tried to compile is fairly simple and has only one .cpp file:
#include <Ice2DApp.h>
class SampleApp : public Ice2D::Ice2DApp
{
public:
void Settings() override;
void Setup() override;
void Update() override;
void Draw() override;
private:
Ice2D::TextFormat font1;
Ice2D::LinearBrush linBrush;
Ice2D::RadialBrush radBrush;
unsigned short int fps;
};
void SampleApp::Settings()
{
// Settings for window creation
SetWindowTitle(L"Sample Ice2D Window");
SetWindowSize(600, 300);
}
void SampleApp::Setup()
{
// Fill pointers and initialize game logic
CreateTextFomat(L"arial", 36.0f, font1);
Ice2D::GradientStop linStops[3] =
{
{0.0f, Ice2D::Color(Ice2D::Color::Red)},
{0.5f, Ice2D::Color(Ice2D::Color::Yellow)},
{1.0f, Ice2D::Color(Ice2D::Color::Green)}
};
Ice2D::GradientStop radStops[2] =
{
{0.0f, Ice2D::Color(Ice2D::Color::Yellow)},
{1.0f, Ice2D::Color(Ice2D::Color::Purple)}
};
CreateLinearBrush(linStops, 3u, linBrush, { 10, 150 }, { 254, 150 });
CreateRadialBrush(radStops, 2u, radBrush, { 0, 0 }, { 0, 0 }, { 30, 30 });
SetLinearBrush(linBrush);
SetRadialBrush(radBrush);
}
void SampleApp::Update()
{
// Update game logic
if (FrameCount() % 60 == 0)
{
fps = (unsigned short int)(1.0f / DeltaTime());
SetTextColor(fps <= 60 ? Ice2D::Color::Red : Ice2D::Color::Black);
}
radBrush->SetCenter(MakePoint(mouse.GetPos()));
}
void SampleApp::Draw()
{
// Execute Draw Commands
BeginDraw();
Clear(Ice2D::Color::CornflowerBlue);
FillRect(10, 100, fps * 2 + 10, 200, BrushMode::Linear);
DrawRect(10, 100, 500, 200, 2.0f);
FillEllipse(MakePoint(mouse.GetPos()), 30, 30, BrushMode::Radial);
RenderText(L"FPS: " + std::to_wstring(fps), font1, 10, 10, 300, 50);
EndDraw();
}
int WINAPI WinMain(
HINSTANCE /* hInstance */,
HINSTANCE /* hPrevInstance */,
LPSTR /* lpCmdLine */,
int /* nCmdShow */)
{
HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
if (SUCCEEDED(CoInitialize(NULL)))
{
SampleApp app1;
if (SUCCEEDED(app1.Initialize()))
{
app1.Start();
}
CoUninitialize();
}
return 0;
}
I think the issue is either with how I compiled it or how I included it in the sample project. I've made sure that they're using the same compiler version and runtime library. I have tried dynamically linking, but that gives me a whole other set of issues (the .dll file was corrupt). I'm using Visual Studio 2022, by the way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于其他遇到相同问题的任何人(并且不介意切换到动态链接的库),我通过在需要在示例项目中访问的任何功能之前添加
__ extspec(dllexport)
来解决问题。我使用宏来做到这一点:#define Ice2d_api __declspec(dllexport)
。To anyone else having the same problem (and don't mind switching to a dynamically linked library), I fixed the problem by adding
__declspec(dllexport)
before any function I needed to access in the sample project. I used a macro to do this:#define ICE2D_API __declspec(dllexport)
.