There is a certain structure described in the header file.

typedef struct i_str { int index; #define ERR_N_1 0 int err_n_1; int stat_index; struct { #define STATE_N_1 0 int state_n_1; } state; } i_str 

In the main file there is another function that outputs this structure by comparing the index with the error number with the help of some function comp(a, b) :

 void i_str_dump(struct i_str *msg) { if(comp(msg->index , ERR_N_1)) printf("Error_1: %i", msg->err_n_1); if(comp(msg->stat_index, STATE_N_1)) printf("state 1: %i", msg->state.state_n_1); } 

And here it is not compiled, the problem is in #define STATE_N_1 0 . For some reason, this define is not perceived by the compiler. How can I access it without changing anything in the header file?

  • transfer all #define to the global code area (before the structure declaration) so that it acts in all the code - perfect
  • what exactly is writing? And in the "main file" is #define STATE_N_1 0 ? I didn’t check but maybe define works first in the header file and only then the result is added to the main file ... - ProkletyiPirat
  • And what's the problem? The compiler is in pure Russian-English and declares: the problem with #define STATE_N_1 0 ? By the way, there, in the header file with a semicolon at the end is not enough. - alexlz
  • 3
    Again, please cure the underground knock. Standard answer: ПСНЛ (underground knock is not curable). - alexlz
  • four
    So. We can’t describe the problem as the compiler swears - we don’t say we are offended by the questions. Dude, you're wrong. If you want to help, do not portray the partisan in captivity by the Germans. Pieces of text and compiler messages - in the studio. - alexlz

1 answer 1

@bunch , if you collect the presented fragments of your code and add to the beginning #include <stdio.h> , and the symbol ; after i_str then

gcc -c normally compiles, and g ++ naturally swears on undeclared comp() .

 avp@avp-ubu1:~/hashcode$ gcc define.c -c avp@avp-ubu1:~/hashcode$ echo $? 0 avp@avp-ubu1:~/hashcode$ g++ define.c -c define.c: In function 'void i_str_dump(i_str*)': define.c:18:31: error: 'comp' was not declared in this scope define.c:20:38: error: 'comp' was not declared in this scope avp@avp-ubu1:~/hashcode$ 

And the location of #define STATE_N_1 0 nothing to do with it.

If swearing goes, like:

 avp@avp-ubu1:~/hashcode$ gcc define.c -c define.c:13:0: warning: "STATE_N_1" redefined [enabled by default] define.h:1:0: note: this is the location of the previous definition avp@avp-ubu1:~/hashcode$ 

then this means that the identifier was already defined earlier (for example, in the included .h file) with a different value .