1

Consider following header file,

    // filename : packet_types.h
    #ifndef _PACKET_TYPES_
    #define _PACKET_TYPES_

    struct Packet {
        enum Type {
           typ_ONE,
           typ_TWO, 
           typ_THREE,
        };

        static const char * STRINGS[] = { 
           "ONE",
           "TWO",
           "THREE"
        };

        const char * getText( int enum ) {
             return STRINGS[enum];
        }
    };
    #endif

As Arduino has limited memory, I don't want to include this portion of code when I include this file, packet_types.h,

        static const char * STRINGS[] = { 
           "ONE",
           "TWO",
           "THREE"
        };

        const char * getText( int enum ) {
             return STRINGS[enum];
        }

But for my GUI application, I want the complete file. I want to use same file for both Arduino and GUI, but how can I add compiler directive #define,#ifdef, #ifndef... to do this job, when I include this file from some other file(main.cpp). Thanks.

Oli
  • 1,313
  • 14
  • 31
  • 2
    Bad name choice for the header guards there. Any name (be it pre-processor macros, variables, class or function names or anything else) starting with underscore followed by an upper-case letter is reserved for the implementation (as is any name containing two consecutive underscores, anywhere) and should not be used in user code. – Jesper Juhl Oct 23 '16 at 13:49

1 Answers1

3

Although it is possible to use some preprocessor juggling, the right solution is to simply use C++ as it was intended to be used. Only declare these class members and methods in the header file:

    static const char * STRINGS[];

    const char * getText( int enum );

And then define them in one of the source files:

    const char * Packet::STRINGS[] = { 
       "ONE",
       "TWO",
       "THREE"
    };

    const char * Packet::getText( int enum ) {
         return STRINGS[enum];
    }

With the aforementioned preprocessor juggling, all it ends up accomplishing is the same logical result, but in a more confusing and roundabout way.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • It would be great if you point out how I can I do using compiler directive only. For Example – Oli Oct 23 '16 at 13:39
  • 1
    Just put the necessary bits inside an `#ifdef`, and define the symbol only in one of the source files, before its `#include`. You already know how to use `#ifndef`, so you already know everything you need to do. – Sam Varshavchik Oct 23 '16 at 13:41
  • thanks @sam, Including #define TEXT before including #include "packet_types.h" did the job, where I enclosed the unwanted code in #ifdef TEXT ... #endif. – Oli Oct 23 '16 at 14:00