GCC链接/符号名称与C ++和汇编程序文件 [英] GCC linking / symbol name mangling with C++ and assembler files
问题描述
我在编译和链接用汇编程序编写的库和我在C ++中的程序代码时遇到了一些麻烦。在我的情况下,它是为使用avr-gcc套件的AVR微控制器,但我想这个问题将适用于GCC一般。
I'm having some trouble compiling and linking a library that's written in assembler with my program code which is in C++. In my case it's for an AVR microcontroller using the avr-gcc suite, but I imagine this problem would apply to GCC generally.
我可以从C ++源码和程序集中得到.o文件,但链接它们会产生错误:
I can get .o files built from the C++ source and assembly, but linking them gives the error:
> avr-gcc -mmcu=attiny84 -lm -o obj\AvrTest.elf obj\Main.o obj\i2cmaster.o
obj\Main.o: In function `main':
Main.cpp:(.text+0x0): undefined reference to `i2c_init()'
uber-simple Main.cpp
代码:
Here's the uber-simple Main.cpp
code:
#include "i2cmaster.h"
void main() {
i2c_init();
while ( true ) {
}
}
i2cmaster.h
定义了这样的函数原型:
i2cmaster.h
defines the function prototype like this:
extern void i2c_init(void);
在汇编器文件中, i2cmaster.S
,该函数是:
And in the assembler file, i2cmaster.S
, that function is:
.global i2c_init
.func i2c_init
i2c_init:
cbi SDA_DDR,SDA ;release SDA
cbi SCL_DDR,SCL ;release SCL
cbi SDA_OUT,SDA
cbi SCL_OUT,SCL
ret
.endfunc
链接看起来像一个没有脑子的 - 但我得到的参考错误。我注意到为 Main.cpp
生成的汇编代码如下:
The linking would seem like a no-brainer - but I get that reference error. I noticed the assembler code generated for Main.cpp
comes out like this:
.global main
.type main, @function
main:
rcall _Z8i2c_initv
.L2:
rjmp .L2
.size main, .-main
我怀疑问题是 i2c_init
正在通过一些名称管理过程变得 _Z8i2c_initv
我不明白, _Z8i2c_initv
无法再匹配在 i2cmaster.o
对象文件中的未损坏的 i2c_init
符号。
I suspect the problem is that i2c_init
is becoming _Z8i2c_initv
through some name mangling process I don't understand, and that _Z8i2c_initv
can no longer be matched with the non-mangled i2c_init
symbol in the i2cmaster.o
object file.
有没有人理解这个过程,以及如何在编译/汇编过程中抑制它,使这些模块可以相互通信?还是别的什么我可能做错了?
Does anyone understand the mangling process, and how to suppress it during compilation/assembly so that these modules can talk to each other? Or anything else I might be doing wrong?
推荐答案
确定,解决了。显然,符号调整是一个C ++的东西,并且i2c库没有设置为正确使用C ++。可以使用以下方法修复它:
OK, solved it. Apparently the symbol mangling is a C++ thing, and the i2c library isn't set up to work with C++ correctly. Was able to fix it by using the following:
extern "C" {
#include "i2cmaster.h"
};
void main() {
i2c_init();
while ( true ) {
}
}
这篇关于GCC链接/符号名称与C ++和汇编程序文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!