I'm looking at some microcontroller C headers and there is code like this:
#define OSCCONL OSCCONL
extern volatile uint8_t OSCCONL __attribute__((__sfr__));
#define OSCCONH OSCCONH
extern volatile uint8_t OSCCONH __attribute__((__sfr__));
#define CLKDIV CLKDIV
extern volatile uint16_t CLKDIV __attribute__((__sfr__));
__extension__ typedef struct tagCLKDIVBITS {...}
What is the purpose of those repetitive "#define A A" ?
CodePudding user response:
As the preprocessor does not know anything about the C language and variables you need to add some definitions know if the variable (variable) will be defined in the C code. You can use those definitions later in the code.
Example:
#if defined(OSCONL)
OSCONL = something;
#endif
And if your microcontroller does not have this register this part of the code will not be compiled. It is a very common technique.
EDIT:
But wouldn't that work the same if it just had #define OSCCONL
The idea is to have the same #define
as variable or function name. It makes code consistent and easy to remember.
Otherwise, you would have to have different #define
and object name making coding more difficult, as our would have to remember both. In this convention, you can read the documentation of the uC and use the same name to check if this object exists without remembering how the #define
is called.
CodePudding user response:
One purpose could be that it can be used in a macro check such as #ifdef or #ifndef
This allows for conditional compiling where only parts of the source code will be compiled into the binary.