How do I use the alias attribute?

With the attribute alias, one variable can be accessed with different names. These names do not occupy additional memory resources. Typical use cases are:

  • To ensure compatibility of variables for different software versions

  • Compliance with naming conventions of supplier software.

Example

	int add1 = 1;

	extern int addx __attribute__((alias("add1"))); /* declaration */

	int foo(int bar)
	{
		bar = add1;
		bar = addx;
		return bar;
	}

The definition int add1 = 1 will reserve memory for the variable. The following declaration with the alias attribute will create a symbol without allocating new memory. If you use the option -Wl,-Map=<name>.map and then inspect the generated map file, you will see that both variables have the same address. The variables are identical and can be accessed with both names.