Conditional compiles in a library

I'm creating a library where I would like the user to be able to exclude specific parts from it in order to save some space if the memory is getting tight. The user should not be forced to edit the library, but just his own sketch.

In an ordinary gcc-project I'll probably just handle that in the makefile with some variables that will ifeq and add some defines to CFLAGS so it will be preprocessed on all .h and .ccp-files. Easy as pie....

Is there any neat trick that can do the same thing in the Arduino universe?

Doing

#define NO_FOO
#include <mylib.h>

only affects the mylib.h. The mylib.cpp never sees the NO_FOO-variable.

I've tried to put the #define in a separate .h-file

#include <mylib_no_foo.h>
#include <mylib.h>

in the hopes that some of the Arduino magic preprocessing would add all #includes too all source files but no dice. (I'm not really 100% how the Arduino IDE does its stuff.)

So - is it possible do do this? Or should I just let the users suffer?

Try this...

In the Sketch directory, create a file named "mylib_options.h". Put the #define in there. The user now has easy control over the build settings.

In your library, add the following...

#include "mylib_options.h"

Your library now has a way to include the build settings.

The downside is that the users may have to always provide a "mylib_options.h" file. Depending on the compiler search order, you may be able to include an empty / default version with your library.

Thanks for the tip. I'll focus my efforts using that technique...

/mats