Search code examples
apiassemblyx86masmmasm32

How to fix masm32: error LNK2001: unresolved external symbol


I'm creating an app to test an api func IsCharLowerA and then output res using MessageBoxA. I'm using 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"

I've tried to use microsoft masm32 (to compile code in visual studio), but when the app starts it's only ask for a char and then closes. I could not try to debug due to "source not available" error.

Some code:

.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

Update #1: (changed MessageBoxA -> MessageBoxA@16 and others)

Code:

.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

Res:

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

I have a code error LNK2001: unresolved external symbol _MessageBox (uploaded #2 "Final working code") is not succeed linking too


Solution

  • The problems was:

    • 1) For scanf needed OFFSET scan_res, because not value is needed.
    • 2) Can not be compiled using both windows and console subsystem. That's why i used only printfs.

    • 3) Was not enough library msvcrt.

    • 4) Сonverting from OMF to COFF could be ignored.

    Working code:

    .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