链接程序集和 C++ 时未定义的引用使用 g++
为了考试,我必须在 Assembly 中编写一个类的一些成员函数。我已经遵循了每一条指示,但我仍然无法让它工作。这是相关文件。标头和主要方法已经提供,我只需要编写构造函数和elab1
方法。
类头
#include <iostream>
using namespace std;
struct st { int a; int vv1[4]; double vv2[4]; };
class cl
{ double b; st s;
public:
cl(int *p, double *d);
void elab1(st ss);
void stampa()
{ int i; cout << b << ' ' << s.a << endl;
for (i=0;i<4;i++) cout << s.vv1[i] << ' '; cout << '\t';
for (i=0;i<4;i++) cout << s.vv2[i] << ' '; cout << endl;
cout << endl;
}
};
主要测试方法
// prova1.cpp
#include "cc.h" // class header
int main()
{
st s = {1, 1,2,3,4, 1,2,3,4 };
int v[4] = {10,11,12,13 };
double d[4] = { 2, 3, 4, 5 };
cl cc1(v, d);
cc1.stampa();
cc1.elab1(s);
cc1.stampa();
}
这是我的程序集:
# es1.s
.text
.global __ZN2clC1EPiPe
__ZN2clC1EPiPe:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
pushl %eax
pushl %ebx
pushl %ecx
pushl %edx
pushl %esi
cmpl $0, 12(%ebp)
je fine
cmpl $0, 16(%ebp)
je fine
movl 8(%ebp), %eax
movl 12(%ebp), %ebx
movl 4(%ebx), %ecx
movl %ecx, 12(%eax)
fldz
fstpl (%eax)
movl $0, -4(%ebp)
ciclo:
cmpl $4, -4(%ebp)
je fine
movl -4(%ebp), %esi
movl 12(%ebp), %ebx
movl (%ebx, %esi, 4), %ecx
subl %esi, %ecx
movl %ecx, 16(%eax, %esi, 4)
movl 16(%ebp), %ebx
pushl %eax
movl %esi, %eax
movl $3, %ecx
imull %ecx
movl %eax, %edx
popl %eax
movl 12(%ebp), %ecx
fldl (%ebx, %edx, 4)
fldl (%ecx, %esi, 4)
faddp %st, %st(1)
fstpl 32(%eax, %edx, 4)
fldl (%ebx, %edx, 4)
fldl (%eax)
faddp %st, %st(1)
fstpl (%eax)
incl -4(%ebp)
jmp ciclo
fine:
popl %esi
popl %edx
popl %ecx
popl %ebx
popl %eax
movl 8(%ebp), %eax
leave
ret
.global __ZN2cl5elab1E2st
__ZN2cl5elab1E2st: #TODO
我尝试使用提供的命令行语句来编译和链接所有内容给我们:
g++ -o es1 -fno-elide-constructors es1.s prova1.cpp
但它只给了我一堆未定义的参考
:
/tmp/ccbwS0uN.o: In function `main':
prova1.cpp:(.text+0xee): undefined reference to `cl::cl(int*, double*)'
prova1.cpp:(.text+0x192): undefined reference to `cl::elab1(st)'
collect2: ld returned 1 exit status
你知道如何解决这个问题吗?我想我可能以错误的方式翻译了函数名称,但我已经检查了几次。
I have to write some member functions of a class in Assembly for an exam. I've followed every instruction but I still can't get it to work. Here are the relevant files. The header and the main method are already provided, I just need to write the constructor and the elab1
method.
Class header
#include <iostream>
using namespace std;
struct st { int a; int vv1[4]; double vv2[4]; };
class cl
{ double b; st s;
public:
cl(int *p, double *d);
void elab1(st ss);
void stampa()
{ int i; cout << b << ' ' << s.a << endl;
for (i=0;i<4;i++) cout << s.vv1[i] << ' '; cout << '\t';
for (i=0;i<4;i++) cout << s.vv2[i] << ' '; cout << endl;
cout << endl;
}
};
Main method for testing
// prova1.cpp
#include "cc.h" // class header
int main()
{
st s = {1, 1,2,3,4, 1,2,3,4 };
int v[4] = {10,11,12,13 };
double d[4] = { 2, 3, 4, 5 };
cl cc1(v, d);
cc1.stampa();
cc1.elab1(s);
cc1.stampa();
}
And this is my assembly:
# es1.s
.text
.global __ZN2clC1EPiPe
__ZN2clC1EPiPe:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
pushl %eax
pushl %ebx
pushl %ecx
pushl %edx
pushl %esi
cmpl $0, 12(%ebp)
je fine
cmpl $0, 16(%ebp)
je fine
movl 8(%ebp), %eax
movl 12(%ebp), %ebx
movl 4(%ebx), %ecx
movl %ecx, 12(%eax)
fldz
fstpl (%eax)
movl $0, -4(%ebp)
ciclo:
cmpl $4, -4(%ebp)
je fine
movl -4(%ebp), %esi
movl 12(%ebp), %ebx
movl (%ebx, %esi, 4), %ecx
subl %esi, %ecx
movl %ecx, 16(%eax, %esi, 4)
movl 16(%ebp), %ebx
pushl %eax
movl %esi, %eax
movl $3, %ecx
imull %ecx
movl %eax, %edx
popl %eax
movl 12(%ebp), %ecx
fldl (%ebx, %edx, 4)
fldl (%ecx, %esi, 4)
faddp %st, %st(1)
fstpl 32(%eax, %edx, 4)
fldl (%ebx, %edx, 4)
fldl (%eax)
faddp %st, %st(1)
fstpl (%eax)
incl -4(%ebp)
jmp ciclo
fine:
popl %esi
popl %edx
popl %ecx
popl %ebx
popl %eax
movl 8(%ebp), %eax
leave
ret
.global __ZN2cl5elab1E2st
__ZN2cl5elab1E2st: #TODO
I try to compile and link everything with the command-line statement that has been provided to us:
g++ -o es1 -fno-elide-constructors es1.s prova1.cpp
but it only gives me a bunch of undefined reference
s:
/tmp/ccbwS0uN.o: In function `main':
prova1.cpp:(.text+0xee): undefined reference to `cl::cl(int*, double*)'
prova1.cpp:(.text+0x192): undefined reference to `cl::elab1(st)'
collect2: ld returned 1 exit status
Do you have any idea how can I solve this issue? I thought that probably I might have translated the function names in the wrong way, but I've checked them several times.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
c++filt
应用于您的名称修改并与错误消息中的签名进行比较。当删除一个下划线并使用 c++filt 进行过滤时,我得到了你的错误名称
cl::cl(int*, long double*)
,它与你的错误消息/类声明中的任何内容都不匹配。正确的重整名称应为
cl::cl(int*, double*)
的_ZN2clC1EPiPd
。我建议您改进获取损坏名称的方法(无论它是什么)。
Apply
c++filt
to your name mangling and compare to the signature in the error message.When removing one underscore and filtering with c++filt, I get for your mangled name
cl::cl(int*, long double*)
which does not match any in your error message/class declaration.The correctly mangled name should be
_ZN2clC1EPiPd
forcl::cl(int*, double*)
.I suggest that you improve the way (wahtever it is) to get the mangled name.