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:


#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

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:


#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

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto