In my project I have to include only one of two different header files:
ha.h, hb.h
based on a predefined parameter: p.
These two header files contain different functions.
ha.h contains function a: void a(){};
hb.h contains function b: void b(){};
In main program I need to call either function a or function b depend on which header file is included.
How can I manage that?
Need more info, is your "predefined parameter: p" known at compile-time? Or, only run-time?
Also, you shouldn't put function definitions in a header file, only their prototypes. The definitions belong in a separate .cpp or perhaps a .c file. See Reply #3 Here.
I assume that the real situation is more complicated than you say, but you cannot include files at run time, so your parameter p must be known at compile time. If you put both functions in the same file then only the one that is used will be compiled into the program.
The compiler is very smart. If you never use a() then it won't compile it and it takes up no space.
The same way you used to decide to include ha.h or hb.h should be used in the code which calls a() and b()
For example....
#define USEA 1
#define USEB 2
#define p USEA
#if(p==USEA)
#include "ha.h"
#endif
...
void loop() {
#if(p==USEA)
a();
#endif
Thanks everyone, thanks MorganS
Problem solved. I never thought #if #endif can be used in loop() function.