Preprocessor processes defines unexpectedly

I am in the process of writing a library and I am not getting the results that I am expecting when from some preprocessing commands.

Below is the block of preprocessor code in my header file, foo.h:

#if G_VAL < GS_90
#define G_CONVERSION 0
#define MODE 31

#else
#define G_CONVERSION 50
#define MODE 39
#endif

Relevant PDE source:

//#define G_VAL GS_15
#define G_VAL GS_90

#include <MMA7361.h>

If I define G_VAL in foo.h, I have no issue. When I move the #define to a pde file, prior to the #inlcude<foo.h> line, I have problems. MODE which is only used in the pde file is properly set, but G_CONVERSION which is used in foo.cpp is not set correctly. They are always set to 1 and 0 respectively regardless of the value assigned to G_VAL. If I remove the #defines from the first part when G_VAL == GS_90, the compiler throws a couple of errors stating that G_CONVERSION is not in the scope. There is no error with MODE.

Are the preproccesor commands in the pde file processed after foo.cpp and foo.h are compiled regardless of the fact that the include is after those commands? Or, am is there something that I should/can fix?

Thanks

#if G_VAL < GS_90

The processor directives are not really designed for mathematics.

If I remove the #defines from the first part when G_VAL == GS_90, the compiler throws a couple of errors stating that G_CONVERSION is not in the scope. There is no error with MODE.

But, it’s probably not your value of MODE that is in scope.

When I move the #define to a pde file, prior to the #inlcude<foo.h> line, I have problems

The Arduino IDE does some stuff to your code prior to invoking the preprocessor. Try putting a dummy include statement before your #defines. The file to be included must actually exist, but not have to contribute anything useful to the code.

#if G_VAL < GS_90

The processor directives are not really designed for mathematics.

The goal with the comparison was to see what value was assigned to G_VAL. Would it be better to replace the above code with #ifndef GS_90 and just use #define GS_90 in place of #define GS_VAL GS_90?

If I remove the #defines from the first part when G_VAL == GS_90, the compiler throws a couple of errors stating that G_CONVERSION is not in the scope. There is no error with MODE.

But, it’s probably not your value of MODE that is in scope.

Actually that was what baffled me, the value assigned to MODE was in fact 39, the correct value. G_CONVERSION was 0, the wrong value.

When I move the #define to a pde file, prior to the #inlcude<foo.h> line, I have problems

The Arduino IDE does some stuff to your code prior to invoking the preprocessor. Try putting a dummy include statement before your #defines. The file to be included must actually exist, but not have to contribute anything useful to the code.

I created a file , dummy.h, and placed #include <dummy.h> at the top of the pde file. It did not fix the problem.

Thanks for your thoughts.

I think I might use constants in place of the #defines and allow the program to change the mode at run time. That said if there are any other ideas as to why I got the behavior that I did, I am still very interested.

The goal with the comparison was to see what value was assigned to G_VAL. Would it be better to replace the above code with #ifndef GS_90 and just use #define GS_90 in place of #define GS_VAL GS_90?

I think it would.

Actually that was what baffled me, the value assigned to MODE was in fact 39, the correct value. G_CONVERSION was 0, the wrong value.

Not defined, and defined with the wrong value, are two different things. Terminology is very important.

I think I might use constants in place of the #defines and allow the program to change the mode at run time. That said if there are any other ideas as to why I got the behavior that I did, I am still very interested.

Personally, I think that is a much better solution. Except, of course, you can't modify constants (that's the meaning of const).