Define your preprocessor
-
I'm using
#define
to setup whether certain code gets included or not in my Arduino project. Works fine for statements like:#if c_Use_RFID //...Do the RFID code... #endif
Now, I'm making tie-in sections, trying to do:
#if c_Use_RFID && c_Use_Net //Do the thing with both RFID and Net #endif
This seems to not work, and the section is never included (apparently) as demonstrated:
(if I put just one of the two in the#if
, it works as expected)What am I doing wrong here? (besides, you know, the premise of conditional compilation etc)
-
@Tsaukpaetra just do two
#if
s and be done with it.
-
@Gąska said in Define your preprocessor:
@Tsaukpaetra just do two
#if
s and be done with it.I hope not to have to, it's already incredibly spaghetti as it is.
-
@Tsaukpaetra said in Define your preprocessor:
c_Use_Net
I am a dummbly. Apparently, this was not properly capitalized. Which, in preprocessor-land means it just represents nothing (since it was not set?)
Yay consistency?
-
@Tsaukpaetra while you’re at it, change your defines to ALL_CAPS.
-
@topspin said in Define your preprocessor:
@Tsaukpaetra while you’re at it, change your defines to ALL_CAPS.
Sometime later, perhaps. I want maximal confusion!
-
@Tsaukpaetra said in Define your preprocessor:
I hope not to have to, it's already incredibly spaghetti as it is.
@Tsaukpaetra said in Define your preprocessor:
I want maximal confusion!
-
@Gąska said in Define your preprocessor:
@Tsaukpaetra said in Define your preprocessor:
I hope not to have to, it's already incredibly spaghetti as it is.
@Tsaukpaetra said in Define your preprocessor:
I want maximal confusion!
Spaghetti is worse than Confusion!
At least, at this point. I'm about 21 percent to MVP and the code is already fucking tall...
-
@Tsaukpaetra
#if defined(c_Use_RFID) && defined(c_Use_Net)
-
@LB_ said in Define your preprocessor:
@Tsaukpaetra
#if defined(c_Use_RFID) && defined(c_Use_Net)
Except they should both be defined and be
0
or1
...
-
@Tsaukpaetra Oh, then the original way you were doing it should work, it's odd that it isn't working for you. What compiler is that?
-
@LB_ said in Define your preprocessor:
@Tsaukpaetra Oh, then the original way you were doing it should work, it's odd that it isn't working for you. What compiler is that?
Arduino's c++, which I've been told it gcc???
Apparently it's not an error to have non-defined stuffs in the
#if
expression...
-
@Tsaukpaetra Must be a pretty broken version then... https://gcc.godbolt.org/z/17T8q1
-
@LB_ said in Define your preprocessor:
@Tsaukpaetra Must be a pretty broken version then... https://gcc.godbolt.org/z/17T8q1
No no, it should have been more like this:
The actual define was
c_Use_NET
, but the expression called itc_Use_Net
.
-
@Tsaukpaetra Wow... that's bizarre... seems to work in clang, and gcc, and MSVC's latest versions, what a strange and awful feature of C...
-
@LB_ said in Define your preprocessor:
@Tsaukpaetra Wow... that's bizarre... seems to work in clang, and gcc, and MSVC's latest versions, what a strange and awful feature of C...
Eww.... the standard uses more jargon, but the situation boils down to:
After all macro expansion and evaluation of defined ... expressions, any identifier which is not a boolean literal is replaced with the number 0 (this includes identifiers that are lexically keywords, but not alternative tokens like and).
-
@Watson said in Define your preprocessor:
any identifier which is not a boolean literal is replaced with the number 0
So, almost like my assumptions.
-
@Tsaukpaetra said in Define your preprocessor:
Now, I'm making tie-in sections, trying to do:
#if c_Use_RFID && c_Use_Net //Do the thing with both RFID and Net #endif
This seems to not work, and the section is never included (apparently) as demonstrated:
But those two don't show the same expression for the
#if
.
In this sort of thing, being exact is vital because the problem can be the most trivial of things.
-
@dkf said in Define your preprocessor:
@Tsaukpaetra said in Define your preprocessor:
Now, I'm making tie-in sections, trying to do:
#if c_Use_RFID && c_Use_Net //Do the thing with both RFID and Net #endif
This seems to not work, and the section is never included (apparently) as demonstrated:
But those two don't show the same expression for the
#if
.
In this sort of thing, being exact is vital because the problem can be the most trivial of things.The screenshot is a later attempt to explicitly make them bool-like. The pseudo-code similarly did not work.
-
@Tsaukpaetra said in Define your preprocessor:
The screenshot is a later attempt to explicitly make them bool-like. The pseudo-code similarly did not work.
Well, if nested
#if
s work then do that. An extra line or two of code doesn't hurt much.And put a comment on the
#endif
lines with the matching opening condition so you can see what pairs up with what. Otherwise things become really confusing all too easily.
-
@Tsaukpaetra said in Define your preprocessor:
Apparently it's not an error to have non-defined stuffs in the #if expression...
That's surprising... Maybe just for that compiler. I know MS chokes. Which leads to some tests like:
#if defined(SOMETHING) && SOMETHING #endif