GCC provides a rich set of function, variable, and type attributes that allow developers to specify various properties and behaviors beyond what's available in standard C. Here's a list of some of the commonly used GCC compiler attributes, along with a brief explanation for each:
- __attribute__((constructor)):This attribute causes the function to be called automatically before the main() function, similar to constructors in C++ for global objects.
- __attribute__((destructor)):Opposite of constructor. It causes the function to be called automatically after main() exits or after exit() is called.
- __attribute__((used)):This attribute ensures that the function or variable is not eliminated by the compiler even if it appears to be unused. It is useful for things like interrupt handlers in embedded systems.
- __attribute__((unused)):Indicates that the function/variable/parameter might be unused and should not cause a compiler warning if it isn't used.
- __attribute__((deprecated)):If a function or variable is marked with this attribute, a warning will be produced if the program uses that function or variable.
- __attribute__((weak)):Declares the function or variable as weak, meaning it can be overridden by functions or variables with the same name in other files.
- __attribute__((packed)):Used on structures, it directs the compiler to pack the structure members together as closely as possible, usually to ensure the smallest size. It can have implications for alignment and performance.
- __attribute__((aligned(X))):Specifies a particular alignment (in bytes) for a variable or structure field. X is a power of two.
- __attribute__((noreturn)):Indicates that the function does not return. An example is the exit() function.
- __attribute__((pure)):Indicates that a function has no effects except for its return value and the return value is solely based on its inputs. It doesn't use global variables or have other side effects.
- __attribute__((const)):Similar to pure, but indicates that the function does not read global memory and only relies on its arguments.
- __attribute__((always_inline)):Suggests that the compiler should always inline the function, regardless of the optimization level.
- __attribute__((no_instrument_function)):When using GCC's instrumentation features (e.g., for profiling), this attribute ensures that no instrumentation code is added to the specified function.
- __attribute__((visibility("default|hidden|protected|internal"))):Sets the visibility of functions, which can be useful for creating shared libraries with controlled public interfaces.
- __attribute__((naked)):As previously mentioned, this attribute tells the compiler not to generate standard function prologue and epilogue.
This is not an exhaustive list, and the specific behavior of some attributes can be target-dependent. Developers should consult the GCC documentation for a complete list and detailed descriptions of each attribute, as well as to ensure the attribute is supported for their target architecture.