c - Missing declaration for object definition (Rule MisraC2012-8.4)
问题描述
I try to understand what to do with this error:
required: Missing declaration for object definition [hi] (Rule MisraC2012-8.4)
My code is:
typedef struct {
int l;
} some_struct;
some_struct hi = {
5
};
int main(void) {
return(0);
}
static solve this issue but I need to use this variable as extern from another file.
解决方案
Rule 8.4 is regarding type incompatibility of objects with external linkage. There's a possible severe bug that the rule aims to prevent from: declaring the object as one type in one file, then define it as another type in another file.
To prevent such bugs, MISRA therefore requires that a previous extern
declaration of the object is visible to the file that contains the definition.
So if you aim to have some_struct hi
defined at file scope, you must add a declaration somewhere too, preferably in a header file:
extern some_struct hi;
static
solves the problem because then you no longer have external linkage and the rule does not apply then.
Please note that (non-const) extern
spaghetti globals are highly discouraged in any form of program, MISRA or no MISRA. In the vast majority of possible cases, the presence of such global variables in a program is caused by bad program design.
推荐阅读
- javascript - 错误:找不到选择器的节点:#email
- ruby-on-rails - Gemfile 冲突版本
- django - 在 Django 网站的管理页面中创建普通用户后,我无法使用该普通用户登录
- ios - SwiftUI iOS 14 日期选择器奇怪的行为
- python - 切换名字和姓氏顺序的功能
- javascript - 如何使用 discord.js 获取另一个单词之后的单词
- c# - Ubuntu 版本 Edge 上的 Chromium Edge 驱动程序
- python - 遍历列表以在 Jinja、Python-Flask、SQLite 中创建表
- java - Dataproc Spark 作业因 IO 异常而失败,当历史服务器尝试访问 GCS 存储桶时无法初始化 Spark 上下文
- java - 在 Java 中拆分字符串的问题