链接到 User32.dll 时出现链接错误 2001

发布于 2024-12-04 12:00:47 字数 1918 浏览 0 评论 0原文

我正在尝试链接一个对象文件,该文件使用在 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。

这是怎么回事?我怎样才能成功链接我的程序?

I'm trying to link an object file that uses two methods declared in winuser.h and defined in User32.dll: GetMonitorInfo and WindowFromMonitor. The source compiles to an object file just fine, but when I try to link, I get the following error output:

D3dCtx.obj : error LNK2001: unresolved external symbol xGetMonitorInfo
D3dCtx.obj : error LNK2001: unresolved external symbol xMonitorFromWindow

The thing is, I don't call "xGetMonitorInfo" or "xMonitorFromWindow". Running grep on all source files shows that only "GetMonitorInfo" and "WindowFromMonitor" are being called. I'm properly including windows.h, which includes winuser.h. I'm also properly setting my LIBPATH in the linker options, which is confirmed by verbose link output.

The following also appears in my verbose link output:

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)

Furthermore, GetMonitorInfo is defined in winuser.h as:

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

When I change all reference to "GetMonitorInfo" to "GetMonitorInfoA", I only get

D3dCtx.obj : error LNK2001: unresolved external symbol xMonitorFromWindow

as my linker error output. Unfortunately, MonitorFromWindow doesn't seem to have multiple versions available.

I should note that I am using the 64bit versions of the libraries, link, and cl.

What's going on here, and how can I successfully link my program?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇色安年 2024-12-11 12:00:47

我不知道您是否能够找到解决方案,但我遇到了同样的问题,发生这种情况的原因是我包含一个名为 multimon.h 的文件,

看起来以防万一在 64 位编译中,由于宏定义,这些函数的定义来自两个源,并且可能来自 multimon.h 的一个被覆盖并且是错误的。

我通过注释掉这个包含来解决它,并且它已经开始正常链接。

//#include <multimon.h>

I don't know whether you were able to find solution to this or not But I had the same problem and the reason this was happening was that I had a file included named multimon.h

Looks like in case of 64 bit compilation, due to Macro definitions, the definitions of these functions are coming from two sources and probably one from multimon.h is overriding and is wrong.

I solved it by just commenting out this include and it has started to link fine.

//#include <multimon.h>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文