MFC 静态链接未解析的外部符号

Posted

技术标签:

【中文标题】MFC 静态链接未解析的外部符号【英文标题】:MFC Static Linking Unresolved External Symbol 【发布时间】:2013-12-20 00:38:51 【问题描述】:

我正在尝试使用 Use of MFC:Use MFC in a Static Library 构建自定义版本的 GyazoWin,这会导致大量参考错误。

如何安抚 Visual Studio,以便我可以使用静态链接库进行构建?

更新

我设法将 user32 添加到链接库中(我认为),现在我的错误数量只有 1/3。谁能告诉我现在缺少什么库?

Error   2   error LNK2019: unresolved external symbol __imp__BitBlt@36 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)  S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   3   error LNK2019: unresolved external symbol __imp__CreateCompatibleBitmap@12 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)  S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   4   error LNK2019: unresolved external symbol __imp__CreateCompatibleDC@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)   S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   5   error LNK2019: unresolved external symbol __imp__CreateFontW@56 referenced in function "long __stdcall LayerWndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?LayerWndProc@@YGJPAUHWND__@@IIJ@Z)   S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   6   error LNK2019: unresolved external symbol __imp__CreatePen@12 referenced in function "long __stdcall LayerWndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?LayerWndProc@@YGJPAUHWND__@@IIJ@Z) S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   7   error LNK2019: unresolved external symbol __imp__CreateSolidBrush@4 referenced in function "long __stdcall LayerWndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?LayerWndProc@@YGJPAUHWND__@@IIJ@Z)   S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   8   error LNK2019: unresolved external symbol __imp__DeleteDC@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   9   error LNK2019: unresolved external symbol __imp__DeleteObject@4 referenced in function "long __stdcall LayerWndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?LayerWndProc@@YGJPAUHWND__@@IIJ@Z)   S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   10  error LNK2019: unresolved external symbol __imp__GetDeviceCaps@8 referenced in function "long __stdcall LayerWndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?LayerWndProc@@YGJPAUHWND__@@IIJ@Z)  S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   11  error LNK2019: unresolved external symbol __imp__GetStockObject@4 referenced in function "unsigned short __cdecl MyRegisterClass(struct HINSTANCE__ *)" (?MyRegisterClass@@YAGPAUHINSTANCE__@@@Z)   S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   12  error LNK2019: unresolved external symbol __imp__Rectangle@20 referenced in function "long __stdcall LayerWndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?LayerWndProc@@YGJPAUHWND__@@IIJ@Z) S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   13  error LNK2019: unresolved external symbol __imp__SelectObject@8 referenced in function "long __stdcall LayerWndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?LayerWndProc@@YGJPAUHWND__@@IIJ@Z)   S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   14  error LNK2019: unresolved external symbol __imp__SetBkMode@8 referenced in function "long __stdcall LayerWndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?LayerWndProc@@YGJPAUHWND__@@IIJ@Z)  S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   15  error LNK2019: unresolved external symbol __imp__SetTextColor@8 referenced in function "long __stdcall LayerWndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?LayerWndProc@@YGJPAUHWND__@@IIJ@Z)   S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   16  error LNK2019: unresolved external symbol __imp__TextOutW@20 referenced in function "long __stdcall LayerWndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?LayerWndProc@@YGJPAUHWND__@@IIJ@Z)  S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   17  error LNK2019: unresolved external symbol __imp__ShellExecuteExW@4 referenced in function "void __cdecl execUrl(char const *)" (?execUrl@@YAXPBD@Z) S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   18  error LNK2019: unresolved external symbol __imp__SHGetSpecialFolderPathW@16 referenced in function "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl getId(void)" (?getId@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\gyazowin\gyazowin.obj gyazowin
Error   19  error LNK1120: 17 unresolved externals  S:\Software\Microsoft\Visual Studio\Projects\Completed (New)\gyazowin\Debug\gyazowin.exe    gyazowin

【问题讨论】:

这看起来很可疑,好像 user32.lib 和 shell32.lib 不在您的链接行中。 我用的C++不多,怎么添加文件?如果它解决了我的问题,那么它就是一个答案。 你需要 gdi32.lib 【参考方案1】:

对于来自平台库的功能:

    使用您喜欢的搜索引擎搜索函数的名称(例如“GetStockObject”)。

    在 MSDN 上查找该函数的文档(例如 GetStockObject)。它通常是最好的结果之一。

    在文档中的“要求”下,找到“库”条目。那是您必须链接到的库才能使用该函数(对于GetStockObject,它是 gdi32.lib)。

    重复直到解决所有错误。

【讨论】:

在我的情况下,我需要 accept 不包括 winsock2 - 在文件中使用 #pragma comment(lib, "Ws2_32.lib")

以上是关于MFC 静态链接未解析的外部符号的主要内容,如果未能解决你的问题,请参考以下文章

计算机系统篇之链接:静态链接(中)——符号解析

静态链接

MSVC 中的链接错误 LNK2019,带有 __imp__ 前缀的未解析符号,但应该来自静态库

未解析的外部符号静态变量(头文件中定义的方法使用的变量)

链接器如何使用静态库解析引用

MFC C++ 使用静态成员和编译问题