gcc - 在编译时确定 LLVM 与 GCC

标签 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

最佳答案

它应该与 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

关于gcc - 在编译时确定 LLVM 与 GCC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7419167/

相关文章:

iphone - 雪豹更新到 xcode 4.1 后 SVN 不工作

rust - Rust宏: What is the difference between braced and parenthesized macro bodies?

c++ - boost::call_traits - 为什么 gcc 为此给出 false?

c++ - 关于 C++ STL 中的类型

objective-c - “#import <map>”的词法或预处理器问题

c - 如何通过宏在两个空指针之间复制内存?

c++ - constexpr 无需回归即可替换宏

c - 错误: ‘string’ undeclared (first use in this function) gcc OS X

linux - 如何在确定地址(gcc,linux)的可执行文件中创建 "empty"空间?

ios - 在 Main.storyboard 和 AppDelegate.h 之间建立联系