#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函數;html
若是test函數沒有定義,執行main函數時foo地址爲0,不執行foo函數。??ide
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: aligned
, alloc_size
, noreturn
, returns_twice
, noinline
,noclone
, always_inline
, flatten
, pure
, const
, nothrow
, sentinel
, format
, format_arg
, no_instrument_function
,no_split_stack
, section
, constructor
, destructor
, used
, unused
, deprecated
, weak
, malloc
, alias
, ifunc
,warn_unused_result
, nonnull
, gnu_inline
, externally_visible
, hot
, cold
, artificial
, error
and warning
. Several other attributes are defined for functions on particular target systems. Other attributes, including section
are supported for variables declarations (see Variable Attributes) and for types (see Type Attributes).ui
GCC plugins may provide their own attributes.this
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
.spa
alias ("
target
")
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.code
Not all target machines support this orm
......htm
.....ci
weak
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
")
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
alias
,
weakref
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.