【问题标题】:Compatible declaration for __attribute__ ((section(".abc.dfe"))) const volatile uint8 attributeVariable = 0; - MISRA compliant__attribute__ ((section(".abc.dfe"))) 的兼容声明 const volatile uint8 attributeVariable = 0; - 符合 MISRA
【发布时间】:2019-05-10 10:24:17
【问题描述】:

以下 const 在我正在测试 MISRA 准则违规的项目文件中声明为

__attribute__ ((section(".abc.dfe"))) const volatile uint8 attributeVariable = 0;

MISRA 测试产生以下消息

A compatible declaration shall be visible when an object or function with external linkage is defined. 
Global definition of 'attributeVariable ' variable has no previous declaration.

我已经修复了其他未使用 __attribute__ 关键字的全局定义,将其声明为

extern const volatile uint8 attributeVariable;

在头文件中。我不确定是否可以在使用 __attribute__ 关键字时以相同的方式在标题中编写声明。 __attribute__ 会影响我编写变量外部声明的方式吗?

【问题讨论】:

  • 你应该测试一下。当您的声明不包含该属性时会发生什么?
  • 我会,但我担心我可能会改变函数的行为,因为我不是文件的作者——我的团队从以前的开发人员那里“继承”了它。我认为 MISRA 在这种情况下会产生警告消息,但我宁愿采取更谨慎的方式,咨询对代码中到底发生了什么有更深入了解的人。

标签: c++ c embedded misra


【解决方案1】:

这里有两个问题。

首先,MISRA-C 要求代码应该是标准 C,因此您必须偏离使用标准 C 的规则。

其次,MISRA-C 不希望您在文件范围内声明不是static 的变量。不仅 MISRA-C 不赞成全局变量,所以问问自己是否真的必须在所有地方公开这个变量,或者是否可以通过 setter/getter 函数来访问它。

话虽如此,我相信__attribute__ 的工作方式与其他类型限定符非常相似。你可以写在声明的开头或结尾等处。所以写应该没有问题,例如:

extern const volatile uint8_t attributeVariable __attribute__ ((section(".abc.dfe")));

【讨论】:

  • 您甚至可以在 extern 声明中删除 __attribute__,因为代码不应该真正关心 extern 的定义和分配位置 - 尽管我不确定那里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
  • 1970-01-01
  • 2016-01-11
  • 2013-08-23
相关资源
最近更新 更多