#define,#include和#undef在嵌入式C中的使用方式如下:

huangapple go评论66阅读模式
英文:

#define #include and #undef in C for embedded

问题

我正在尝试理解一段不是我编写的旧代码。

我有一个名为task.c的文件和它的task.h。在task.c的开头有以下内容:

#define TASK
#include "task.h"
#undef
#include "main.h"
...

extern struct s_system sys;

而在task.h中有以下内容:

#ifndef TASK
extern void myfunc(struct my_Struct *);
...
#endif

我正在尝试理解为什么它被构建成这样,而不是像这样在.c中直接包含.h文件:#include "task.h"

我的问题还涉及到编译器(我使用的是IAR 9.20.4)显示的警告,提示:

Warning[Pe231]: declaration is not visible outside of function

对于extern void myfunc(struct my_Struct *);的每一行。

英文:

I'm trying to understand an old code not writte by me.

I've a file called task.c with its task.h. On the task.c, at the very beginning there's

#define TASK
  #include "task.h"
#undef
#include "main.h"
...

extern struct s_system sys;

and in the task.h there's

#ifndef TASK
  extern void myfunc(struct my_Struct *);
  ...
#endif

I'm trying to understand why it has been build like this instead of just include the .h in the .c like #include "task.h"

My question also comes to the fact that the compiler (I'm using IAR 9.20.4) shows a warning saying that

Warning[Pe231]: declaration is not visible outside of function

on every line of extern void myfunc(struct my_Struct *);

答案1

得分: 3

"由于您收到了“声明在函数外不可见”的消息,那么某人在某个时间点某处犯了错误。

extern void myfunc(struct my_Struct *); 本身是一个函数原型作用域内声明 struct my_Struct。每次声明都是一个新类型,仅在函数声明内可见。即使在两个函数声明的实例之间或在函数声明的实例与函数定义之间,它也不是相同的类型,尽管名称相同,都是 struct my_Struct

要正确,类型 struct my_Struct 必须在任何函数之外声明,并在这些声明之前声明。然后函数声明中对它的引用将使用可见类型,而不是声明一个新类型。 (对此,简单的声明 struct my_Struct 足够了。结构内容的定义可以在其他地方,可以在使用它们的地方可见。)

我们不知道发生了什么错误以及何时何地发生了错误。这可能是编写代码的错误,编辑代码的错误,修改项目的构建过程的错误,或者在从旧版本的C更改为现代版本时未更新代码的错误。项目的修订历史可能会提供线索。

无论如何,请在代码中添加 struct my_Struct;。"

英文:

Since you are getting the message “declaration is not visible outside of function”, then somebody made a mistake somewhere sometime.

By itself, extern void myfunc(struct my_Struct *); declares struct my_Struct with function prototype scope. It is a new type each time it is declared and is visible only inside the declaration of the function. It is not even the same type between two instances of that function declaration or between an instance of that function declaration and the function definition, even though the name is the same, struct my_Struct.

To be correct, the type struct my_Struct must be declared outside of any function and before those declarations. Then the references to it inside the function declarations will use the visible type instead of declaring a new type. (The simple declaration struct my_Struct suffices for this. The definition of the structure contents can be elsewhere, visible where they are used.)

We do not know what error occurred where or when. It could be an error writing the code, and error editing the code, and error modifying the project’s build process, or a failure to update the code when changing from some old version of C to a modern version. Revision history of the project might contain a clue.

In any case, add struct my_Struct; to the code.

huangapple
  • 本文由 发表于 2023年7月18日 15:43:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定