Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

GCC/Clang Attributes Quick Reference

This document is a cheat sheet for commonly used attributes in GCC and Clang compilers.
Attributes are compiler hints that control function behavior, optimization, safety checks, memory layout, and linkage.
They don’t change C syntax but provide extra metadata to the compiler.


🧩 Function Attributes

Initialization & Finalization

__attribute__((constructor))        // Run before main()
__attribute__((destructor))         // Run after main()
__attribute__((constructor(200)))   // Priority: lower number runs earlier

Optimization

__attribute__((always_inline))  // Force inline
__attribute__((noinline))       // Prevent inline
__attribute__((hot))            // Function is frequently used, optimize aggressively
__attribute__((cold))           // Rarely called, deprioritize optimization
__attribute__((flatten))        // Inline all calls inside this function

Execution Contracts

__attribute__((noreturn))   // Function never returns (e.g., exit)
__attribute__((pure))       // Depends only on arguments + globals, no side effects
__attribute__((const))      // Pure function: depends only on arguments
__attribute__((malloc))     // Returns a unique pointer (like malloc)

Safety

__attribute__((format(printf,1,2))) // Validate printf-like args
__attribute__((format(scanf,1,2)))  // Validate scanf-like args
__attribute__((nonnull(1,2)))       // Args #1 and #2 cannot be NULL
__attribute__((warn_unused_result)) // Warn if return value is ignored

📦 Variable & Struct Attributes Alignment

int x __attribute__((aligned(16)));   // 16-byte alignment
struct __attribute__((packed)) S {    // No padding (tight packing)
   char a;
   int b;
};

Section Placement

int x __attribute__((section(".mydata")));  // Place into custom ELF section

Usage

int y __attribute__((unused));  // Suppress "unused variable" warning
int z __attribute__((used));    // Force variable into binary even if unused

🔌 Linking & Visibility

__attribute__((visibility("default"))) // Export symbol (default)
__attribute__((visibility("hidden")))  // Hide symbol, not exported
__attribute__((alias("other_func")))   // Create function alias
__attribute__((weak))                  // Weak symbol, can be overridden

⚡ Special Attributes

Branch Prediction Hints

#define likely(x)   __builtin_expect(!!(x), 1) // Expression likely true
#define unlikely(x) __builtin_expect(!!(x), 0) // Expression likely false

Embedded / Low-level

__attribute__((interrupt))   // Mark as interrupt handler
__attribute__((naked))       // No prologue/epilogue (raw assembly body)

🎯 Example

#include <stdio.h>
#include <stdlib.h>

__attribute__((constructor))
static void init() {
    printf("init before main\n");
}

__attribute__((destructor))
static void fini() {
    printf("fini after main\n");
}

__attribute__((noreturn))
void fatal(const char *msg) {
    printf("FATAL: %s\n", msg);
    exit(1);
}

__attribute__((pure))
int add(int a, int b) { return a+b; }

__attribute__((deprecated("use add instead")))
int old_add(int a, int b) { return a+b; }

int main() {
    printf("sum=%d\n", add(2,2));
    // old_add(1,2); // warning: deprecated
    fatal("error!"); // noreturn
}

More here

https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html?utm_source=chatgpt.com https://clang.llvm.org/docs/AttributeReference.html?utm_source=chatgpt.com

About

This document is a cheat sheet for commonly used attributes in GCC and Clang compilers. Attributes are compiler hints that control function behavior, optimization, safety checks, memory layout, and linkage.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors