CW 4.5 for s12x - #if and sizeof

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CW 4.5 for s12x - #if and sizeof

Jump to solution
1,788 Views
calou
Contributor III
hello,
I use CW 4.5 for s12x
I would like to display an message error if the struct is  too much important:
 
typedef struct
{
 //tensions de références vref1 doit tjrs commencer la structure
 SWORD vref1;
 SWORD vref2;
 SWORD vref3;
 //config des switchs
 SWORD swcfg;
 //variable pour tester si l'eeprom a été configurée ou non
 //0=>non configuré 1=>configurée
 SWORD etat; 
}DATA_CONFIG;
_EEPROM_H_ DATA_CONFIG Config;
 
#if (sizeof(DATA_CONFIG)>2000)
#error "error message"
#endif
 
I have the message Undefined Macro 'sizeof'
How could solve this problem?
 
Thank you for help
 
Alban Edit: s'te plait indique le produit dans le sujet.

Message Edited by Alban on 2007-03-14 03:37 PM

Labels (1)
Tags (1)
0 Kudos
1 Solution
701 Views
CompilerGuru
NXP Employee
NXP Employee
The ANSI-C language does clearly separate the preprocessing from the C language parsing. The sizeof (types, stucts, variables, functions, enums,...) elements are all parts of the C language, during preprocessing they are identifiers like any other too.
I know some compilers (gnu and others) do support to use some features like sizeof in the preprocessor as language extension, but this extension is not available in CW for HC12 V4.5.

I don't have a really clean solution for you, but at least a solution. The point is that you can cause the compiler to generate an error both a preprocess time, as you tried, or at "compile" (say C parsing) time. During the parsing, things like sizeof are known. The reason why it is not so clean is that there is no equivalent to the preprocessor #error directive, so I'm usually "abusing" some other condition in the language to get the compile time test.

A example, to give an idea.
extern char TestCondition[2];
#define COMPILE_TIME_CHECK(cond) extern char TestCondition[(cond)+1];
COMPILE_TIME_CHECK(sizeof(int) == 3);

downside: using some other error message, unless you look at the location from where the error happens, the message maybe confusing.
upside: it's the ANSI-C compliant version.

Daniel

View solution in original post

0 Kudos
2 Replies
702 Views
CompilerGuru
NXP Employee
NXP Employee
The ANSI-C language does clearly separate the preprocessing from the C language parsing. The sizeof (types, stucts, variables, functions, enums,...) elements are all parts of the C language, during preprocessing they are identifiers like any other too.
I know some compilers (gnu and others) do support to use some features like sizeof in the preprocessor as language extension, but this extension is not available in CW for HC12 V4.5.

I don't have a really clean solution for you, but at least a solution. The point is that you can cause the compiler to generate an error both a preprocess time, as you tried, or at "compile" (say C parsing) time. During the parsing, things like sizeof are known. The reason why it is not so clean is that there is no equivalent to the preprocessor #error directive, so I'm usually "abusing" some other condition in the language to get the compile time test.

A example, to give an idea.
extern char TestCondition[2];
#define COMPILE_TIME_CHECK(cond) extern char TestCondition[(cond)+1];
COMPILE_TIME_CHECK(sizeof(int) == 3);

downside: using some other error message, unless you look at the location from where the error happens, the message maybe confusing.
upside: it's the ANSI-C compliant version.

Daniel
0 Kudos
701 Views
calou
Contributor III
Thank you Daniel for your help
 
Regards
0 Kudos