ISO C 标准中 __attribute__ 的替换是什么?
ISO C 标准中 __attribute__
的替换是什么? 我想移植我的独立于编译器的软件。
What is the replacement of __attribute__
in ISO C standard?
I want to port my software which is compiler independent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有一个。
一种解决方案是抽象宏背后的属性。例如:
There isn't one.
One solution is to abstract the attributes behind macros. e.g.:
对于这个 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.C23引入了属性说明符序列,即这些属性:
可以使用宏
__has_c_attribute
来检测这些属性的可用性:为了整合 ISO C 和 GNU C 的属性,我经常这样做:
在编译器尚不支持 ISO 的情况下C 的属性,但确实支持 GNU C 的属性,该宏将扩展以利用 GNU C 的
fallthrough
属性。否则,它就会膨胀得一无所有。C23 has introduced attribute specifier sequences, namely these attributes:
You can use the macro
__has_c_attribute
to detect the availability of these attributes:To consolidate ISO C's and GNU C's attributes, I often do something like this:
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.