如何修复 masm32:错误 LNK2001:未解析的外部符号

How to fix masm32: error LNK2001: unresolved external symbol

我正在创建一个应用程序来测试 api func IsCharLowerA,然后使用 MessageBoxA 输出 res。我正在使用 masm32.

link /SUBSYSTEM:WINDOWS kod.obj
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

kod.obj : warning LNK4033: converting object format from OMF to COFF
kod.obj : error LNK2001: unresolved external symbol __ExitProcess@4
kod.obj : error LNK2001: unresolved external symbol __MessageBoxA@16
kod.obj : error LNK2001: unresolved external symbol __imp__printf
kod.obj : error LNK2001: unresolved external symbol __imp__scanf
kod.obj : error LNK2001: unresolved external symbol _IsCharLowerA@4
LINK : error LNK2001: unresolved external symbol _WinMainCRTStartup
ml kod.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: kod.asm
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/z2
"kod.obj"
"kod.exe"
NUL
LINK : warning LNK4044: unrecognized option "z2"; ignored
kod.obj : warning LNK4033: converting object format from OMF to COFF
LINK : fatal error LNK1181: cannot open input file "kod.exe"

我试过使用 microsoft masm32(在 visual studio 中编译代码),但是当应用程序启动时它只要求一个字符然后关闭。由于 "source not available" 错误,我无法尝试调试。

一些代码:

.586 
.model flat, stdcall 
option casemap: none 

include C:\masm32\include\windows.inc 
include C:\masm32\include\kernel32.inc 
include C:\masm32\include\user32.inc 
include C:\masm32\include\msvcrt.inc

includelib C:\masm32\lib\msvcrt.lib
includelib C:\masm32\lib\kernel32.lib
includelib C:\masm32\lib\user32.lib 

.data
msg db 'Enter char: ', 0
messagebox_title db ' Лабораторна робота № 5 ', 0
result_0 db ' NOT LOWERCASE ', 0
result_1 db ' LOWERCASE ', 0
scan_modifier db '%c', 0    

.data?
scan_res dd ?

.code
start:  
    push ebp
    mov ebp, esp

    invoke crt_printf, OFFSET msg

    invoke crt_scanf, OFFSET scan_modifier, scan_res

    push scan_res
    call IsCharLowerA

    push 0
    push offset messagebox_title        

    cmp eax, 0
    jne notNULL
    push offset result_0
    jmp next
notNULL:
    push offset result_1
next:
    push 0                           
    call MessageBoxA          

    push 0                        
    call ExitProcess   

    pop ebp
end start

更新 #1:(更改了 MessageBoxA -> MessageBoxA@16 和其他)

代码:

.586 
.model flat, stdcall 
option casemap: none 

include C:\masm32\include\windows.inc 
include C:\masm32\include\kernel32.inc 
include C:\masm32\include\user32.inc 
include C:\masm32\include\msvcrt.inc

includelib C:\masm32\lib\msvcrt.lib
includelib C:\masm32\lib\kernel32.lib
includelib C:\masm32\lib\user32.lib 

extrn MessageBoxA@16 : PROC
extrn ExitProcess@4 : PROC

.data
msg db 'Enter char: ', 0
messagebox_title db ' Лабораторна робота № 5 ', 0
result_0 db ' NOT LOWERCASE ', 0
result_1 db ' LOWERCASE ', 0
scan_modifier db '%c', 0    

.data?
scan_res dd ?

.code
start:  
    push ebp
    mov ebp, esp

    invoke crt_printf, OFFSET msg

    invoke crt_scanf, OFFSET scan_modifier, scan_res

    push scan_res
    call IsCharLowerA

    push 0
    push offset messagebox_title        

    cmp eax, 0
    jne notNULL
    push offset result_0
    jmp next
notNULL:
    push offset result_1
next:
    push 0                           
    call MessageBoxA@16          

    push 0                        
    call ExitProcess@4   

    pop ebp
end start

回复:

link /SUBSYSTEM:WINDOWS kod.obj
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

kod.obj : warning LNK4033: converting object format from OMF to COFF
kod.obj : error LNK2001: unresolved external symbol _MessageBoxA@16
kod.obj : error LNK2001: unresolved external symbol __imp__printf
kod.obj : error LNK2001: unresolved external symbol _ExitProcess@4
kod.obj : error LNK2001: unresolved external symbol __imp__scanf
kod.obj : error LNK2001: unresolved external symbol _IsCharLowerA@4
LINK : error LNK2001: unresolved external symbol _WinMainCRTStartup
kod.exe : fatal error LNK1120: 6 unresolved externals

我有一个代码error LNK2001: unresolved external symbol _MessageBox(上传#2 "Final working code")也没有成功链接

问题是:

  • 1) 对于 scanf 需要 OFFSET scan_res,因为不需要值。
  • 2) 无法同时使用 windows 和控制台子系统进行编译。这就是为什么我只使用 printfs.

  • 3) 库 msvcrt 不够。

  • 4) OMF转COFF可以忽略

工作代码:

.586 
.model flat, stdcall 
option casemap: none 

include C:\masm32\include\windows.inc 
include C:\masm32\include\user32.inc 
include C:\masm32\include\msvcrt.inc

includelib C:\masm32\lib\msvcrt.lib
includelib C:\masm32\lib\user32.lib 

.data
msg db 'Enter char: ', 0
result_0 db ' NOT LOWERCASE ', 0
result_1 db ' LOWERCASE ', 0
scan_modifier db '%c', 0    

.data?
scan_res dd ?

.code
start:  
    mov ebp, esp

    invoke crt_printf, OFFSET msg

    invoke crt_scanf, OFFSET scan_modifier, OFFSET scan_res

    push scan_res
    call IsCharLowerA    

    cmp eax, 0
    jne notNULL
    invoke crt_printf, OFFSET result_0
    jmp next
notNULL:
    invoke crt_printf, OFFSET result_1
next:                                                         
    ret
end start