【问题标题】:Determine LLVM versus GCC at compile time在编译时确定 LLVM 与 GCC
【发布时间】:2011-11-17 03:56:00
【问题描述】:

我正在尝试编写类似于以下内容的宏:

#ifndef DEPRECATED_ATTRIBUTE_MESSAGE
  #define DEPRECATED_ATTRIBUTE_MESSAGE(message) __attribute__((deprecated (message)))
#endif

这有效,但仅适用于 Apple LLVM 3.0 编译器。它在编译时因其他任何事情而中断,这意味着我必须将其剥离到

#ifndef DEPRECATED_ATTRIBUTE_MESSAGE
  #define DEPRECATED_ATTRIBUTE_MESSAGE(message) __attribute__((deprecated))
#endif

用处不大。

我的问题:

我认为解决方案是在编译时应用一些宏来识别编译器的版本。有没有办法区分 Apple LLVM 3.0 编译器与 LLVM GCC 4.2 或 GCC 4.2(或其他)?

理想情况下,我想解决这样的问题,但我找不到合适的宏来解决它:

#ifdef [Apple LLVM 3.0]
  #ifndef DEPRECATED_ATTRIBUTE_MESSAGE
    #define DEPRECATED_ATTRIBUTE_MESSAGE(message) __attribute__((deprecated (message)))
  #endif
#else
  #ifndef DEPRECATED_ATTRIBUTE_MESSAGE
    #define DEPRECATED_ATTRIBUTE_MESSAGE(message) __attribute__((deprecated))
  #endif
#endif

【问题讨论】:

    标签: gcc xcode4 macros llvm conditional-compilation


    【解决方案1】:

    它应该适用于Clang’s feature checking macros:

    // In case the compiler/preprocessor doesn't support __has_extension
    #ifndef __has_feature         // Optional of course.
      #define __has_feature(x) 0  // Compatibility with non-clang compilers.
    #endif
    #ifndef __has_extension
      #define __has_extension __has_feature // Compatibility with pre-3.0 compilers.
    #endif    
    
    #if __has_extension(attribute_deprecated_with_message)
      #ifndef DEPRECATED_ATTRIBUTE_MESSAGE
        #define DEPRECATED_ATTRIBUTE_MESSAGE(message) __attribute__((deprecated (message)))
      #endif
    #else
      #ifndef DEPRECATED_ATTRIBUTE_MESSAGE
        #define DEPRECATED_ATTRIBUTE_MESSAGE(message) __attribute__((deprecated))
      #endif
    #endif
    

    【讨论】:

      【解决方案2】:

      Apple LLVM 编译器定义了__clang__

      【讨论】:

      • Clang 定义了__clang__,但不幸的是llvm-gcc 没有。有人知道如何区分gccllvm-gcc吗?我需要这个的原因是llvm-gcc 有一些重要的区别。例如,llvm-gcc 4.2 没有定义 __GXX_RTTI,而 gcc 定义了。
      • 这是答案:llvm-gcc 定义了__llvm__
      • llvm-gcc-4.2 没有定义__GXX_RTTI 的事实与它使用 LLVM 的事实无关;这就是 gcc 4.2 的行为方式。
      • 你是说gcc 4.2也没有定义__GXX_RTTI?我以为是的。
      • gcc 4.2 没有定义 __GXX_RTTI。
      猜你喜欢
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多