简体   繁体   English

如何解决MISRA C:2012 Rule:8.4?

[英]How to resolve MISRA C:2012 Rule:8.4?

I have a C code which I am trying to make it MISRA Compliance.I am getting following error in two different case: 我有一个C代码,试图使其达到MISRA Compliance。在两种不同的情况下出现以下错误:

Case 1]note 9075: external symbol 'buf' defined without a prior declaration [MISRA 2012 Rule 8.4, required] uint32_t buf[BUF_SIZE](in main.c); 案例1]注释9075:未事先声明就定义了外部符号“ buf” [MISRA 2012规则8.4,必需] uint32_t buf [BUF_SIZE](在main.c中);

How can I define it an another way so it will follow MISRA rule ? 我如何以其他方式定义它,使其遵循MISRA规则?

Case 2] note 9075: external symbol 'buf' defined without a prior declaration [MISRA 2012 Rule 8.4, required] uint32_t buf[64U]; 案例2]注9075:未事先声明就定义了外部符号'buf'[MISRA 2012规则8.4,必需] uint32_t buf [64U];

case1:
header.h
#define BUF_SIZE 64U
test.c
#include "header.h"
uint32_t buf[BUF_SIZE];

case2:
test.c
uint32_t buf[64U];

How can I define it an another way so it will follow MISRA rule ? 我如何以其他方式定义它,使其遵循MISRA规则?

If the array is meant to be accessed by code from multiple different files , then put a declaration of it into header.h : 如果要通过代码从多个不同文件访问该数组 ,则将其声明放入header.h

extern uint32_t buf[BUF_SIZE];

Do not omit the extern . 不要忽略extern Do not omit the array size, although standard C permits doing so. 尽管标准C允许这样做,但不要忽略数组大小。 Keep the definition already present in the .c file, unmodified. 保留该定义在.c文件中已经存在且未经修改。

If the array is meant for use only in the file in which it is declared , then make it static: 如果该数组仅用于声明了它的文件中 ,则将其设为静态:

static uint32_t buf[BUF_SIZE];

Do not declare it in any header in this case. 在这种情况下, 请勿在任何标头中声明它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM