Как исправить masm32: ошибка LNK2001: неразрешенный внешний символ - PullRequest
0 голосов
/ 10 ноября 2019

Я создаю приложение для тестирования API-функции IsCharLowerA, а затем выводю res с помощью MessageBoxA. Я использую 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), но когда приложение запускается, оно только запрашивает char, а затем закрывается. Я не смог попробовать отладить из-за ошибки «источник недоступен».

Некоторый код:

.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

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

У меня есть код ошибка LNK2001: неразрешенный внешний символ _MessageBox (загруженный # 2 "Окончательный рабочий код") тоже не удается связать

1 Ответ

0 голосов
/ 12 ноября 2019

Проблемы были:

  • 1) Для scanf необходимо OFFSET scan_res, потому что не требуется значение.
  • 2) Не может быть скомпилировано с использованием как окон, так и консольной подсистемы. Вот почему я использовал только 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
...