gpt4 book ai didi

c - MISRA C 2012 - 规则 21.1 - 以下划线开头的宏

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:19 26 4
gpt4 key购买 nike

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 inundefined 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com