一、attribute 属性表
跳转到“一、attribute 属性表”| 属性 | 语法 | 用途 | 示例 |
|---|---|---|---|
| section | __attribute__((section("name"))) | 指定存储段 | __attribute__((section(".ccmram"))) int var; |
| aligned | __attribute__((aligned(n))) | 对齐到n字节 | __attribute__((aligned(32))) uint8_t buf[1024]; |
| packed | __attribute__((packed)) | 取消对齐,紧凑存储 | struct __attribute__((packed)) { uint8_t a; uint32_t b; }; |
| weak | __attribute__((weak)) | 弱符号,可被覆盖 | __attribute__((weak)) void handler(void); |
| alias | __attribute__((alias("name"))) | 创建别名 | void f(void) __attribute__((alias("g"))); |
| used | __attribute__((used)) | 防止被优化掉 | __attribute__((used)) static int keep_me; |
| unused | __attribute__((unused)) | 消除未使用警告 | __attribute__((unused)) int param; |
| noinit | __attribute__((noinit)) | 不初始化 | __attribute__((noinit)) uint8_t big_buf[65536]; |
| constructor | __attribute__((constructor)) | main前执行 | __attribute__((constructor)) void init(void); |
| destructor | __attribute__((destructor)) | main后执行 | __attribute__((destructor)) void cleanup(void); |
| interrupt | __attribute__((interrupt)) | 中断处理函数 | __attribute__((interrupt)) void ISR(void); |
| naked | __attribute__((naked)) | 无函数序言/尾声 | __attribute__((naked)) void asm_func(void); |
| noreturn | __attribute__((noreturn)) | 函数不返回 | __attribute__((noreturn)) void reset(void); |
| optimize | __attribute__((optimize("O3"))) | 指定优化级别 | __attribute__((optimize("O0"))) void debug(void); |
| always_inline | __attribute__((always_inline)) | 强制内联 | __attribute__((always_inline)) static inline void fast(void); |
| noinline | __attribute__((noinline)) | 禁止内联 | __attribute__((noinline)) void no_inline(void); |
| pure | __attribute__((pure)) | 纯函数,无副作用 | __attribute__((pure)) int calc(int x); |
| const | __attribute__((const)) | 只依赖参数的函数 | __attribute__((const)) int square(int x); |
| hot | __attribute__((hot)) | 热点代码 | __attribute__((hot)) void frequent(void); |
| cold | __attribute__((cold)) | 冷代码 | __attribute__((cold)) void rare(void); |
| deprecated | __attribute__((deprecated)) | 标记为过时 | __attribute__((deprecated)) void old_api(void); |
| format | __attribute__((format(printf,1,2))) | 格式检查 | __attribute__((format(printf,1,2))) void log(char *fmt, ...); |
二、内置函数 - 位操作
跳转到“二、内置函数 - 位操作”| 函数 | 返回值 | 功能 | 示例 |
|---|---|---|---|
__builtin_clz(x) | int | 前导零个数(32位) | int lz = __builtin_clz(0x00F00000); // 8 |
__builtin_clzll(x) | int | 前导零个数(64位) | int lz = __builtin_clzll(0x00F0000000000000LL); |
__builtin_ctz(x) | int | 尾随零个数(32位) | int tz = __builtin_ctz(0x00000F00); // 8 |
__builtin_ctzll(x) | int | 尾随零个数(64位) | int tz = __builtin_ctzll(0x00000F0000000000LL); |
__builtin_popcount(x) | int | 1的个数(32位) | int cnt = __builtin_popcount(0xFF); // 8 |
__builtin_popcountll(x) | int | 1的个数(64位) | int cnt = __builtin_popcountll(0xFFLL); |
__builtin_parity(x) | int | 奇偶性(32位) | int p = __builtin_parity(0x11); // 0(偶) |
__builtin_parityll(x) | int | 奇偶性(64位) | int p = __builtin_parityll(0x11LL); |
__builtin_ffs(x) | int | 第一个1的位置 | int pos = __builtin_ffs(0x08); // 4 |
__builtin_ffsll(x) | int | 第一个1的位置(64位) | int pos = __builtin_ffsll(0x08LL); |
__builtin_bswap16(x) | uint16_t | 字节交换(16位) | uint16_t sw = __builtin_bswap16(0x1234); |
__builtin_bswap32(x) | uint32_t | 字节交换(32位) | uint32_t sw = __builtin_bswap32(0x12345678); |
__builtin_bswap64(x) | uint64_t | 字节交换(64位) | uint64_t sw = __builtin_bswap64(0x123456789ABCDEF0); |
三、内置函数 - 原子操作
跳转到“三、内置函数 - 原子操作”| 函数 | 功能 | 返回值 | 示例 |
|---|---|---|---|
__builtin_sync_fetch_and_add(ptr, val) | 原子加法 | 旧值 | int old = __builtin_sync_fetch_and_add(&cnt, 1); |
__builtin_sync_fetch_and_sub(ptr, val) | 原子减法 | 旧值 | int old = __builtin_sync_fetch_and_sub(&cnt, 1); |
__builtin_sync_fetch_and_or(ptr, val) | 原子OR | 旧值 | int old = __builtin_sync_fetch_and_or(&flags, 0x01); |
__builtin_sync_fetch_and_and(ptr, val) | 原子AND | 旧值 | int old = __builtin_sync_fetch_and_and(&flags, ~0x01); |
__builtin_sync_fetch_and_xor(ptr, val) | 原子XOR | 旧值 | int old = __builtin_sync_fetch_and_xor(&flags, 0x01); |
__builtin_sync_add_and_fetch(ptr, val) | 原子加法 | 新值 | int new = __builtin_sync_add_and_fetch(&cnt, 1); |
__builtin_sync_bool_compare_and_swap(ptr, old, new) | CAS操作 | bool | if(__builtin_sync_bool_compare_and_swap(&var, 0, 1)) |
__builtin_sync_val_compare_and_swap(ptr, old, new) | CAS操作 | 旧值 | int old = __builtin_sync_val_compare_and_swap(&var, 0, 1); |
__builtin_sync_synchronize() | 内存屏障 | void | __builtin_sync_synchronize(); |
__builtin_sync_lock_test_and_set(ptr, val) | 原子设置 | 旧值 | int old = __builtin_sync_lock_test_and_set(&lock, 1); |
__builtin_sync_lock_release(ptr) | 释放锁 | void | __builtin_sync_lock_release(&lock); |
四、内置函数 - 其他常用
跳转到“四、内置函数 - 其他常用”| 函数 | 功能 | 返回值 | 示例 |
|---|---|---|---|
__builtin_expect(exp, c) | 分支预测 | exp的值 | if(__builtin_expect(err, 0)) |
__builtin_constant_p(exp) | 判断编译时常量 | int | if(__builtin_constant_p(x)) |
__builtin_object_size(ptr, type) | 对象大小 | size_t | size_t sz = __builtin_object_size(p, 0); |
__builtin_return_address(level) | 返回地址 | void* | void *ret = __builtin_return_address(0); |
__builtin_frame_address(level) | 栈帧地址 | void* | void *frame = __builtin_frame_address(0); |
__builtin_alloca(size) | 栈上分配 | void* | char *buf = __builtin_alloca(256); |
__builtin_types_compatible_p(t1,t2) | 类型兼容性 | int | if(__builtin_types_compatible_p(typeof(a), int)) |
__builtin_choose_expr(const, e1, e2) | 编译时选择 | - | __builtin_choose_expr(1, x, y) |
__builtin_offsetof(type, member) | 成员偏移 | size_t | size_t off = __builtin_offsetof(struct s, field); |
__builtin_prefetch(addr, rw, locality) | 预取数据 | void | __builtin_prefetch(&data[i+1], 0, 1); |
五、内置函数 - 溢出检查
跳转到“五、内置函数 - 溢出检查”| 函数 | 功能 | 返回值 | 示例 |
|---|---|---|---|
__builtin_add_overflow(a,b,res) | 加法溢出检查 | bool | if(__builtin_add_overflow(a, b, &result)) |
__builtin_sub_overflow(a,b,res) | 减法溢出检查 | bool | if(__builtin_sub_overflow(a, b, &result)) |
__builtin_mul_overflow(a,b,res) | 乘法溢出检查 | bool | if(__builtin_mul_overflow(a, b, &result)) |
__builtin_uadd_overflow(a,b,res) | 无符号加法溢出 | bool | if(__builtin_uadd_overflow(a, b, &result)) |
__builtin_usub_overflow(a,b,res) | 无符号减法溢出 | bool | if(__builtin_usub_overflow(a, b, &result)) |
__builtin_umul_overflow(a,b,res) | 无符号乘法溢出 | bool | if(__builtin_umul_overflow(a, b, &result)) |
__builtin_sadd_overflow(a,b,res) | 有符号加法溢出 | bool | if(__builtin_sadd_overflow(a, b, &result)) |
__builtin_ssub_overflow(a,b,res) | 有符号减法溢出 | bool | if(__builtin_ssub_overflow(a, b, &result)) |
__builtin_smul_overflow(a,b,res) | 有符号乘法溢出 | bool | if(__builtin_smul_overflow(a, b, &result)) |
六、语言扩展
跳转到“六、语言扩展”| 扩展 | 语法 | 用途 | 示例 |
|---|---|---|---|
| typeof | typeof(expr) | 获取表达式类型 | typeof(x) y = x; |
| 语句表达式 | ({ statements; }) | 复合语句作为表达式 | int max = ({int a=1; int b=2; a>b?a:b;}); |
| 零长度数组 | type array[0] | 结构体末尾可变数组 | struct { int len; char data[0]; }; |
| case范围 | case low ... high: | switch范围匹配 | case 'A' ... 'Z': |
| 指定初始化 | [index] = value | 数组指定位置初始化 | int a[10] = {[5]=1, [8]=2}; |
| 复合字面量 | (type){init} | 创建匿名对象 | func((struct point){.x=1, .y=2}); |
| 标签值 | &&label | 获取标签地址 | void *ptr = &&label; |
| __restrict | type * __restrict ptr | 指针无别名 | void copy(int * __restrict dst, int * __restrict src); |
| __thread | __thread type var | 线程局部存储 | __thread int tls_var; |
| __int128 | __int128 | 128位整数 | __int128 big_num; |
七、预定义宏
跳转到“七、预定义宏”| 宏 | 值/含义 | 用途 | 示例 |
|---|---|---|---|
__GNUC__ | 主版本号 | GCC版本检查 | #if __GNUC__ >= 7 |
__GNUC_MINOR__ | 次版本号 | GCC版本检查 | #if __GNUC_MINOR__ >= 3 |
__GNUC_PATCHLEVEL__ | 补丁级别 | GCC版本检查 | __GNUC_PATCHLEVEL__ |
__VERSION__ | 版本字符串 | 编译器版本 | printf("%s", __VERSION__); |
__OPTIMIZE__ | 定义时表示开启优化 | 优化检查 | #ifdef __OPTIMIZE__ |
__OPTIMIZE_SIZE__ | 定义时表示-Os | 大小优化检查 | #ifdef __OPTIMIZE_SIZE__ |
__NO_INLINE__ | 定义时禁用内联 | 内联检查 | #ifndef __NO_INLINE__ |
__BYTE_ORDER__ | 字节序 | 大小端检查 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
__SIZEOF_POINTER__ | 指针大小 | 架构检查 | #if __SIZEOF_POINTER__ == 8 |
__ARM_ARCH | ARM架构版本 | ARM检查 | #if __ARM_ARCH >= 7 |
__thumb__ | Thumb模式 | 指令集检查 | #ifdef __thumb__ |
__FILE__ | 当前文件名 | 调试信息 | printf("File: %s", __FILE__); |
__LINE__ | 当前行号 | 调试信息 | printf("Line: %d", __LINE__); |
__func__ | 当前函数名 | 调试信息 | printf("Func: %s", __func__); |
__FUNCTION__ | 当前函数名(GCC) | 调试信息 | printf("Func: %s", __FUNCTION__); |
__PRETTY_FUNCTION__ | 完整函数签名 | 调试信息 | printf("%s", __PRETTY_FUNCTION__); |
__DATE__ | 编译日期 | 版本信息 | printf("Built: %s", __DATE__); |
__TIME__ | 编译时间 | 版本信息 | printf("At: %s", __TIME__); |
__TIMESTAMP__ | 时间戳 | 版本信息 | printf("%s", __TIMESTAMP__); |
八、#pragma指令
跳转到“八、#pragma指令”| 指令 | 语法 | 功能 | 示例 |
|---|---|---|---|
| diagnostic | #pragma GCC diagnostic [action] "[option]" | 诊断控制 | #pragma GCC diagnostic ignored "-Wunused" |
| optimize | #pragma GCC optimize ("[option]") | 优化控制 | #pragma GCC optimize ("O3") |
| target | #pragma GCC target ("[option]") | 目标选项 | #pragma GCC target ("thumb") |
| push_options | #pragma GCC push_options | 保存选项 | #pragma GCC push_options |
| pop_options | #pragma GCC pop_options | 恢复选项 | #pragma GCC pop_options |
| reset_options | #pragma GCC reset_options | 重置选项 | #pragma GCC reset_options |
| message | #pragma message "text" | 编译时消息 | #pragma message "TODO: fix this" |
| warning | #pragma GCC warning "text" | 编译警告 | #pragma GCC warning "deprecated" |
| error | #pragma GCC error "text" | 编译错误 | #pragma GCC error "not supported" |
| poison | #pragma GCC poison identifier | 禁用标识符 | #pragma GCC poison malloc |
| pack | #pragma pack(n) | 结构体对齐 | #pragma pack(1) |
九、内联汇编语法
跳转到“九、内联汇编语法”| 组成部分 | 格式 | 说明 | 示例 |
|---|---|---|---|
| 基本格式 | __asm__(code) | 简单汇编 | __asm__("nop"); |
| 扩展格式 | __asm__(code : output : input : clobber) | 完整汇编 | 见下方 |
| 输出操作数 | "=constraint"(var) | =表示只写 | "=r"(result) |
| 输入操作数 | "constraint"(var) | 输入变量 | "r"(value) |
| 输入输出 | "+constraint"(var) | +表示读写 | "+r"(counter) |
| 破坏列表 | "register" | 被修改的寄存器 | "r0", "r1", "memory", "cc" |
| volatile | __asm__ volatile | 防止优化 | __asm__ volatile("dmb"); |
约束字符表:
| 约束 | 含义 | 示例 |
|---|---|---|
r | 通用寄存器 | "r"(value) |
m | 内存操作数 | "m"(variable) |
i | 立即数 | "i"(100) |
n | 已知立即数 | "n"(sizeof(int)) |
g | 任意操作数 | "g"(data) |
0-9 | 匹配第n个操作数 | "0"(same_as_first) |
十、STM32/Cortex-M专用
跳转到“十、STM32/Cortex-M专用”| 功能 | 实现 | 说明 | 示例 |
|---|---|---|---|
| NOP | __asm__("nop") | 空操作 | __NOP(); |
| WFI | __asm__("wfi") | 等待中断 | __WFI(); |
| WFE | __asm__("wfe") | 等待事件 | __WFE(); |
| SEV | __asm__("sev") | 发送事件 | __SEV(); |
| BKPT | __asm__("bkpt #0") | 断点 | __BKPT(0); |
| DMB | __asm__("dmb") | 数据内存屏障 | __DMB(); |
| DSB | __asm__("dsb") | 数据同步屏障 | __DSB(); |
| ISB | __asm__("isb") | 指令同步屏障 | __ISB(); |
| CPSID | __asm__("cpsid i") | 禁用中断 | __disable_irq(); |
| CPSIE | __asm__("cpsie i") | 启用中断 | __enable_irq(); |
| MRS | __asm__("mrs %0, reg" : "=r"(val)) | 读特殊寄存器 | MRS r0, PRIMASK |
| MSR | __asm__("msr reg, %0" :: "r"(val)) | 写特殊寄存器 | MSR PRIMASK, r0 |
| REV | __asm__("rev %0, %1") | 字节反转 | __REV(value); |
| CLZ | __asm__("clz %0, %1") | 前导零计数 | __CLZ(value); |
这些表格涵盖了GCC编译器的主要扩展机制,可以作为快速参考手册使用。
来源:CSDN 原文,首次发布于 2025-08-24。