Configurable libraries?

Post Reply
Phono
Posts: 92
Joined: Tue May 05, 2015 6:08 am
Location: Ermont, France

Configurable libraries?

Post by Phono » Tue Jun 27, 2017 7:50 am

Hi,
I would like to know if it is possible to configure a library specifically for a sketch, without altering the library code.
For example, in the HardwareCan library (the one I have contributed), there are the following lines:

Code: Select all

#ifndef CAN_RX_QUEUE_SIZE
	#define CAN_RX_QUEUE_SIZE 16
#endif
If I want to modify the value of this constant, may I insert a #define in my sketch, just before the #include:

Code: Select all

#define CAN_RX_QUEUE_SIZE 30
#include <HardwareCAN.h>
so that for this sketch only, the constant is worth 30 instead of 16?

User avatar
RogerClark
Posts: 7481
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Configurable libraries?

Post by RogerClark » Tue Jun 27, 2017 9:45 pm

I don't think this is possible, because the defines would need to be in a header that the library includes.

And the libraries do not include headers from the sketch folder.

With a Make based build system, those defines would be passed directly to the compiler using the -Dxxx syntax, but the Arduino build process does not seem to have a way to inject this

dannyf
Posts: 167
Joined: Wed May 11, 2016 4:29 pm

Re: Configurable libraries?

Post by dannyf » Tue Jun 27, 2017 11:30 pm

may I insert a #define in my sketch, just before the #include:
if that's the way the code is written - and for user configurable macros, that should be how the code is written - the answer to your question is yes.

A better approach is to define that in your IDE. That way, you don't have to worry about the inconsistency of that macro.

User avatar
Rick Kimball
Posts: 1058
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: Configurable libraries?

Post by Rick Kimball » Tue Jun 27, 2017 11:31 pm

Phono wrote:
Tue Jun 27, 2017 7:50 am
Hi,
I would like to know if it is possible to configure a library specifically for a sketch, without altering the library code.
For example, in the HardwareCan library (the one I have contributed), there are the following lines:

Code: Select all

#ifndef CAN_RX_QUEUE_SIZE
	#define CAN_RX_QUEUE_SIZE 16
#endif
If I want to modify the value of this constant, may I insert a #define in my sketch, just before the #include:

Code: Select all

#define CAN_RX_QUEUE_SIZE 30
#include <HardwareCAN.h>
so that for this sketch only, the constant is worth 30 instead of 16?
Just yesterday I wrote up an approach on how one might override defines on the command line for a specific sketch without changing the underlying library code:
https://github.com/stm32duino/Arduino_C ... /issues/41

Of course this assumes the things you wan to change don't override the command line. Your use case seems like the perfect example of my approach.
-rick

Phono
Posts: 92
Joined: Tue May 05, 2015 6:08 am
Location: Ermont, France

Re: Configurable libraries?

Post by Phono » Wed Jun 28, 2017 7:35 am

@dannyf: I omitted to say that I use the Arduino IDE V1.6.9.
@RickKimball: I am lucky you just contributed on this subject! Your method is not straightforward, but I will have a look to it. I dream that the Arduino team think of integrating this feature in their IDE!

Post Reply