开发者

C preprocessor in include header files

开发者 https://www.devze.com 2022-12-18 20:06 出处:网络
I have a structure defined in a header file called data.h. I am including data.h in myfile.c. In the structure, I have part of the variables blocked off with:

I have a structure defined in a header file called data.h.

I am including data.h in myfile.c.

In the structure, I have part of the variables blocked off with:

#ifndef TEST
 int x;
#endif

and in myfile.c I have:

#ifdef TEST
localx++;
#else
mystruct.x++; //<-compiler complains on this line when compiling
#endif

When I try to compile with -DTEST I get a compiler complaining that mystruct type does not containing a field called x. 开发者_运维技巧What is up with this?

I don't have a C compiler handy, so here is what I just typed up:

in data.h

typdef struct {

#ifndef TEST
int x;
#endif
int y;
} coords;

in myfile.c

#include "data.h" 
static coords coord1;
int localx;

int main( ) 
{ 
#ifdef TEST
  localx = 1;
#else
  coord1.x = 1;
#endif

  coord1.y = 2;
  printf("%i\n", coord1.x);
  printf("%i\n", coord1.y);
  printf("%i\n", localx);

  return 0; 
} 

This compiles when I type cc myfile.c but not with cc myfile.c -DTEST I am using the MIPSPro C compiler referenced here.


You most recent edit (which may well be different by the time anyone reads this) will have a problem in the section that has a bunch of printf() statements. The line:

 printf("%i\n", coord1.x);

is referencing the x member of the struct regardless of the setting of the TEST preprocessor macro. It needs to be inside a conditional compilation section too in order to compile correctly (rather not compile at all) when the x member doesn't exist.


Since you are using ifndef for the field x, it is only there to use if TEST is not defined!!

#ifdef allows a section of a program to be compiled only if the macro that is specified as the parameter has been defined, no matter which its value is. For example:

#ifdef TABLE_SIZE

int table[TABLE_SIZE];

#endif  

In this case, the line of code int table[TABLE_SIZE]; is only compiled if TABLE_SIZE was previously defined with #define, independently of its value. If it was not defined, that line will not be included in the program compilation.

#ifndef serves for the exact opposite: the code between #ifndef and #endif directives is only compiled if the specified identifier has not been previously defined. For example:

#ifndef TABLE_SIZE
#define TABLE_SIZE 100
#endif
int table[TABLE_SIZE];

In this case, if when arriving at this piece of code, the TABLE_SIZE macro has not been defined yet, it would be defined to a value of 100. If it already existed it would keep its previous value since the #define directive would not be executed.

From: http://www.cplusplus.com/doc/tutorial/preprocessor/


Except for the typo (typdef), your example compiles fine for me using gcc.

Edit: The new example shouldn't compile. You need to wrap every reference to "x" in #ifdef directives.

Also, gcc accepts the -D flag before the file list, but I don't have access to MIPSpro. The docs say you have the command line out of order.

0

精彩评论

暂无评论...
验证码 换一张
取 消