我试图从C ++调用汇编函数,链接器说我试图调用的函数不存在。这是错误:
avr-gcc -mmcu=atmega328 -Wall -o main.elf hello.S main.cpp
/tmp/ccS7uaAX.o: In function `main':
main.cpp:(.text+0x14): undefined reference to `hello(char, char, char, char, char)'
collect2: error: ld returned 1 exit status
这是我的来源:
//Somewhere in hello.S
.global hello
hello:
ret
//In main.cpp
extern void hello(char, char, char, char, char);
int main(){
hello(1, 2, 3, 4, 5);
}
如果这有帮助,这里是我将文件编译成目标文件后的反汇编:
这是main.o:
00000000 <main>:
0: 0f 93 push r16
2: cf 93 push r28
4: df 93 push r29
6: cd b7 in r28, 0x3d ; 61
8: de b7 in r29, 0x3e ; 62
a: 05 e0 ldi r16, 0x05 ; 5
c: 24 e0 ldi r18, 0x04 ; 4
e: 43 e0 ldi r20, 0x03 ; 3
10: 62 e0 ldi r22, 0x02 ; 2
12: 81 e0 ldi r24, 0x01 ; 1
14: 0e 94 00 00 call 0 ; 0x0 <main>
18: 80 e0 ldi r24, 0x00 ; 0
1a: 90 e0 ldi r25, 0x00 ; 0
1c: df 91 pop r29
1e: cf 91 pop r28
20: 0f 91 pop r16
22: 08 95 ret
这是hello.o:
00000000 <hello>:
0: 08 95 ret
我现在已经搜索了几个小时,我真的不知道最近会发生什么。
每个文件单独编译,它只是我认为搞砸的链接。
我也可以毫无问题地编译纯汇编。
任何帮助都会受到赞赏,我刚刚开始研究将AVR与Assembly和C ++混合使用。
答案 0 :(得分:3)
试试这个:在main.cpp中,
extern "C" void hello(char, char, char, char, char);
其中包含"C"
。 C ++更改函数的名称以包含其类型,称为名称修改。 “C”告诉编译器不要乱码。汇编程序不会破坏,因此编译器需要知道不要。
顺便说一句,修改是链接器如何能够告诉你它正在寻找的hello
的参数类型。
编辑在您当前的目标文件上执行strings main.o |grep hello
,您会看到对其后面带有各种有趣字符的hello的引用。那是错误的名字。