GCC _attribute__ weak weakref

本文详细介绍了GCC编译器中弱符号与弱引用的概念及使用方法。解释了如何通过__attribute__((weakref(target)))标记函数声明为弱引用,并讨论了其在不同情况下的行为表现。

#include <stdio.h>

/*void test()
{
	printf("the test for weak refrence!\n");	
}*/

static __attribute__ ((weakref("test"))) void foo();

int main()
{
	if(foo) foo();	
}

如果test函数有定义,执行main程序时将执行test函数;

如果test函数没有定义,执行main函数时foo地址为0,不执行foo函数。??

In GNU C, you declare certain things about functions called in your program which help the compiler optimize function calls and check your code more carefully.

The keyword __attribute__ allows you to specify special attributes when making a declaration. This keyword is followed by an attribute specification inside double parentheses. The following attributes are currently defined for functions on all targets: alignedalloc_sizenoreturnreturns_twicenoinline,noclonealways_inlineflattenpureconstnothrowsentinelformatformat_argno_instrument_function,no_split_stacksectionconstructordestructorusedunuseddeprecatedweakmallocaliasifunc,warn_unused_resultnonnullgnu_inlineexternally_visiblehotcoldartificialerror and warning. Several other attributes are defined for functions on particular target systems. Other attributes, including sectionare supported for variables declarations (see Variable Attributes) and for types (see Type Attributes).

GCC plugins may provide their own attributes.

You may also specify attributes with `__' preceding and following each keyword. This allows you to use them in header files without being concerned about a possible macro of the same name. For example, you may use __noreturn__ instead of noreturn.

alias ("target ") The  alias attribute causes the declaration to be emitted as an alias for another symbol, which must be specified. For instance,
void __f () { /* Do something. */; }
          void f () __attribute__ ((weak, alias ("__f")));

defines `f' to be a weak alias for `__f'. In C++, the mangled name for the target must be used. It is an error if `__f' is not defined in the same translation unit.

Not all target machines support this 

......

.....

weak The  weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions which can be overridden in user code, though it can also be used with non-function declarations. Weak symbols are supported for ELF targets, and also for a.out targets when using the GNU assembler and linker. 
weakref weakref ("target ") The  weakref attribute marks a declaration as a weak reference. Without arguments, it should be accompanied by an  alias attribute naming the target symbol. Optionally, the target may be given as an argument to  weakref itself. In either case,  weakref implicitly marks the declaration as  weak. Without a target, given as an argument to  weakref or to  aliasweakref is equivalent to  weak.
static int x() __attribute__ ((weakref ("y")));
          /* is equivalent to... */
          static int x() __attribute__ ((weak, weakref, alias ("y")));
          /* and to... */
          static int x() __attribute__ ((weakref));
          static int x() __attribute__ ((alias ("y")));

A weak reference is an alias that does not by itself require a definition to be given for the target symbol. If the target symbol is only referenced through weak references, then it becomes aweak undefined symbol. If it is directly referenced, however, then such strong references prevail, and a definition will be required for the symbol, not necessarily in the same translation unit.

The effect is equivalent to moving all references to the alias to a separate translation unit, renaming the alias to the aliased symbol, declaring it as weak, compiling the two separate translation units and performing a reloadable link on them.


转载于:https://my.oschina.net/u/218425/blog/78304

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值