Scope of pre-compiler directives.

What's the scope of a pre-compiler directive? Is it global?

Here's the background. I've written a number of libraries that all use the same library to write to a log file on an SD card. I need to be able to switch between using one of two different (but very similar) libraries to write this file.

My top level sketch includes this pre-compiler directive and conditional statement....

#define LOG3                               // Switches between using the singleton logger <Log3.h> or <log3.h>

#ifdef LOG3                                   // Switches between using the singleton logger based on <Log3.h> or <log3.h>
#include <Log3.h>                             // Logger (opens, writes one line & closes the file)
#else
#include <Log2.h>                             // Logger (opens the log file and leaves it open all the time)
#endif

#include <SingLog2.h>                      // Singleton version of the log file

So if I define LOG3 then the code should use my log3.h library, if I don't define it I should use log2.h

And the code for the logger itself (SingLog2.cpp) includes the following....

#ifdef LOG3
static Log3 logFile;             // Version 3 of the log file library
#else
static Log2 logFile;             // Version 2 of the log file library
#endif

I thought that the scope of the pre-compiler directive was supposed to be global, but experimentation indicates that isn't the case.

If the pre-compiler isn't global, is there anyway to achieve what I'm trying to do? If I can't add a global switch then every time I switch between arduino projects I'm going to have manually edit every library, and then edit them back again next time I change projects again.

Thanks

If I can't add a global switch then every time I switch between arduino projects I'm going to have manually edit every library, and then edit them back again next time I change projects again.

Or put the library files in the same folder as the sketch and #include them using quote marks around the .h filename.

Hello,

It looks like this known bug, which was apparently fixed. So try with latest version of the Arduino IDE.

It looks like this known bug, which was apparently fixed

Ok thanks, so it ought to work and isn't a cock up on my part for once! I'll try upgrading

apparently fixed

Where did you find information that this was fixed?

I've just upgraded to 1.6.5 and still have the same error. I can't find any mention of it being fixed in any release notes.

Or put the library files in the same folder as the sketch and #include them using quote marks around the .h filename.

I would prefer NOT to do this, the libraries get used by multiple projects so I'd need to make multiple copies, one for each project - a configuration nightmare

I understand why you would not want to do it that way, but it is an option.

I need to be able to switch between using one of two different (but very similar) libraries to write this file.

Why not have a single library that is initialised from the main program with different parameters to provide different functionality ?

Why not have a single library that is initialised from the main program with different parameters to provide different functionality ?

That's a possibility, but this particular library is used for general logging so it's used by ALL the libraries I use. Adding an extra parameter to switch functionality would require making changes (albeit minor) to every library I use (that's probably over a hundred libraries!)

Being able to switch between libraries at the top-level sketch would make things a whole lot easier.

What you are doing involves editing the sketch itself anyway so why not edit the #include directly?