我正在尝试编写类似于以下内容的宏:
#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/