在编译时确定LLVM与GCC

时间:2011-09-14 15:44:52

标签: gcc xcode4 macros llvm conditional-compilation

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

#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

2 个答案:

答案 0 :(得分:4)

它适用于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

答案 1 :(得分:1)

Apple LLVM编译器定义__clang__

相关问题