【问题标题】:Is __extension__ keyword portable among all compilers?__extension__ 关键字在所有编译器中是否可移植?
【发布时间】:2019-11-09 23:13:30
【问题描述】:

__extension__ 关键字可以在所有编译器中移植吗? 我在 gcc 手册中查看了它,但他们没有提到这个关键字是否可移植。如果有人知道,我将感谢您的帮助。

【问题讨论】:

    标签: c keyword portability


    【解决方案1】:

    没有。 C 标准未涵盖的任何内容都不可移植。这就是为什么如果我们想要(或必须)使用编译器特定的扩展编译指示或属性,我们需要编写如此糟糕的代码的原因。

    #if  defined ( __GNUC__ )
    #ifndef __weak
    #define __weak   __attribute__((weak))
    #endif /* __weak */
    #ifndef __packed
    #define __packed __attribute__((__packed__))
    #endif /* __packed */
    #endif /* __GNUC__ */
    
    
    /* In HS mode and when the DMA is used, all variables and data structures dealing
       with the DMA during the transaction process should be 4-bytes aligned */
    
    #if defined ( __GNUC__ ) && !defined (__CC_ARM) /* GNU Compiler */
    #ifndef __ALIGN_END
    #define __ALIGN_END    __attribute__ ((aligned (4U)))
    #endif /* __ALIGN_END */
    #ifndef __ALIGN_BEGIN
    #define __ALIGN_BEGIN
    #endif /* __ALIGN_BEGIN */
    #else
    #ifndef __ALIGN_END
    #define __ALIGN_END
    #endif /* __ALIGN_END */
    #ifndef __ALIGN_BEGIN
    #if defined   (__CC_ARM)      /* ARM Compiler */
    #define __ALIGN_BEGIN    __align(4U)
    #elif defined (__ICCARM__)    /* IAR Compiler */
    #define __ALIGN_BEGIN
    #endif /* __CC_ARM */
    #endif /* __ALIGN_BEGIN */
    #endif /* __GNUC__ */
    

    【讨论】:

    • 公平地说,您发布的代码似乎是针对执行 DMA 的设备驱动程序。这将是高度依赖于实现的代码,并且可能不严格兼容 C,因为严格兼容的 C 可能无法用于代码尝试执行的操作。
    猜你喜欢
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多