MISRA C:2012 Rule 8.7
Functions and objects should not be defined with external linkage if they are referenced in only one translation unit
描述
默认 Polyspace® as You Code 分析中反激活了此检查项。请参阅Polyspace as You Code 分析中停用的检查项 (Polyspace Access)。
规则定义
Functions and objects should not be defined with external linkage if they are referenced in only one translation unit 1 .
理由
遵守此规则可避免您的标识符与另一个翻译单元或库中的相同标识符发生混淆。如果通过赋予对象内部链接或无链接来限制或减少对象的可见性,您或其他人就不会轻易无意中访问该对象。
Polyspace 实现
规则检查项标记:
在文件范围内定义但未使用
static指定符且仅在一个文件中使用的对象。未使用
static指定符定义但仅在一个文件中调用的函数。
如果您打算只在一个文件中使用该对象或函数,请将其声明为静态。
如果您使用值为 custom 的选项(例如输入 (-variables-written-in-loop) 和参数 (-variables-written-before-loop) 来显式指定要初始化的一组变量,则检查项不会标记这些变量。检查项假设在实际应用中,除了当前使用这些变量的文件外,包含 main 的文件还必须初始化这些变量。因此,这些变量在多个翻译单元中被使用。
故障排除
如果您预期会出现违规,但未看到该违规,请参阅诊断为何编码规范违规未按预期显示。
示例
检查信息
| 组:声明和定义 |
| 类别:建议 |
| AGC 类别:建议 |
版本历史记录
在 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++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.