The _ attribute _ mechanism is a major feature of gnu c. It can be used to set attributes of functions, variables, and data types. Next, we will give a brief introduction to the two attributes that are useful in our work.
This attribute specifies that the function is executed before the main function is executed.
RunBeforeMain( RunBeforeMain(% main(%
The output sequence is as follows:
RunbeforeMain
Main
2. weak, alias
Weak: weak symbol. if two identical global symbols exist, a redefinition error is thrown. if weak attribute is used, when both weak symbol and non-weak symbol exist, linker uses non-weak symbol. if only weak symbol exists, only weak symbol is used.
Alias: sets the alias of a function, as shown in figure
example() __attribute__((alias()));
Set the example function as the alias of _ example.
These two attributes can be used together to compile and run successfully even if the non-weak symbol is not declared.
// Strong. c
#include <stdio.h>
// Weak. c
#include <stdio.h> StrongFun()__attribute__((weak, alias( main( argc, **
For separate links,
Gcc-o weak. o
./Weak
Output result:
Weak. c: WeakFun
At the same time,
Gcc-o strong weak. o strong. o
./Strong
Output result
Strong. c: StrongFun