Override Define

Hi,

I am using a library, that #defines BAUDRATE 9600 somewhere in the code.
I want to use this lib, but with another baud rate for Serial interface.
Is there a chance to override this define? I put it on build_flags, but it doesn’t help.

I’m not sure of the order of priority, so this may not work. But if it’s defined, first thing you would normally want to do is UN-define it… via build_unflags. Then you can use build_flags to define it. However, I have this nasty feeling it’s really for undefining platform related defines, not library level defines… but worth a try!

Other option may be to make a include header, that gets pulled in before that library, which defines BAUDRATE before the library gets pulled in… i.e. if it’s called SerialLib, make a config.h header , include it before SerialLib is included, and have something like the following in it:

#ifdef BAUDRATE 
#undef BAUDRATE
#define BAUDRATE 115200
#endif 

The undef and ifdef should really be needed if it included before SerialLib, but if it didn’t, this would hopefully still ensure it’s set to the right value.

thanks for your reply.
Unfortunately, both suggestions didn’t work.

It seems, that the #defines are completely ignored in a static linked library. Is that possible?
What I tried:

  • Use build_unflags
  • Use build_flags = -U BAUDRATE (Redirecting...)
  • Use #ifdef/#undef at the very top of my main.h file
  • The config.h is protected by include guards. I copied the whole content in a new file with the same include guards. VSCode is showing that the config.h is not going to be loaded (greyed out), but the lib is still using the wrong #define. That’s why I am wondering if I even can override a definition in a static linked lib?
1 Like

I found out one interesting thing and that proofs - from my point of view - that the lib is using different BAUDRATE / define.
Serial.printlns that came out of the library arriving with 9600 baud, but Serial.printlns from my code are sent and received with 115200 baud.
As the same Serial interface is used, it’s likely that the initialization Serial.begin in my code is executed later. I do not see any other possibility, but I am no C/C++ expert …

1 Like

I don’t know much about how this side of things work in compiler land. From what I do know of them, I’m pretty sure you’re right, since it is a binary that has already been compiled, making things like defines ‘hard-coded’.

Ok thanks for your evaluation.
That means, there is no way to override that define, right?

That’s my educated guess, yes! :wink:

To give a more definitive answer actual code would be needed, and then one of the more experience programmers on the forum would be more likely to respond.

Pls see here: Stackoverflow
It shows that indeed a static library (.a) file is precompiled and you just use the header and provide the .a for the linker to include. No way to change the #define.

BUT: You can of course modify the lib file yourself if you have its source code at hand, and like argumented in the post, have several configs of the library in place.
I did it myself in one of my projects (not the sophisticated approach as described in the post, I just changed the #define to the value I needed and recompiled the lib :slight_smile:)

1 Like