GCC attributes

GCC attributes enable the programmer to express ideas that the standardized c/c++ languages do not, such as funcion calling convention and structure packing and member alignment. While other compilers use a mix of and vendor extension specifiers the __attribute__ syntax allows for building up a set of attributes within preprocessor  directives.

Attributes may appear on function declarations, on struct class, union or enum definitions and on object definitions.

Syntax

Attributes have the following general syntax:

__attribute__(( attributename[(param [, param [, ... ] ] ) ] [, ...] ))

An empty __attribute__() is also legal.

Multiple __attribute__ specifiers may appear together instead of creating a comma seperated attribute list within a single __attribute__ specifier.

As an example:

void exit(int status) __attribute__((noreturn));

Problems

There are some known issues with attributes in c++ code due to the fact that __attribute__ does not contribute to the name mangling scheme. Also typeid is unable to distinguish between types based on __attribute__ specifiers.

There are also some problems when using __attribute__ with oil-style function definitions in C. The first parameter in such a definition can not have an __attribute__ associated with it because any such __attribute__ would attach to the function instead of the parameter. This problem does not appear when using modern function definition syntax.

Another problem associated with attributes is that not all attributes make sense in all contexts in which they are legal. It is up to the programmer to ensure that a given attribute makes sense in context.

Sample function attributes

noreturn: The noreturn attribute specifies that the function does not return. This attribute allows the compiler to perform some optimizations as well as informing the programmer of unreachable code. The noreturn attribute does not however affect exception handling.

noinline: The noinline attribute specifies that the function should not be considered as a canidate for inline expansion.

always_inline: The always_inline attribute specifies that the function should be generated inline whenever possible.

pure: The pure specifier indicates that the function has no effect other than the return value and that the return value depends onlyon the function arguments and/or global variables. This attribute allows for elimitation of some function calls when the parameters have not changed from one call to the next.

For example, given a function int square(int) that returns the value of its argument squared, the function could be declared

int square(int) __attribute__((pure));

and the compiler would be free to eliminate repeated calls where the argument has not changed.

const: The const attribute is a more strict variant of 'pure' and indicates that the function has no effects other than its return value and that it depends only on its parameters to generate that return value. Like 'pure' const allows for the elimination of function calls with repeated arguments. The const attribute may not be used on functions that take pointer arguments and examine the data pointed to. Likewise, a const function may not call a non-const function.

nothrow: The nothrow attribute specifies that the function can not throw an exception. This attribute first appeared in gcc 3.2.

format: The format attribute specifies that the function takes a printf/scanf type format argument list. The gcc info book should be consulted for details of its use.

cdecl/stdcall/fastcall: These attributes specify the calling convention to use for the named function.

Sample variable attributes

aligned(ALIGNMENT): the aligned attribute specifies a minimum alignment (in bytes) for the variable to structure field. This attribute may also appear on structure definitions, in which case it applies to all objects of that type that don't have an __attribute__((aligned(ALIGNMENT))) of their own.

packed: The packed attribute specifies that a variable or structure field should have the smallest possible alignment. This attribute is useful for creating structures that match the layout of data from an external source, such as memory mapped regions.

section(SECTION-NAME): the section attribute specifies which section the object should appear in, overriding the default chosen by the compiler. The section attribute may only appear on initialized definitions of global data. This limitation is due to the way that linkers operate. This attribute may not be available on all systems.

shared: The shared attribute specifies that the defined object should be located in a shared memory region. All instances of a given module (usually a dynamic library) see the same data in shared memory. This attribute must appear along with a section attribute and is only available on Microsoft Windows ports of gcc.

unused: The unused attribute specifies that the object may not be used in code. This attribute is useful for supressing warnings.

dllimport/dllexport: These attributes specify that the named object will be found in a dynamic link library. This attribute is specific to Microsoft Windows ports of gcc

Sample type attributes

deprecated: This attribute specifies that the named type is deprecated and its use should be avoided. This attribute is also valid for functions and variables.

Many attributes that are valid for variables may also be applied to types.

Sample C++ attributes

init_priority(PRIORITY): The init_priority attribute allows the programmer to have greater control over the order of object initializations. This attribute may only be applied to object definitions at namespace scope. PRIORITY is a constant integral expression.

java_interface: This attribute specifies that the named class is a Java inteface. It may only be applied to classes declared within an extern "Java" block. Calls to methods within this class with be dispatched via GCJ'sinterface table rather than the normal virtual table dispatch.

There are many attributes that have not been described in this page.

See also: 'info gcc' on most systems where gcc is available for detailed information on which attributes are available with the compiler version and target system you are using.