#define directive ignored on IDE 1.8.2

I have had a program for quite some time that compiled great in IDE v1.6.5. I have upgraded to 1.8.2 and have used that code base to move onto newer things. The sketch code has a #define in it which is checked in a library module to cause different code generation. The normal way of operating. Well, it is supposed to be honored in the library. It is now being ignored. Didn't used to be.

It seems like, now, the libraries are being compiled ahead of the sketch and #defines which are directed at the library are coming too late to be utilized because of the late compiling of the sketch.

This is easily reproduced. Make a sketch and a little library. Have a #define in the sketch which, when utilized, will remove a bad line of code in the library. Put in the #define in the sketch and it is ignored. When put into the library, it is honored.

So why can't I just put the #define in the library? Well, the reason I have to put it in the sketch is the usual. I have two different target machines and for one, I need the #define to make the code do one thing and for the other, I remove the #define so it will compile a different way. This has been working for a long time and now fails.

Any suggestions? Until this is made to work again, I will have to edit the library when I want to compile for a different target but I really need this working again.

Mike

Sketch code:

#define BadLine         // Comment/Uncomment this for different compilation in the library.
#include "SomeLibrary.h" 
void setup() {
}
void loop() {
}

Library code:

int i;
#ifdef BadLine
  Stuff = BadStuff
#else
  i = 1;
#endif

Is that your .h file?

It won't compile.

I=1 is illegal outside a function.

One, 38 posts and no code-tags... Takes you a while to read How to use the forum ;)

And next, that's not a sketch... It gives you 0 feedback so you have NO idea...

Third, made a real test Sketch

#define BAD
#include "bla.h"

void setup() {
  Serial.begin(115200);
  printIt();
}

void loop() {
  
}

bla.h

#ifdef BAD
void printIt(){
  Serial.println("Yes, it's bad!!!");
}
#else
void printIt(){
  Serial.println("No, it's not that bad");
}
#endif

And it print "Yes,.." as expected and "No,..." if I comment it. So it most be something more complex in that code you don't show.

There is no way that putting a #define in your sketch EVER affected the compilation of a library.

It's somewhat possible that it affected the compilation of the sketch itself in ways that happened to appear to work in some particular instance, since I've seen C++ libraries that include "minor" method code within the .h file.

Provide the actual details...

So

MikeyMoMo: I have had a program for quite some time that compiled great in IDE v1.6.5. I have upgraded to 1.8.2 and have used that code base to move onto newer things. The sketch code has a #define in it which is checked in a library module to cause different code generation. The normal way of operating. Well, it is supposed to be honored in the library. It is now being ignored. Didn't used to be.

It seems like, now, the libraries are being compiled ahead of the sketch and #defines which are directed at the library are coming too late to be utilized because of the late compiling of the sketch.

This is nothing related to the IDE version. i.e. if you were to take the same code you have now and go back to the older IDE and build it, it would fail the same way. Putting a #define in a sketch to modify library compilation is definitely not "The normal way of operating". In fact, what you describe it is simply not possible unless the Arduino library placed its code inside the class definition inside the library .h header file instead of in a .cpp file. This is typically not the way Arduino libraries are written. A very few that do that, contain a templated class as that is required for a templated class, in a few other cases like my hd44780 library, the code is inside the class definition in a header file rather that using a .cpp file. But this is very very rare in Arduino libraries.

The reason you can't affect generated code in a library is that the library code is compiled separately from your sketch and the library code module has absolutely no knowledge of what you may have defined in your sketch code module.

--- bill