C语言宏定义

您所在的位置:网站首页 宏程序表达语句有哪些 C语言宏定义

C语言宏定义

2024-07-12 08:41| 来源: 网络整理| 查看: 265

C语言宏定义-主流编译器相关的宏

文章目录 C语言宏定义-主流编译器相关的宏1. 目的2. 编译器类型识别3. 编译器相关的通用宏4. 编译器相关的特性宏

1. 目的

嵌入式c语言日常开发中,主要面对的编译器有3种,KEIL、IAR、GCC,为了实现多编译代码的统一,抽象了编译器相关的宏,初始设计来源于cmsis源码抽象。

2. 编译器类型识别 /** * @def CMO_COMPILER_TYPE * @brief Compiler type. */ /* Compiler enum */ // clang-format off #define CMO_COMPILER_ARMCC 0 /**< armcc compiler */ #define CMO_COMPILER_IAR 1 /**< iar compiler */ #define CMO_COMPILER_GCC 2 /**< gcc compiler */ // clang-format on #if defined(__CC_ARM) #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 400677) #error "Please use Arm Compiler Toolchain V4.0.677 or later!" #endif #define CMO_COMPILER_TYPE CMO_COMPILER_ARMCC #elif defined(__ICCARM__) #define CMO_COMPILER_TYPE CMO_COMPILER_IAR #elif defined(__GNUC__) #define CMO_COMPILER_TYPE CMO_COMPILER_GCC #else #error "Unknown compiler(now support ARMCC, IAR, GCC)!" #endif 3. 编译器相关的通用宏

命令规则为CMO__$(XXXX), 注意是这里是CMO__双下划线开头表示编译器相关的通用宏。

CMO__ASM嵌入汇编CMO__INLINE内联函数CMO__STATIC_INLINE静态内联函数CMO__STATIC_FORCEINLINE函数使用了强制内联关键字,但实际没有进行内联,将给出警告CMO__NO_RETURN函数没有返回值CMO__CHECK_RETURN函数返回值需要检查,否则编译warningCMO__USED变量或者函数有used属性CMO__WEAK函数是weak类型,可以外部被实现而覆盖CMO__ALIGNED(x)对齐方式CMO__PACKED取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐CMO__RESTRICTrestrict是c99标准引入的,它只可以用于限定和约束指针,并表明指针是访问一个数据对象的唯一且初始的方式 /** * @brief Compiler Marco. * - CMO__ASM * - CMO__INLINE * - CMO__STATIC_INLINE * - CMO__STATIC_FORCEINLINE * - CMO__NO_RETURN * - CMO__CHECK_RETURN * - CMO__USED * - CMO__WEAK * - CMO__ALIGNED * - CMO__PACKED * - CMO__RESTRICT */ #if (CMO_COMPILER_TYPE == CMO_COMPILER_ARMCC) /* ARM Compiler */ #define CMO__ASM __asm #define CMO__INLINE __inline #define CMO__STATIC_INLINE static __inline #define CMO__STATIC_FORCEINLINE static __forceinline #define CMO__NO_RETURN __declspec(noreturn) #define CMO__CHECK_RETURN __attribute__((warn_unused_result)) #define CMO__USED __attribute__((used)) #define CMO__WEAK __attribute__((weak)) #define CMO__ALIGNED(x) __attribute__((aligned(x))) #define CMO__PACKED __attribute__((packed)) #define CMO__RESTRICT __restrict #elif (CMO_COMPILER_TYPE == CMO_COMPILER_IAR) /* IAR Compiler */ #if (__VER__ >= 8000000) #define __ICCARM_V8 1 #else #define __ICCARM_V8 0 #endif #define CMO__ASM __asm #define CMO__INLINE inline #define CMO__STATIC_INLINE static inline #define __FORCEINLINE _Pragma("inline=forced") #define CMO__STATIC_FORCEINLINE __FORCEINLINE CMO__STATIC_INLINE #if __ICCARM_V8 #define CMO__NO_RETURN __attribute__((__noreturn__)) #else #define CMO__NO_RETURN _Pragma("object_attribute=__noreturn") #endif #define CMO__CHECK_RETURN // TODO: #if __ICCARM_V8 #define CMO__USED __attribute__((used)) #else #define CMO__USED _Pragma("__root") #endif #if __ICCARM_V8 #define CMO__WEAK __attribute__((weak)) #else #define CMO__WEAK _Pragma("__weak") #endif #if __ICCARM_V8 #define CMO__ALIGNED(x) __attribute__((aligned(x))) #elif (__VER__ >= 7080000) /* Needs IAR language extensions */ #define CMO__ALIGNED(x) __attribute__((aligned(x))) #else //#warning No compiler specific solution for CMO__ALIGNED.CMO__ALIGNED is ignored. #define CMO__ALIGNED(x) #endif #if __ICCARM_V8 #define CMO__PACKED __attribute__((packed, aligned(1))) #else /* Needs IAR language extensions */ #define CMO__PACKED __packed #endif #define CMO__RESTRICT __restrict #elif (CMO_COMPILER_TYPE == CMO_COMPILER_GCC) /* GCC Compiler */ #define CMO__ASM __asm #define CMO__INLINE inline #define CMO__STATIC_INLINE static inline #define CMO__STATIC_FORCEINLINE __attribute__((always_inline)) static inline #define CMO__NO_RETURN __attribute__((__noreturn__)) #if !defined(__GNUC__) || (__GNUC__ < 3) || \ (__GNUC__ == 3 && __GNUC_MINOR__ < 4) #define CMO__CHECK_RETURN #else #define CMO__CHECK_RETURN __attribute__((warn_unused_result)) #endif #define CMO__USED __attribute__((used)) #define CMO__WEAK __attribute__((weak)) #define CMO__PACKED __attribute__((packed, aligned(1))) #define CMO__ALIGNED(x) __attribute__((aligned(x))) #define CMO__RESTRICT __restrict #else #error Unknown compiler. #endif 4. 编译器相关的特性宏

命令规则为CMO__HAVE_$(XXXX), 注意是以CMO__HAVE固定开头表示编译器特性宏开关。

CMO__HAVE_STATIC_ASSERT检查编译器是否支持_Static_assertCMO__HAVE_TYPEOF检查编译器是否支持typeofCMO__HAVE_BUILTIN_TYPES_COMPATIBLE_P检查编译器是否支持__builtin_types_compatible_p函数 /** * @def CMO__HAVE_STATIC_ASSERT * @brief GCC 4.6 and higher have the C11 `_Static_assert` builtin. */ #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || \ (__STDC_VERSION__ >= 201100) #define CMO__HAVE_STATIC_ASSERT 1 #else #define CMO__HAVE_STATIC_ASSERT 0 #endif /* _Static_assert built in */ /** * @def CMO__HAVE_TYPEOF * @brief GUNC have `typeof` keyword. */ #ifdef(__GNUC__) #define CMO__HAVE_TYPEOF 1 #else #define CMO__HAVE_TYPEOF 0 #endif /* __GNUC__ */ /** * @def CMO__HAVE_BUILTIN_TYPES_COMPATIBLE_P * @brief GUNC have `__builtin_types_compatible_p` function. */ #ifdef(__GNUC__) #define CMO__HAVE_BUILTIN_TYPES_COMPATIBLE_P 1 #else #define CMO__HAVE_BUILTIN_TYPES_COMPATIBLE_P 0 #endif /* __GNUC__ */


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3