tholken
November 16, 2016, 6:36am
1
I'm having a bit of a misunderstanding of the pre-processor.
Iam working on with the NeoGPS library and I'm getting errors when trying to use a #if statement
Main TEST_JUNK.ino
#include "TEST_cfg.h"
#if !defined(NMEAGPS_PARSE_GGA) | \
!defined(NMEAGPS_PARSE_GLL) | \
!defined(NMEAGPS_PARSE_GSA) | \
!defined(NMEAGPS_PARSE_GST) | \
!defined(NMEAGPS_PARSE_GSV) | \
!defined(NMEAGPS_PARSE_RMC) | \
!defined(NMEAGPS_PARSE_VTG) | \
!defined(NMEAGPS_PARSE_ZDA)
#error CRAP!
#endif
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
TEST_cfg.h File
#define NMEAGPS_PARSE_GGA
//#define NMEAGPS_PARSE_GLL
//#define NMEAGPS_PARSE_GSA
//#define NMEAGPS_PARSE_GSV
//#define NMEAGPS_PARSE_GST
#define NMEAGPS_PARSE_RMC
//#define NMEAGPS_PARSE_VTG
//#define NMEAGPS_PARSE_ZDA
Here's the odd part..
When I uncomment all the #define statements in the TEST_cfg.h file the Following IF statement works.
If I comment out any ONE it fails
TEST_JUNK:19: error: #error CRAP!
#error CRAP!
^
exit status 1
#error CRAP!
It's odd as I think it would be an OR statement... yet its treating it like an AND statement.
TEST_cfg.h (222 Bytes)
TEST_JUNK.ino (637 Bytes)
system
November 16, 2016, 7:15am
2
It's odd as I think it would be an OR statement... yet its treating it like an AND statement.
There is a difference between a bitwise OR (|) and a logical OR (||). Perhaps if you used the correct operator...
tholken
November 16, 2016, 7:08pm
3
Same issue..
Logical or Bit Wise.. it still treats it as an AND
Id hate to have to do it individually.
So, you want the preprocessor to barf if any of those constants is not defined. Preprocessor is barfing. Ergo, one of those constants is not defined. Sweet! Indeed, I see in your TEST_cfg that NMEAGPS_PARSE_GLL is not defined. So, the preprocessor is doing exactly what you asked it to do.
Did you want it to barf if NONE of them are defined? Or to put it another way, if ALL of them are undefined? In that case you need either
! (a | b | c | d | e)
or
!a & !b & !c & !d & !e
tholken
November 17, 2016, 11:53pm
5
Ok, so I guess my problem is a Logic problem
My basic misunderstanding is that I thought
! (a | b | c | d | e)
Is equivalent to
!a | !b | !c | !d | !e
You are right... I am trying to validate that at least ONE element is defined.... I don't care which one.
tholken:
Ok, so I guess my problem is a Logic problem
My basic misunderstanding is that I thought
! (a | b | c | d | e)
Is equivalent to
!a | !b | !c | !d | !e
You are right... I am trying to validate that at least ONE element is defined.... I don't care which one.
They are not equivalent. In math-speak, it is know as De Morgan's laws .
MarkT
November 18, 2016, 1:05am
7
Google 'de Morgans law'
Or put another way, if I am not tired and I am not hungry, then I am not tired or hungry.
tholken
November 18, 2016, 4:39am
8
Thanks! Perfect Explanation
"if I am not tired and I am not hungry, then I am not tired or hungry."
had to read the De Morgan's laws a few times to understand it.
I agree, that's the perfect example to use. I shall steal it for future use.
And I can understand how you got tripped up. De Morgan's laws are the kind of thing that aren't obvious if you aren't explicitly taught it and you don't write out the truth tables. I know I raised an eyebrow when it came up in class.