问题描述
我有一个程序,可以使用2种不同的编译器进行编译:
I have a program which I compile with 2 different compilers:
用于PowerPC的GCC 3.4.4交叉编译器
GCC 4.8.1 MinGW编译器
在程序中,我使用的是汇编程序指令.weak
.该文档说:
In the program I am using the assembler directive .weak
. The documentation says:
Makes a symbol with weak binding globally visible to the linker.
所以我要这样:
__asm__(".weak " "foo" "\n.set " "foo" "," "dummy_foo" "\n");
清除foo
弱点并将其赋予dummy_foo
别名.
To cleare foo
weak and give it an alias to dummy_foo
.
当我交叉编译PowerPC时,此代码在GCC 3.4.4下工作正常,但在为x86目标进行编译时,它在GCC 4.8.1下不起作用. -代码已编译,但未将foo
声明为weak
,并且我的链接器为我提供了未定义的引用.这是什么问题?
This code works fine under GCC 3.4.4 when I cross compile für PowerPC but it doesn't work with GCC 4.8.1 when I compile for x86 target. - The Code compiles, but foo
is not declared weak
and my linker gives me an undefined reference. What is the problem here?
//
如BSH所建议,它必须是:
As BSH suggested, it has to be:
__asm__(".weak " "_foo" "\n.set " "_foo" "," "_dummy_foo" "\n");
如果我将此行放入与foo()
声明相同的C文件中,则可以正常工作.当我将其放在单独的C文件中时,问题仍然存在(然后适用于GCC 3.4.4交叉编译器,但不适用于GCC 4.8.1)
If I put this line into the same C-File as my declaration of foo()
it works fine. The problem still persists when I put it in a seperate C-File (then it works for the GCC 3.4.4 Cross Compiler, but not for the GCC 4.8.1 )
推荐答案
在MinGW中,符号前面带有下划线_foo
,您需要将其更改为:
In MinGW, symbols are prefixed with an underscore, _foo
, you need to change it to:
__asm__(".weak " "_foo" "\n.set " "_foo" "," "_dummy_foo" "\n");
或考虑将属性 __attribute__((weak, alias("dummy_foo")))
与foo
一起使用代替.
or consider using the attribute __attribute__((weak, alias("dummy_foo")))
with foo
instead.
这篇关于汇编器.weak指令仅适用于Cross Compile GCC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!