scope of a #define statement

Hi,

Here's the problem, I've got a library that I use on a variety of projects. Sometimes it's used on a Mega (with plenty of memory) and sometimes on a mini-pro (not much memory).

To cope with the different memory available, I use a #define statement to enable or disable some of the less important functions that I don't need when used on memory-scarce projects.

Code looks a bit like this.......

gps.h

#ifndef gps_h
#define gps_h

#define LOADS_OF_MEMORY

class gps
{
  public:
    
    gps(); //Constructor
    void funcA(void);
    void funcB(void);

#ifdef LOADS_OF_MEMORY
    void funcC(void);
    void funcD(void);
#endif

  private:

};


#endif

So, when I know I'm going to be short of memory I comment-out the line '#define LOADS_OF_MEMORY' and the two non-essential functions funcC() and funcD() will get missed from the build.

What I'm doing works ok, but it does have a problem (not a coding problem, but a human problem). The catch is that because the #define statement is in the .h file for the library then I need to remember to edit that file before I build the project. If I then forget what I've done and come to use the same library in a later project then sometimes I get problems because half the functions have been missed due to the #define.

I thought the solution would be to remove the #define from the library header file, and put it in the top-level arduino sketch (the .ino file) that defines the overall project. That way I don't need to change the library file each time a build a different project.

The problem seems to be that scope of the #define doesn't extend beyond the .ino file into the .h file.

Is there a solution? can I extend the scope of the #define? or is there a better approach to this problem?

Ok - just solved the problem myself.

The issue wasn't with the scope of the #define, it was with the order.

If I include the #define line before #include<gps.h> then it works ok

Can I give myself karma for solving my own problem myself? :slight_smile:

The problem seems to be that scope of the #define doesn't extend beyond the .ino file into the .h file.

"into the .h file" is not a correct interpretation of what happens. The text in the header file is placed in the sketch in exactly the place that the #include statement was, when the preprocessor runs.

The order of preprocessor statements matters.

Can I give myself karma for solving my own problem myself?

Only after subtracting karma for having caused the problem yourself.

The linker does NOT include functions which are not called in to the finished code. So -ve points for playing with a no existent problem.

Mark

loads of included files suck up RAM by allocating stuff that the linker does NOT optimize out... so while your comment is superficially true in this example, it's wrong almost all the time in real life.

@cndg, please post an example.