ISO C 标准中 __attribute__ 的替换是什么?

发布于 2024-12-06 03:31:12 字数 68 浏览 0 评论 0原文

ISO C 标准中 __attribute__ 的替换是什么? 我想移植我的独立于编译器的软件。

What is the replacement of __attribute__ in ISO C standard?
I want to port my software which is compiler independent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等往事风中吹 2024-12-13 03:31:12

没有一个。

一种解决方案是抽象宏背后的属性。例如:

#ifdef __GNUC__
#define UNUSED __attribute((unused))__
#else
#define UNUSED
#endif

...

void function(void) UNUSED;

There isn't one.

One solution is to abstract the attributes behind macros. e.g.:

#ifdef __GNUC__
#define UNUSED __attribute((unused))__
#else
#define UNUSED
#endif

...

void function(void) UNUSED;
﹏半生如梦愿梦如真 2024-12-13 03:31:12

对于这个 gcc 扩展提供的广泛的功能,没有任何通用的替代品。大多数不兼容 gcc 的其他编译器使用#pragma 来实现类似的目标。从 C99 开始,C 有了 _Pragma 运算符,它允许您在代码中间(不仅在适当的行上)喷出编译指示,并使用宏组成编译指示的内容。但是,您仍然必须对各个功能进行特定的“翻译”以适应目标编译器的相应编译指示语法。

There is no general replacement for the wide range of facilities that this gcc extension offers. Most other compilers that are not gcc compatible use #pragma to achieve similar goals. Since C99, C has the _Pragma operator that allows you to spew pragmas in the middle of your code (not only on proper lines) and to compose the contents of a pragma with macros. But then you still have to do specific "translations" of individual features to the corresponding pragma syntax of your target compiler.

攒眉千度 2024-12-13 03:31:12

C23引入了属性说明符序列,即这些属性:

[[deprecated]]
[[deprecated("reason")]]
[[fallthrough]]
[[nodiscard]]
[[nodiscard("reason")]]
[[maybe_unused]]
[[noreturn]]
[[_Noreturn]]
[[unsequenced]]
[[reproducible]]

可以使用宏__has_c_attribute 来检测这些属性的可用性:

#if defined(__has_c_attribute)
    /* They are available. Further do: */
    #if __has_c_attribute(deprecated)
    /* to check for the availability of individual attributes. */

为了整合 ISO C 和 GNU C 的属性,我经常这样做:

#if defined(__has_c_attribute)
    #if __has_c_attribute(fallthrough)
        #define ATTRIB_FALLTHROUGH        [[fallthrough]]
    #endif
#elif defined(__GNUC__) || defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
    #define ATTRIB_FALLTHROUGH            __attribute__((fallthrough))
#else 
    #define ATTRIB_FALLTHROUGH            /**/
#endif 

#if !defined(ATTRIB_FALLTHROUGH)
    #define ATTRIB_FALLTHROUGH            /**/
#endif

在编译器尚不支持 ISO 的情况下C 的属性,但确实支持 GNU C 的属性,该宏将扩展以利用 GNU C 的 fallthrough 属性。否则,它就会膨胀得一无所有。

C23 has introduced attribute specifier sequences, namely these attributes:

[[deprecated]]
[[deprecated("reason")]]
[[fallthrough]]
[[nodiscard]]
[[nodiscard("reason")]]
[[maybe_unused]]
[[noreturn]]
[[_Noreturn]]
[[unsequenced]]
[[reproducible]]

You can use the macro __has_c_attribute to detect the availability of these attributes:

#if defined(__has_c_attribute)
    /* They are available. Further do: */
    #if __has_c_attribute(deprecated)
    /* to check for the availability of individual attributes. */

To consolidate ISO C's and GNU C's attributes, I often do something like this:

#if defined(__has_c_attribute)
    #if __has_c_attribute(fallthrough)
        #define ATTRIB_FALLTHROUGH        [[fallthrough]]
    #endif
#elif defined(__GNUC__) || defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
    #define ATTRIB_FALLTHROUGH            __attribute__((fallthrough))
#else 
    #define ATTRIB_FALLTHROUGH            /**/
#endif 

#if !defined(ATTRIB_FALLTHROUGH)
    #define ATTRIB_FALLTHROUGH            /**/
#endif

In cases where a compiler does not yet support ISO C's attributes but does support GNU C's attributes, the macro will expand to utilize GNU C's fallthrough attribute. Otherwise, it will expand to nothing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文