ciar

#define #include and #undef in C for embedded


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 *);


Solution

  • 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.