【发布时间】:2011-11-16 06:38:16
【问题描述】:
我正在尝试链接使用在 winuser.h 中声明并在 User32.dll 中定义的两种方法的目标文件:GetMonitorInfo 和 WindowFromMonitor。源编译成目标文件就好了,但是当我尝试链接时,我得到以下错误输出:
D3dCtx.obj : error LNK2001: unresolved external symbol xGetMonitorInfo
D3dCtx.obj : error LNK2001: unresolved external symbol xMonitorFromWindow
问题是,我不叫“xGetMonitorInfo”或“xMonitorFromWindow”。对所有源文件运行 grep 显示只有“GetMonitorInfo”和“WindowFromMonitor”被调用。我正确地包括了 windows.h,其中包括 winuser.h。我还在链接器选项中正确设置了我的 LIBPATH,这由详细链接输出确认。
以下内容也出现在我的详细链接输出中:
Found __imp_GetMonitorInfoA
Referenced in nafxcw.lib(afxribboncategory.obj)
Referenced in nafxcw.lib(afxtooltipctrl.obj)
Referenced in nafxcw.lib(afxribbonkeytip.obj)
Referenced in nafxcw.lib(afxfullscreenimpl.obj)
Referenced in nafxcw.lib(afxframeimpl.obj)
Referenced in nafxcw.lib(afxglobalutils.obj)
Referenced in nafxcw.lib(afxdropdowntoolbar.obj)
Referenced in nafxcw.lib(wincore.obj)
Referenced in nafxcw.lib(afxglobals.obj)
Referenced in nafxcw.lib(afxpopupmenu.obj)
Referenced in nafxcw.lib(afxpropertygridtooltipctrl.obj)
Loaded User32.lib(USER32.dll)
Found __imp_MonitorFromWindow
Referenced in nafxcw.lib(wincore.obj)
Loaded User32.lib(USER32.dll)
此外,GetMonitorInfo 在 winuser.h 中定义为:
WINUSERAPI
BOOL
WINAPI
GetMonitorInfoA(
__in HMONITOR hMonitor,
__inout LPMONITORINFO lpmi);
WINUSERAPI
BOOL
WINAPI
GetMonitorInfoW(
__in HMONITOR hMonitor,
__inout LPMONITORINFO lpmi);
#ifdef UNICODE
#define GetMonitorInfo GetMonitorInfoW
#else
#define GetMonitorInfo GetMonitorInfoA
#endif // !UNICODE
当我将所有对“GetMonitorInfo”的引用更改为“GetMonitorInfoA”时,我只会得到
D3dCtx.obj:错误 LNK2001:无法解析的外部符号 xMonitorFromWindow
作为我的链接器错误输出。不幸的是,MonitorFromWindow 似乎没有多个版本可用。
请注意,我使用的是 64 位版本的库、链接和 cl。
这是怎么回事,如何才能成功链接我的程序?
【问题讨论】:
-
江湖郎中喜欢宏麻烦。 grep x##
-
好吧,我没有找到导致这种情况的宏,只有一个文件引用了这些函数。但是,您的评论确实激发了我使用宏来解决这个问题。这不是最好的解决方案(这让我有点受伤),但这段代码真的只需要编译,而不是维护。
标签: windows visual-c++ dll linker