【发布时间】:2015-06-24 21:14:37
【问题描述】:
我正在尝试从 cpp 文件中的 asm 文件运行程序,但出现此类错误:
Error 1 error LNK2019: unresolved external symbol _calka referenced in function _main D:\Addem\main.obj Project
Error 2 error LNK1120: 1 unresolved externals D:\Addem\Debug\Project.exe 1 1 Project
这是我的 main.cpp:
#include <iostream>
using namespace std;
extern "C" void calka();
int main()
{
calka();
system("Pause");
return 0;
}
还有我的 calka.asm
; The calka Subroutine (calka.asm)
public _calka
INCLUDELIB kernel32.lib
INCLUDELIB user32.lib
INCLUDELIB Irvine32.lib
INCLUDE Irvine32.inc
INCLUDE macros.inc
.data
BUFFER_SIZE = 50
buffer BYTE BUFFER_SIZE DUP(?)
filename BYTE 80 DUP(0)
fileHandle HANDLE ?
Rozmiar dw ?
Znak db ?
Suma dw ?
Minus db 0
wynik dw ?
temp dd ?
.code
_calka proc near
; Let user input a filename.
mWrite "Podaj nazwe pliku (z rozszerzeniem): "
mov edx,OFFSET filename
mov ecx,SIZEOF filename
call ReadString
; Open the file for input.
mov edx,OFFSET filename
call OpenInputFile
mov fileHandle,eax
; Check for errors.
cmp eax,INVALID_HANDLE_VALUE ; error opening file?
jne file_ok ; no: skip
mWrite <"Cannot open file",0dh,0ah>
jmp quit ; and quit
file_ok:
; Read the file into a buffer.
mov edx,OFFSET buffer
mov ecx,BUFFER_SIZE
call ReadFromFile
jnc check_buffer_size ; error reading?
mWrite "Error reading file. " ; yes: show error message
call WriteWindowsMsg
jmp close_file
check_buffer_size:
cmp eax,BUFFER_SIZE ; buffer large enough?
jb buf_size_ok ; yes
mWrite <"Error: Buffer too small for the file",0dh,0ah>
jmp quit ; and quit
buf_size_ok:
mov buffer[eax],0 ; insert null terminator
;mWrite "File size: "
;call WriteDec ; display file size
;call Crlf
; Display the buffer.
; Pytanie o wielkosc przedzialu
mWrite "Podaj wielkosc przedzialu h na osi x: "
mov edx,OFFSET Rozmiar
mov ecx,SIZEOF Rozmiar
call ReadDec
mov Rozmiar, ax
;mWrite <"Buffer:",0dh,0ah,0dh,0ah>
;mov edx,OFFSET buffer ; display the buffer
;call WriteString
;call Crlf
mov ecx, 15
mov ebx, OFFSET buffer
mov si, 0
jmp odczyt1
odczyt:
mov ebx, temp
inc ebx
odczyt1:
mov cl, byte ptr [ebx]
mov Znak, cl
mov temp, ebx
cmp Znak, '$' ;koniec pliku
je koniec
cmp Znak, 3bh ;srednik - nastepna liczba
je nastepna
cmp Znak, 20h ;spacja - ponowne wczytanie
je odczyt
cmp Znak, '-'
je Zmien_znak
mov al, Znak
sub al, 30h
mov bl, al
mov bh, 0
mov ax, 10
mul si
add ax, bx
mov si, ax
jmp odczyt
Zmien_znak:
mov Minus, 1
jmp odczyt
nastepna:
cmp Minus, 0
je dodaj
jne odejm
dodaj:
add Suma, si
mov si, 0
jmp odczyt
odejm:
sub Suma, si
mov Minus, 0
mov si, 0
jmp odczyt
koniec:
cmp Minus, 0
je dod
jne minu
dod:
add Suma, si
mov si, 0
jmp kon
minu:
sub Suma, si
mov Minus, 0
mov si, 0
kon:
mov ax, Suma
mul Rozmiar
mWrite "Wynik calkowania: "
cmp ax,0
;mov ebx, eax
jg plus
mWrite "-"
mov bx, 0
sub bx, ax
mov ax, bx
plus:
call WriteDec
close_file:
mov eax,fileHandle
call CloseFile
; Zeby aplikacja sie nie zamknela, heheszki :D
mov edx,OFFSET Rozmiar
mov ecx,SIZEOF Rozmiar
call ReadString
quit:
exit
_calka ENDP
END
我已经尝试了所有我可以关注的事情——将 obj 文件添加到链接器等等。我只想输入文件名,从中读取日期并计算它。 Calka.asm 运行良好,除非我尝试让它在我的 cpp 文件中工作。
我做错了什么?
【问题讨论】:
-
adding obj files to linker- 那么,您添加了calka.obj,它是通过先组装创建的吗? VS 是否可能向您显示它正在运行的命令,如果是,是否包括calka.obj? -
我已经清除了我的解决方案,然后我在解决方案资源管理器中右键单击编译 calka.asm。之后,我添加了带有“添加现有项目”选项的 calka.obj - 我在构建项目后得到的是:
Debug\calka.obj : warning LNK4042: object specified more than once; extras ignored还有更多事情要做吗? -
我不确定“添加现有项目”是否是添加目标文件的方式,在链接器/编译器选项的某处项目属性中没有特定选项卡吗?也许是一样的,只是猜测。
-
如何正确操作?我已经尝试了很多方法,但没有任何方法让它起作用:)。
-
据此:link 包含方法没有区别...
标签: c++ assembly visual-studio-2013 masm irvine32