weakref

weakref is an attribute attached to function declarations which might optionally specify a target name. The target name might also be specified through the attribute alias in any declaration of the function.

References to the weakref function are converted into references of the target name. If the target name is not defined in the current translation unit and it is not referenced directly or otherwise in a way that requires a definition of the target, for example if it is only referenced by using weakref functions, the reference is weak. In the presence of a definition of the target in the current translation unit, references to a weakref function resolve directly to said definition. The weakref attribute does not otherwise affect definitions of the target. A weakref function must have internal linkage.

The weakref attribute, as with other GCC attributes, can be expressed in a pre-fix or post-fix syntax:
pre-fix syntax
static __attribute__((weakref("bar"))) void foo(void);
post-fix syntax
static void foo(void) __attribute__((weakref("bar")));

Functions with weakref or alias attributes may refer to other such functions. The name referred to is that of the last, i.e., non-weakref and non-alias target.

Rules

If a weakref function is declared without the keyword static, an error message is emitted when the compiler is configured with GCC version 4.3 or later.

The target name specified in the weakref function declaration cannot directly or indirectly point to itself.

Using the weakref attribute without providing a target name is not recommended.

If a body is provided in a weakref function declaration with a pre-fix syntax, the attribute is ignored. A warning message reporting this situation will be emitted.

Examples

The following examples illustrates various declarations of weakref functions:
static void foo() __attribute__((weakref("bar")));
void foo() __attribute__((weakref("bar")));
static void foo() __attribute__((weakref,alias("bar")));
static void foo() __attribute__((alias("bar"),weakref));
static void foo() __attribute__((alias("bar"))); 
static void foo() __attribute__((weakref));
static void foo() __attribute__((alias("bar"))); 
void foo() __attribute__((weakref));
static void foo() __attribute__((weakref)); 
static void foo() __attribute__((alias("bar")));
static void foo() __attribute__((weakref)); 
void foo() __attribute__((alias("bar")));