错误LNK2001:无法解析的外部符号_MessageBox
我正在尝试仅使用 masm 而不是 masm32 库创建一个 helloworld 程序。这是代码片段:
.386
.model flat, stdcall
option casemap :none
extrn MessageBox : PROC
extrn ExitProcess : PROC
.data
HelloWorld db "Hello There!", 0
.code
start:
lea eax, HelloWorld
mov ebx, 0
push ebx
push eax
push eax
push ebx
call MessageBox
push ebx
call ExitProcess
end start
我可以使用 masm 组装它:
c:\masm32\code>ml /c /coff demo.asm
Microsoft (R) Macro Assembler Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
Assembling: demo.asm
但是,我无法链接它:
c:\masm32\code>link /subsystem:windows /defaultlib:kernel32.lib /defaultlib:user
32.lib demo.obj
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
demo.obj : error LNK2001: unresolved external symbol _MessageBox
demo.obj : error LNK2001: unresolved external symbol _ExitProcess
demo.exe : fatal error LNK1120: 2 unresolved externals
我在链接期间包含了库,所以不确定为什么它仍然显示未解析的符号?
更新:
c:\masm32\code>link /subsystem:windows /defaultlib:kernel32.lib /defaultlib:user
32.lib demo.obj
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
demo.obj : error LNK2001: unresolved external symbol _MessageBox@16
demo.exe : fatal error LNK1120: 1 unresolved externals
更新 2:最终工作代码!
.386
.model flat, stdcall
option casemap :none
extrn MessageBoxA@16 : PROC
extrn ExitProcess@4 : PROC
.data
HelloWorld db "Hello There!", 0
.code
start:
lea eax, HelloWorld
mov ebx, 0
push ebx
push eax
push eax
push ebx
call MessageBoxA@16
push ebx
call ExitProcess@4
end start
I am trying to create a helloworld program using only masm and not masm32 libs. Here is the code snippet:
.386
.model flat, stdcall
option casemap :none
extrn MessageBox : PROC
extrn ExitProcess : PROC
.data
HelloWorld db "Hello There!", 0
.code
start:
lea eax, HelloWorld
mov ebx, 0
push ebx
push eax
push eax
push ebx
call MessageBox
push ebx
call ExitProcess
end start
I am able to assemble this using masm:
c:\masm32\code>ml /c /coff demo.asm
Microsoft (R) Macro Assembler Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
Assembling: demo.asm
However, I am unable to link it:
c:\masm32\code>link /subsystem:windows /defaultlib:kernel32.lib /defaultlib:user
32.lib demo.obj
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
demo.obj : error LNK2001: unresolved external symbol _MessageBox
demo.obj : error LNK2001: unresolved external symbol _ExitProcess
demo.exe : fatal error LNK1120: 2 unresolved externals
I am including the libs during linking, so not sure why it still says unresolved symbols?
UPDATE:
c:\masm32\code>link /subsystem:windows /defaultlib:kernel32.lib /defaultlib:user
32.lib demo.obj
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
demo.obj : error LNK2001: unresolved external symbol _MessageBox@16
demo.exe : fatal error LNK1120: 1 unresolved externals
UPDATE 2: Final working code!
.386
.model flat, stdcall
option casemap :none
extrn MessageBoxA@16 : PROC
extrn ExitProcess@4 : PROC
.data
HelloWorld db "Hello There!", 0
.code
start:
lea eax, HelloWorld
mov ebx, 0
push ebx
push eax
push eax
push ebx
call MessageBoxA@16
push ebx
call ExitProcess@4
end start
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的函数名称是
MessageBoxA@16
和ExitProcess@4
。几乎所有 Win32 API 函数都是 stdcall,因此它们的名称都用
@
符号,后跟其参数所占用的字节数。此外,当 Win32 函数采用字符串时,有两种变体:一种采用 ANSI 字符串(名称以
A
结尾),另一种采用 Unicode 字符串(名称以W< /代码>)。您提供的是 ANSI 字符串,因此您需要
A
版本。当您不使用汇编语言进行编程时,编译器会为您处理这些问题。
The correct function names are
MessageBoxA@16
andExitProcess@4
.Almost all Win32 API functions are stdcall, so their names are decorated with an
@
sign, followed by the number of bytes taken up by their parameters.Additionally, when a Win32 function takes a string, there are two variants: one that takes an ANSI string (name ends in
A
) and one that takes a Unicode string (name ends inW
). You're supplying an ANSI string, so you want theA
version.When you're not programming in assembly the compiler takes care of these points for you.
尝试在
.data
段之前添加此内容:Try adding this before
.data
segment: