I am writing some code to interface with a specific chip. As such, I have created two standalone files (chip.h and chip.c) to provide a set of functions ("library", but not in the official sense) that talk to the chip.
The idea is to incorporate these files into [any number of] other projects as needed. I want to define a parameter to be used in the chip.c file, but only if it is not defined elsewhere.
Currently I have:
projectDef.h: (chip.h actually included elsewhere)
#define RX_BUF_SZ 250
chip.h: (included in chip.c)
#include "projectDef.h" #ifndef RX_BUF_SZ #define RX_BUF_SZ 100 #endif
I believe that, as written, this will give me the behavior I am looking for (because the #include is before the #ifndef) - meaning, when referenced in the chip.c file, RX_BUF_SZ will be replaced by 250 (not 100). If this is wrong, please let me know.
Is there a better way to do this? By having the #include "projectDef.h" line, I have now made this file slightly less portable (i.e., that line would need to be changed for every project). From a logical standpoint, I don't see a way around this, but I am hoping my ignorance of the build process may be clouding my judgement.
Be gentle...