tags:

views:

282

answers:

3

Hi All.

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?

edit..adding this. I dont 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: http://www.sgi.com/products/software/irix/tools/c.html

+1  A: 

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/

Diego Dias
Registered User Join Date: Nov 2009Posts: 23 in data.hCode:typdef struct {#ifndef TESTint x;#endifint y;} coords;in myfile.cCode:#include "data.h"static coords coord1;int localx;int main(){#ifdef TESTlocalx = 1;#elsecoord1.x = 1;#endifcoord1.y = 2;printf("%i\n", coord1.x);printf("%i\n", coord1.y);printf("%i\n", localx);return 0;}This compiles with "cc myfile.c" but does not compile with "cc myfile.c -DTEST" I am using the MIPSpro compiler on IRIX
Derek
He has the logic the right way 'round: if TEST is defined, don't add the variable to the struct and use the local variable; if TEST is not defined, add the variable to the struct and use it.
UncleO
the #ifdef logic he shows is correct. He runs with -DTEST therefore x in struct should be defined, and so the code should compile. HE has a got subtle error somewhere, uncleo tried his posted code and it works fine
pm100
@pm100,@uncleo : You are right, my post was made before the edits of the question ... I might edit my post as well now
Diego Dias
actually my comment is wrong, with #ifndef and -DTEST the struct x is not there , but the compile should not be trying to use it
pm100
A: 

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.

UncleO
+2  A: 

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.

Michael Burr