问题
I have a program which I compile with 2 different compilers:
GCC 3.4.4 Cross Compiler for PowerPC
GCC 4.8.1 MinGW Compiler
In the program I am using the assembler directive .weak
. The documentation says:
Makes a symbol with weak binding globally visible to the linker.
So I am going like this:
__asm__(".weak " "foo" "\n.set " "foo" "," "dummy_foo" "\n");
To cleare foo
weak and give it an alias to dummy_foo
.
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?
//edit:
As BSH suggested, it has to be:
__asm__(".weak " "_foo" "\n.set " "_foo" "," "_dummy_foo" "\n");
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 )
回答1:
In MinGW, symbols are prefixed with an underscore, _foo
, you need to change it to:
__asm__(".weak " "_foo" "\n.set " "_foo" "," "_dummy_foo" "\n");
or consider using the attribute __attribute__((weak, alias("dummy_foo")))
with foo
instead.
来源:https://stackoverflow.com/questions/19765335/assembler-weak-directive-does-only-work-with-cross-compile-gcc