Gcc attribute weak & amp; alias Application
1 gcc attribute weak & alias Application
Alias ("Target")
The alias attribute causesthe declaration to be emitted as an alias for another symbol, which must bespecified. For instance,
Void _ f () {/* Do something .*/;}
Void f () _ attribute _ (weak, alias ("_ f ")));
Defines 'F' to be a weakalias for '_ F '. in C ++, the mangled name for the target must be used. it is an error if '_ f' is not definedin the same translation unit.
Not all targetmachines support this attribute.
This section describes the alias and weak attributes of GCC.
1.1 gcc weak attribute
The specific syntax is probably the same as the general gcc attribute. The format of _ attribtes _ (weak) is required. Now let's use an example to illustrate.
1. We declare a common_print function in the weak_symbol.c file and declare this function as _ attribute _ (weak). We also provide the definition of this function.
// Weak_symbol.c # include
Voidcommon_print (const char * s) _ attribute _ (weak); // The voidcommon_print (const char * s) {printf ("common_print: % s \ n ", s );}
2. The common_print function is also defined in the strong_symbol.c file. Note that the function has the same declared and defined prototype in two different c files.
// Strong_symbol.c # include
Voidcommon_print (const char * s) // no weak declaration {printf ("application common_print: % s \ n", s);} int main (intargc, char * argv []) {common_print ("I want to test gcc weakattribute"); return 0 ;}
3. Compile and connect to a c file, and the execution result is as follows:
Root @ VM-Ubuntu203001:/home/test/gcc_test #./test_weak
Applicationcommon_print: I want to test gcc weak attribute
Analysis:
1) First, the two functions of the same prototype are defined in a different c file. There is no error in compiling and connecting these two c files. The reason is that there is a strong symbol and weak symbol concept in gcc. The default function definition is strong symbol. When two identical strong symbrs are connected together, the "repeated definition of symbol" error will certainly occur.
However, here we add the weak attribute to common_print in weak_symbol.c, so that when gcc is re-selected, strong symbol is preferred.
2) based on the execution results of the example program, the common_print function in strong_symbol.c is executed. If the implementation of the common_print function is not provided in strong_symbol.c, weak_symbol.c is called.
3) as we can imagine, when using c voice to provide an api library, we can declare these Apis as weak attributes and provide default implementations. When users want to customize their own, it is easy to implement. Many APIs in glibc are designed in this way.
1.2 application scenarios of gcc weak & alias attribute
Let's look at an example first.
1. We provide an implementation of the _ lib_print function in weak_symbol.c, and declare the print_a and print_ B functions at the same time, But what doesn't work with the above is, these two functions add _ attribute _ (weak, alias ("_ lib_print") familiarity, that is, both print_a and print_ B are aliases of _ lib_print (alias ).
//weak_symbol.c#include
void __lib_print(const char *s){ printf("__lib_print : %s\n", s);} void print_a(const char *s) __attribute__((weak, alias("__lib_print")));void print_b(const char *s) __attribute__((weak, alias("__lib_print")));
2. We have re-implemented print_ B in strong_symbol.c, and print_a remains unchanged by default.
//strong_symbol.c#include
void print_b(const char *s){ printf("application print_b : %s\n", s);}int main(int argc, char *argv[]){ print_a("I want to test weak&&alias"); print_b("I want to test weak&&alias"); return 0;}
3. Compile and connect to a c file, and the execution result is as follows:
Root @ VM-Ubuntu203001:/hometest/gcc_test #./test_weak
_ Lib_print: I want to testweak & alias
Application print_ B: I want to testweak & alias
Analysis:
From the execution result, print_a executes the implementation of _ lib_print in weak_symbol.c. Print_ B executes the print_ B implementation in strong_symbol.c. This execution result should be guessed. It must be noted that the alias attribute can provide many different aliases for a function. In this way, when we provide an api library, if multiple APIs have the same default implementation, we can provide a default implementation through the alias mechanism.