Search code examples
cembeddedc-preprocessorlookup-tables

selectable look-up table using pre-processor define


I have a c program that uses a look-up table defined in a separate .c file.

The c program is actually run on a PIC24H and compiled using the MPLAB XC16 compiler. That's not particularly relevant except it provides motivation for what I'm trying to do.

I would like to have several look-up tables, each in their own .c file. I would then like to use the pre-processor to define which table gets loaded into the program code.

I'm unsure if unused tables all in the same .c file would take up program space, or if the compiler would ignore them.

If all tables were stored in the program code, this would take up precious program memory.

Mainly I'm looking for advice on the mechanics of implementing the selective look-up. I'm thinking of below, but not sure how that would actually work.

#define CLEVEL 75 // CLEVEL may be in set {45 60 75}

#if (CLEVEL==45}
#include "clevel45.h"
#elseif (CLEVEL==60}
#include "clevel60.h"
#elseif (CLEVEL==75)
#include "clevel75.h"
#endif

Solution

  • I wouldn't mix files which could not exist in the future, creating useless dependences. Another way:

    your_cfg_file.h:

    #define CLEVEL75
    

    your_clevel75_file.h:

    #ifdef CLEVEL75
    {your parameters}
    #endif
    

    your_clevel60_file.h:

    #ifdef CLEVEL60
    {your parameters}
    #endif
    

    ...and so on...