nickagian
nickagian

Reputation: 663

MPLAB IDE 8.85 conditionally including a header

I have a C Project in MPLAB IDE where I want the main.c file to be able to be used for more than one HWs I have. The main difference is that the pins of the PIC are differently connected.

The idea is to create different .h and .c files for each HW my main file supports and only include this one that corresponds to the HW for which I want to build.

Something like this:

#if defined( HW_1 )
  #include "hw1.h"
#elif defined( HW_2 )
  #include "hw2.h"
#endif

This extra files (hw1.c and hw2.c) include mainly definitions of the functions of the pins, such as:

#define GPI_PG_3V3 RD2

and related functions.

But the problem is that different HWs may have same functionalities, but on different pins. That means the same #define variables may exist for different HWs.

Although I include the respective .h file according to my HW, in the structure of the MPLAB I have the same project and this means I have to include all .h files at the same time. But this leads to the problem that the compiler thinks there are various definitions of the same variable, e.g.:

Error   [237] S:\PIC_Code\ACCEED_4420\Source\acceed1480.c; 23. function "_initGpio" redefined

Does anyone have a good idea on how to solve this? One idea that came to me is to have different projects for all the different HWs I have. Is that the only solution? In such a case it would be difficult how to structure the project directories.

Upvotes: 0

Views: 403

Answers (1)

ElderBug
ElderBug

Reputation: 6145

You might want to take a look at this question. Your problem would be more easily resolved with C++ namespace, but since you use C you will probably have to use some hacks. In the question, the accepted answer probably doesn't fit your case, and the second might add a slight overhead (especially since you seem to use an old compiler). You should try one of the macro hacks in the other answers.

My suggestion, if your compiler doesn't scream, is to use some macro magic to conditionally compile and includes some files. It has the advantage of not compiling everything, which might save some space if the compiler is bad (since it's MPLAB 8, it might be).

In your "my_hw.h" :

// change this line to change the hardware code
#define THE_DRIVER_TO_USE hw1


#define STRINGIFY2(x) #x
#define STRINGIFY(x) STRINGIFY2(x)

#include STRINGIFY(THE_DRIVER_TO_USE.h)

In some "hw_includer.c" file, you put that, and never change it :

// optional macro, see below
#define ALLOWED_TO_COMPILE 1
// yes, include a .c
#include STRINGIFY(THE_DRIVER_TO_USE.c)

Now you just have to change the THE_DRIVER_TO_USE macro to swap drivers, and include my_hw.h instead of hwX.h. It will include the right .h, and compile the right .c. The requirement to this is to NOT compile the hwX.c by themselves. You either exclude them from compilation in the IDE, or put all the code in the .c inside a guard #ifdef ALLOWED_TO_COMPILE #endif if you can't.

Upvotes: 1

Related Questions