MISRA C 2012 中的规则 21.1 指出
#define and #undef shall not be used on a reserved identifier or reserved macro name
This rule applies to identifier or macro beginning with an underscore
Rationale:
Removing or changing the meaning of a reserved macro may result in undefined bahaviour
我不明白为什么宏的名称不能以 unerscore 开头,即使它不是保留宏?例如在我的头文件中:
#ifndef __MY_HEADER_
#define __MY_HEADER_
或者在我正在使用的库中:
#define __I volatile const
我应该更改我所有的代码和我正在使用的库(这是一个大库)以符合此规则还是有更简单的解决方案?
最佳答案
根据 C 标准(第 7.1.3 节),所有 以_[A_Z]
或__
开头的标识符都是保留的。由于它们是保留的,常识和规则 21 禁止您修改(重新定义或取消定义)它们(或创建您自己的)。
因此,您应该将您的代码更改为不使用前导下划线,即使在 include guards 更不用说您的宏了。
可以找到一些进一步的阅读,例如这里:Include guard conventions in C
关于c - MISRA C 2012 - 规则 21.1 - 以下划线开头的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50696794/