 |
 |
| View previous topic :: View next topic |
| Author |
Message |
lucromain
Joined: 04 Apr 2011 Posts: 5
|
| Using Preprocessor macros for register declaration and index |
Posted: Tue Nov 04, 2025 1:05 pm |
|
|
I am using the following preprocessor macro to declare registers and also create an index for each declared register:
| Code: |
const int8 RegCnt = 0;
#define REG_DEFINE_ARRAY(name,dv,size) \
int8 name[size]; \
const int8 Offset_##name = RegCnt; \
const int8 DefaultValue_##name = dv ; \
const int8 RCT = RegCnt; \
#undef RegCnt \
const int8 RegCnt = (RCT + size); \
#undef RCT
|
This macro is called multiple times as shown here:
| Code: | //
// Register name DefaultValue Size
// ------------- ------------ ---
REG_DEFINE_ARRAY(AdminPwd , 0x01 , 8 )
REG_DEFINE_ARRAY(SiteIDMorse , 0x00 , 6 )
...many other registers declared here...
|
This compiled successfully when using V4.140.
When compiling with V5.121, this error appears:
| Code: |
1 Errors, 0 Warnings, Build Failed., Time: 1 Seconds
--- Info 300 "<path>/MyPIC.h" Line 101(165,171): More info: First Declaration of RegCnt
Error[31] <path>/MyPIC.h 134 : Identifier is already used in this scope
|
Line 134 points to the first REG_DEFINE_ARRAY call.
Is there a way to use preprocessor macros to declare and count registers that would be compatible with V5.121? |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19970
|
|
Posted: Wed Nov 05, 2025 11:05 am |
|
|
The problem is that #undef doesn't do what you think.
It should not have worked. That it did was a fluke....
#undef undefines a macro, not a variable name. Hence the failure.
You don't actually need to undef at all. Just use a different name.
C can be made to generate a unique variable name each time a macro
is called.
There are some bits of this that seem silly/pointless.
const, says to put a variable into the ROM. You are telling the compiler to
put RCT into ROM, and then tell it to forget about it at the end of the macro.
Why?. Just make this a macro definition, and it can then be forgotten with
#undef. It never needs to actually be a variable. |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|