Tutorial of gcc __attribute__ ((alias))
GCC Attribute alias allows you to specify multiple aliases(other names) for a symbol (function/variable).
Example of aliasing a function name:
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
static int myfunc(int a, int b) | |
{ | |
printf("%s: Adding %d with %d:\t Result:%d\n", | |
__func__, a, b, a+b); | |
return a+b; | |
} | |
static int add(int a, int b) __attribute__((alias("myfunc"))); | |
int main() | |
{ | |
add(3, 6); | |
myfunc(3, 5); | |
return 0; | |
} |
Output:
myfunc: Adding 3 with 6: Result:9
myfunc: Adding 3 with 5: Result:8
myfunc: Adding 3 with 5: Result:8
Notes:
You can see, calling add, the compiler will only call myfunc. Also point to be noted is __func__ returns myfunc on call to add.
Example of aliasing a variable:
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int oldname = 5; | |
extern int newname __attribute__((alias("oldname"))); | |
int main() | |
{ | |
printf("Value of new name is :%d\n", newname); | |
return 0; | |
} |
Output:
Value of new name is :5
Comments
Post a Comment