MISRA C:2012 Rule 8.7
R2026bFunctions and objects should not be defined with external linkage if they are referenced in only one translation unit
描述
默认 Polyspace® as You Code™ 分析中反激活了此检查项。请参阅 Checkers Deactivated in Polyspace as You Code Analysis (Polyspace as You Code)。
规则定义
Functions and objects should not be defined with external linkage if they are referenced in only one translation unit 1 .
理由
遵守此规则可避免您的标识符与另一个翻译单元或库中的相同标识符之间产生混淆。如果通过赋予对象内部链接或无链接来限制或降低对象的可见性,那么您或其他人无意中访问该对象的可能性就会降低。
Polyspace 实现
规则检查项会标记:
在未使用
static设定符的情况下在文件作用域内定义,但仅在一个文件中使用的对象。在未使用
static设定符的情况下定义,但仅在一个文件中调用的函数。
如果您打算仅在一个文件中使用对象或函数,请将其声明为 static。
如果您使用输入 (-variables-written-in-loop) 和参数 (-variables-written-before-loop) 等选项,并将值设为 custom 以显式指定一组要初始化的变量,则检查项不会标记这些变量。检查项假定在实际应用中,除了当前使用这些变量的任何文件之外,包含 main 的文件也必须对这些变量进行初始化。因此,这些变量用于多个翻译单元中。
故障排除
如果您预期会出现违规,但未看到该违规,请参阅诊断为何编码规范违规未按预期显示。
示例
检查信息
| 组:声明和定义 |
| 类别:建议 |
| AGC 类别:建议 |
PQL 名称:std.misra_c_2012.R8_7 |
版本历史记录
在 R2014b 中推出
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C:2025
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.